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.rest.*;
016import org.apache.juneau.svl.*;
017import org.apache.juneau.utils.*;
018
019/**
020 * File resource variable resolver
021 * 
022 * <p>
023 * The format for this var is <js>"$F{path[,defaultValue]}"</js>.
024 * 
025 * <p>
026 * File variables resolve to the contents of resource files located on the classpath or local JVM directory.
027 * They use the {@link RestRequest#getClasspathReaderResource(String)} method to retrieve the contents of the file.
028 * That in turn uses the {@link ClasspathResourceFinder} associated with the servlet class to find the file.
029 * 
030 * <p>
031 * The {@link ClasspathResourceFinder} is similar to {@link Class#getResourceAsStream(String)} except if it doesn't find the
032 * resource on this class, it searches up the parent hierarchy chain.
033 * 
034 * <p>
035 * If the resource cannot be found in the classpath, then an attempt is made to look in the JVM working directory.
036 * <br>Path traversals outside the working directory are not allowed for security reasons.
037
038 * <p>
039 * Localized resources (based on the locale of the HTTP request) are supported.
040 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for
041 * files in the following order:
042 * <ol>
043 *    <li><js>"MyResource_ja_JP.txt"</js>
044 *    <li><js>"MyResource_ja.txt"</js>
045 *    <li><js>"MyResource.txt"</js>
046 * </ol>
047 * 
048 * <p>
049 * Example:
050 * <p class='bcode'>
051 *    <ja>@RestResource</ja>(
052 *       htmldoc=<ja>@HtmlDoc</ja>(
053 *          aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js>
054 *       )
055 *    )
056 * </p>
057 * 
058 * <p>
059 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments.
060 * This allows you to place license headers in files without them being serialized to the output.
061 * 
062 * <h5 class='section'>See Also:</h5>
063 * <ul>
064 *    <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-svl.SvlVariables">Overview &gt; juneau-rest-server &gt; SVL Variables</a>
065 * </ul>
066 */
067public class FileVar extends DefaultingVar {
068
069   private static final String SESSION_req = "req";
070
071   /**
072    * The name of this variable.
073    */
074   public static final String NAME = "F";
075
076   /**
077    * Constructor.
078    */
079   public FileVar() {
080      super(NAME);
081   }
082
083   @Override /* Parameter */
084   public String resolve(VarResolverSession session, String key) throws Exception {
085      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req);
086      ReaderResource rr = req.getClasspathReaderResource(key);
087      return (rr == null ? null : rr.toCommentStrippedString());
088   }
089}