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>@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 juneau-marshall.SimpleVariableLanguage.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         ReaderResource rr = req.getClasspathReaderResource(key);
088         return (rr == null ? null : rr.toCommentStrippedString());
089      }
090
091      ClasspathResourceManager crm = session.getSessionObject(ClasspathResourceManager.class, SESSION_crm, false);
092      if (crm != null)
093         return crm.getString(key);
094
095      return null;
096   }
097
098   @Override /* Var */
099   public boolean canResolve(VarResolverSession session) {
100      return session.hasSessionObject(SESSION_req) || session.hasSessionObject(SESSION_crm);
101   }
102}