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.http.ReaderResource; 016import org.apache.juneau.rest.*; 017import org.apache.juneau.svl.*; 018import org.apache.juneau.utils.*; 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#getClasspathReaderResource(String)} method to retrieve the contents of the file. 029 * That in turn uses the {@link ClasspathResourceFinder} associated with the servlet class to find the file. 030 * 031 * <p> 032 * The {@link ClasspathResourceFinder} 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>@RestResource</ja>( 053 * htmldoc=<ja>@HtmlDoc</ja>( 054 * aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js> 055 * ) 056 * ) 057 * </p> 058 * 059 * <p> 060 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments. 061 * This allows you to place license headers in files without them being serialized to the output. 062 * 063 * <h5 class='section'>See Also:</h5> 064 * <ul> 065 * <li class='link'>{@doc juneau-svl.SvlVariables} 066 * </ul> 067 */ 068public class FileVar extends DefaultingVar { 069 070 private static final String SESSION_req = "req"; 071 private static final String SESSION_crm = "crm"; 072 073 /** 074 * The name of this variable. 075 */ 076 public static final String NAME = "F"; 077 078 /** 079 * Constructor. 080 */ 081 public FileVar() { 082 super(NAME); 083 } 084 085 @Override /* Parameter */ 086 public String resolve(VarResolverSession session, String key) throws Exception { 087 088 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, false); 089 if (req != null) { 090 ReaderResource rr = req.getClasspathReaderResource(key); 091 return (rr == null ? null : rr.toCommentStrippedString()); 092 } 093 094 ClasspathResourceManager crm = session.getSessionObject(ClasspathResourceManager.class, SESSION_crm, false); 095 if (crm != null) 096 return crm.getString(key); 097 098 return null; 099 } 100}