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