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.Shorts.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import org.apache.juneau.commons.svl.*;
023import org.apache.juneau.http.response.*;
024import org.apache.juneau.marshall.*;
025import org.apache.juneau.rest.server.*;
026
027/**
028 * Request attribute variable resolver.
029 *
030 * <p>
031 * The format for this var is <js>"$R{key1[,key2...]}"</js>.
032 * <br>When multiple keys are used, returns the first non-null/empty value.
033 *
034 * <p>
035 * The possible values are:
036 * <ul>
037 *    <li><js>"authorityPath"</js> - Value returned by {@link RestRequest#getAuthorityPath()}
038 *    <li><js>"contextPath"</js> - Value returned by {@link RestRequest#getContextPath()}
039 *    <li><js>"method"</js> - Value returned by {@link RestRequest#getMethod()}
040 *    <li><js>"pathInfo"</js> - Value returned by {@link RestRequest#getPathInfo()}
041 *    <li><js>"requestParentURI"</js> - Value returned by {@link UriContext#getRootRelativePathInfoParent()}
042 *    <li><js>"requestURI"</js> - Value returned by {@link RestRequest#getRequestURI()}
043 *    <li><js>"servletClass"</js> - The fully-qualified name of the resource class (see {@link RestContext#getResourceClass()})
044 *    <li><js>"servletClassSimple"</js> - The simple name of the resource class (see {@link RestContext#getResourceClass()})
045 *    <li><js>"servletParentURI"</js> - Value returned by {@link UriContext#getRootRelativeServletPathParent()}
046 *    <li><js>"servletPath"</js> - See {@link RestRequest#getServletPath()}
047 *    <li><js>"servletURI"</js> - See {@link UriContext#getRootRelativeServletPath()}
048 * </ul>
049 *
050 * <h5 class='section'>Example:</h5>
051 * <p class='bjava'>
052 *    String <jv>servletClass</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$R{servletClass}"</js>);
053 * </p>
054 *
055 * <h5 class='section'>Notes:</h5><ul>
056 *    <li class='note'>
057 *       This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
058 *    <li class='note'>
059 *       For security reasons, nested and recursive variables are not resolved.
060 * </ul>
061 *
062 * <h5 class='section'>See Also:</h5><ul>
063 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">REST Server SVL Variables</a>
064 * </ul>
065 */
066public class RequestVar extends MultipartResolvingVar {
067
068   /** The name of this variable. */
069   public static final String NAME = "R";
070
071   /**
072    * Constructor.
073    */
074   public RequestVar() {
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   @SuppressWarnings({
085      "java:S3776" // Cognitive complexity acceptable for request variable resolution
086   })
087   public String resolve(VarResolverSession session, String key) {
088      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
089      char c = charAt(key, 0);
090      if (c == 'a') {
091         if (eq(key, "authorityPath"))
092            return req.getAuthorityPath();
093      } else if (c == 'c') {
094         if (eq(key, "contextPath"))
095            return req.getContextPath();
096      } else if (c == 'm') {
097         if (eq(key, "method"))
098            return req.getMethod();
099      } else if (c == 'p') {
100         if (eq(key, "pathInfo"))
101            return req.getPathInfo();
102      } else if (c == 'r') {
103         if (eq(key, "requestParentURI"))
104            return req.getUriContext().getRootRelativePathInfoParent();
105         if (eq(key, "requestURI"))
106            return req.getRequestURI();
107      } else if (c == 's') {
108         if (eq(key, "servletClass"))
109            return req.getContext().getResourceClass().getName();
110         if (eq(key, "servletClassSimple"))
111            return req.getContext().getResourceClass().getSimpleName();
112         if (eq(key, "servletParentURI"))
113            return req.getUriContext().getRootRelativeServletPathParent();
114         if (eq(key, "servletPath"))
115            return req.getServletPath();
116         if (eq(key, "servletURI"))
117            return req.getUriContext().getRootRelativeServletPath();
118      }
119      return req.getAttributes().get(key).asString().orElse(null);
120   }
121
122   @Override /* Overridden from Var */
123   protected boolean allowNested() {
124      return false;
125   }
126
127   @Override /* Overridden from Var */
128   protected boolean allowRecurse() {
129      return false;
130   }
131}