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.server.vars;
018
019import static org.apache.juneau.commons.utils.FileUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import org.apache.juneau.commons.svl.*;
023import org.apache.juneau.http.response.*;
024import org.apache.juneau.rest.server.*;
025
026/**
027 * File resource variable resolver
028 *
029 * <p>
030 * The format for this var is <js>"$F{path[,defaultValue]}"</js>.
031 *
032 * <p>
033 * Contents of files are retrieved from the resource context using {@link RestContext#getStaticFiles()}.
034
035 * <p>
036 * Localized resources (based on the locale of the HTTP request) are supported.
037 * For example, if looking for the resource <js>"MyResource.txt"</js> for the Japanese locale, we will look for
038 * files in the following order:
039 * <ol>
040 *    <li><js>"MyResource_ja_JP.txt"</js>
041 *    <li><js>"MyResource_ja.txt"</js>
042 *    <li><js>"MyResource.txt"</js>
043 * </ol>
044 *
045 * <p>
046 * Example:
047 * <p class='bjava'>
048 *  <ja>@HtmlDocConfig</ja>(
049 *       aside=<js>"$F{resources/MyAsideMessage.html, Oops not found!}"</js>
050 *    )
051 * </p>
052 *
053 * <p>
054 * Files of type HTML, XHTML, XML, JSON, Javascript, and CSS will be stripped of comments.
055 * This allows you to place license headers in files without them being serialized to the output.
056 *
057 * <p>
058 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
059 *
060 * <h5 class='section'>See Also:</h5><ul>
061 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">REST Server SVL Variables</a>
062 * </ul>
063 */
064public class FileVar extends DefaultingVar {
065
066   /**
067    * The name of this variable.
068    */
069   public static final String NAME = "F";
070
071   /**
072    * Constructor.
073    */
074   public FileVar() {
075      super(NAME);
076   }
077
078   @Override /* Overridden from Var */
079   public boolean canResolve(VarResolverSession session) {
080      return session.getBean(RestRequest.class).isPresent();
081   }
082
083   @Override /* Overridden from Var */
084   public String resolve(VarResolverSession session, String key) throws Exception {
085
086      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
087
088      String s = req.getContext().getStaticFiles().getString(key, null).orElse(null);
089      if (s == null)
090         return null;
091      String subType = getFileExtension(key);
092      if (eqa(subType, "html", "xhtml", "xml"))
093         s = s.replaceAll("(?s)<!--(.*?)-->\\s*", "");
094      else if (eqa(subType, "json", "javascript", "css"))
095         s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", "");
096      return s;
097   }
098}