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 * <ul class='seealso'>
064 *    <li class='link'>{@doc juneau-svl.SvlVariables}
065 * </ul>
066 */
067public class FileVar extends DefaultingVar {
068
069   private static final String SESSION_req = "req";
070   private static final String SESSION_crm = "crm";
071
072   /**
073    * The name of this variable.
074    */
075   public static final String NAME = "F";
076
077   /**
078    * Constructor.
079    */
080   public FileVar() {
081      super(NAME);
082   }
083
084   @Override /* Var */
085   public String resolve(VarResolverSession session, String key) throws Exception {
086
087      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, false);
088      if (req != null) {
089         ReaderResource rr = req.getClasspathReaderResource(key);
090         return (rr == null ? null : rr.toCommentStrippedString());
091      }
092
093      ClasspathResourceManager crm = session.getSessionObject(ClasspathResourceManager.class, SESSION_crm, false);
094      if (crm != null)
095         return crm.getString(key);
096
097      return null;
098   }
099
100   @Override /* Var */
101   public boolean canResolve(VarResolverSession session) {
102      return session.hasSessionObject(SESSION_req) || session.hasSessionObject(SESSION_crm);
103   }
104}