001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.vars; 014 015import org.apache.juneau.cp.*; 016import org.apache.juneau.internal.*; 017import org.apache.juneau.rest.*; 018import org.apache.juneau.svl.*; 019 020/** 021 * File resource variable resolver 022 * 023 * <p> 024 * The format for this var is <js>"$F{path[,defaultValue]}"</js>. 025 * 026 * <p> 027 * File variables resolve to the contents of resource files located on the classpath or local JVM directory. 028 * They use the {@link RestRequest#getClasspathResourceAsString(String)} method to retrieve the contents of the file. 029 * That in turn uses the {@link ResourceFinder} associated with the servlet class to find the file. 030 * 031 * <p> 032 * The {@link ResourceFinder} is similar to {@link Class#getResourceAsStream(String)} except if it doesn't find the 033 * resource on this class, it searches up the parent hierarchy chain. 034 * 035 * <p> 036 * If the resource cannot be found in the classpath, then an attempt is made to look in the JVM working directory. 037 * <br>Path traversals outside the working directory are not allowed for security reasons. 038 039 * <p> 040 * Localized resources (based on the locale of the HTTP request) are supported. 041 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for 042 * files in the following order: 043 * <ol> 044 * <li><js>"MyResource_ja_JP.txt"</js> 045 * <li><js>"MyResource_ja.txt"</js> 046 * <li><js>"MyResource.txt"</js> 047 * </ol> 048 * 049 * <p> 050 * Example: 051 * <p class='bcode w800'> 052 * <ja>@HtmlDocConfig</ja>( 053 * aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js> 054 * ) 055 * </p> 056 * 057 * <p> 058 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments. 059 * This allows you to place license headers in files without them being serialized to the output. 060 * 061 * <ul class='seealso'> 062 * <li class='link'>{@doc SvlVariables} 063 * </ul> 064 */ 065public class FileVar extends DefaultingVar { 066 067 private static final String SESSION_req = "req"; 068 private static final String SESSION_crm = "crm"; 069 070 /** 071 * The name of this variable. 072 */ 073 public static final String NAME = "F"; 074 075 /** 076 * Constructor. 077 */ 078 public FileVar() { 079 super(NAME); 080 } 081 082 @Override /* Var */ 083 public String resolve(VarResolverSession session, String key) throws Exception { 084 085 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, false); 086 if (req != null) { 087 String s = req.getClasspathResourceAsString(key); 088 if (s == null) 089 return null; 090 String subType = FileUtils.getExtension(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 098 ResourceManager crm = session.getSessionObject(ResourceManager.class, SESSION_crm, false); 099 if (crm != null) 100 return crm.getString(key); 101 102 return null; 103 } 104 105 @Override /* Var */ 106 public boolean canResolve(VarResolverSession session) { 107 return session.hasSessionObject(SESSION_req) || session.hasSessionObject(SESSION_crm); 108 } 109}