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