001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.server.view.freemarker; 018 019import org.apache.juneau.rest.server.*; 020 021/** 022 * Request-scoped handle so FreeMarker {@code TemplateDirectiveModel}s can resolve per-request 023 * asset URLs during {@code template.process}. 024 * 025 * <p> 026 * {@link FreemarkerViewRenderer} opens the scope around its {@code template.process(...)} call and 027 * always closes it in a {@code finally}, so directives such as {@code <@page toolkit=...>} can reach 028 * the in-flight {@link RestRequest} (e.g. to call {@code ViewsMixin.viewAssetUrl}) without threading 029 * it through every template model. 030 * 031 * @since 10.0.0 032 */ 033public final class FreemarkerRenderScope { 034 035 private static final ThreadLocal<RestRequest> REQ = new ThreadLocal<>(); 036 037 private FreemarkerRenderScope() {} 038 039 static void open(RestRequest req) { 040 REQ.set(req); 041 } 042 043 static void close() { 044 REQ.remove(); 045 } 046 047 /** 048 * Returns the request for the in-flight FreeMarker render. 049 * 050 * @return The {@link RestRequest} for the in-flight FreeMarker render, or {@code null} if none. 051 */ 052 public static RestRequest request() { 053 return REQ.get(); 054 } 055}