001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.vars;
018
019import org.apache.juneau.http.response.*;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.rest.*;
022import org.apache.juneau.svl.*;
023
024/**
025 * File resource variable resolver
026 *
027 * <p>
028 * The format for this var is <js>"$F{path[,defaultValue]}"</js>.
029 *
030 * <p>
031 * Contents of files are retrieved from the request using {@link RestRequest#getStaticFiles()}.
032
033 * <p>
034 * Localized resources (based on the locale of the HTTP request) are supported.
035 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for
036 * files in the following order:
037 * <ol>
038 *    <li><js>"MyResource_ja_JP.txt"</js>
039 *    <li><js>"MyResource_ja.txt"</js>
040 *    <li><js>"MyResource.txt"</js>
041 * </ol>
042 *
043 * <p>
044 * Example:
045 * <p class='bjava'>
046 *  <ja>@HtmlDocConfig</ja>(
047 *       aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js>
048 *    )
049 * </p>
050 *
051 * <p>
052 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments.
053 * This allows you to place license headers in files without them being serialized to the output.
054 *
055 * <p>
056 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
057 *
058 * <h5 class='section'>See Also:</h5><ul>
059 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a>
060 * </ul>
061 */
062public class FileVar extends DefaultingVar {
063
064   /**
065    * The name of this variable.
066    */
067   public static final String NAME = "F";
068
069   /**
070    * Constructor.
071    */
072   public FileVar() {
073      super(NAME);
074   }
075
076   @Override /* Var */
077   public String resolve(VarResolverSession session, String key) throws Exception {
078
079      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
080
081      String s = req.getStaticFiles().getString(key, null).orElse(null);
082      if (s == null)
083         return null;
084      String subType = FileUtils.getExtension(key);
085      if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType))
086         s = s.replaceAll("(?s)<!--(.*?)-->\\s*", "");
087      else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType))
088         s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", "");
089      return s;
090   }
091
092   @Override /* Var */
093   public boolean canResolve(VarResolverSession session) {
094      return session.getBean(RestRequest.class).isPresent();
095   }
096}