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