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.vars; 018 019import static org.apache.juneau.commons.utils.FileUtils.*; 020 021import org.apache.juneau.http.response.*; 022import org.apache.juneau.rest.*; 023import org.apache.juneau.svl.*; 024 025/** 026 * File resource variable resolver 027 * 028 * <p> 029 * The format for this var is <js>"$F{path[,defaultValue]}"</js>. 030 * 031 * <p> 032 * Contents of files are retrieved from the request using {@link RestRequest#getStaticFiles()}. 033 034 * <p> 035 * Localized resources (based on the locale of the HTTP request) are supported. 036 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for 037 * files in the following order: 038 * <ol> 039 * <li><js>"MyResource_ja_JP.txt"</js> 040 * <li><js>"MyResource_ja.txt"</js> 041 * <li><js>"MyResource.txt"</js> 042 * </ol> 043 * 044 * <p> 045 * Example: 046 * <p class='bjava'> 047 * <ja>@HtmlDocConfig</ja>( 048 * aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js> 049 * ) 050 * </p> 051 * 052 * <p> 053 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments. 054 * This allows you to place license headers in files without them being serialized to the output. 055 * 056 * <p> 057 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 058 * 059 * <h5 class='section'>See Also:</h5><ul> 060 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a> 061 * </ul> 062 */ 063public class FileVar extends DefaultingVar { 064 065 /** 066 * The name of this variable. 067 */ 068 public static final String NAME = "F"; 069 070 /** 071 * Constructor. 072 */ 073 public FileVar() { 074 super(NAME); 075 } 076 077 @Override /* Overridden from Var */ 078 public boolean canResolve(VarResolverSession session) { 079 return session.getBean(RestRequest.class).isPresent(); 080 } 081 082 @Override /* Overridden from Var */ 083 public String resolve(VarResolverSession session, String key) throws Exception { 084 085 RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new); 086 087 String s = req.getStaticFiles().getString(key, null).orElse(null); 088 if (s == null) 089 return null; 090 String subType = getFileExtension(key); 091 if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType)) 092 s = s.replaceAll("(?s)<!--(.*?)-->\\s*", ""); 093 else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType)) 094 s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", ""); 095 return s; 096 } 097}