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.*;
016import org.apache.juneau.internal.*;
017import org.apache.juneau.rest.*;
018import org.apache.juneau.svl.*;
019
020/**
021 * Request attribute variable resolver.
022 *
023 * <p>
024 * The format for this var is <js>"$R{key1[,key2...]}"</js>.
025 * <br>When multiple keys are used, returns the first non-null/empty value.
026 *
027 * <p>
028 * The possible values are:
029 * <ul>
030 *    <li><js>"authorityPath"</js> - Value returned by {@link RestRequest#getAuthorityPath()}
031 *    <li><js>"contextPath"</js> - Value returned by {@link RestRequest#getContextPath()}
032 *    <li><js>"method"</js> - Value returned by {@link RestRequest#getMethod()}
033 *    <li><js>"methodDescription"</js> - Value returned by {@link RestRequest#getMethodDescription()}
034 *    <li><js>"methodSummary"</js> - Value returned by {@link RestRequest#getMethodSummary()}
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>"resourceDescription"</js> - Value returned by {@link RestRequest#getResourceDescription()}
039 *    <li><js>"resourceTitle"</js> - See {@link RestRequest#getResourceTitle()}
040 *    <li><js>"servletParentURI"</js> - Value returned by {@link UriContext#getRootRelativeServletPathParent()}
041 *    <li><js>"servletPath"</js> - See {@link RestRequest#getServletPath()}
042 *    <li><js>"servletURI"</js> - See {@link UriContext#getRootRelativeServletPath()}
043 * </ul>
044 *
045 * <h5 class='section'>Example:</h5>
046 * <p class='bcode w800'>
047 *    String resourceTitle = restRequest.resolveVars(<js>"$R{resourceTitle}"</js>);
048 *    String resourceTitleOrDescription = restRequest.resolveVars(<js>"$R{resourceTitle,resourceDescription}"</js>);
049 * </p>
050 *
051 * <ul class='notes'>
052 *    <li>
053 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
054 *       or a session object on the resolver session.
055 *    <li>
056 *       For security reasons, nested and recursive variables are not resolved.
057 * </ul>
058 *
059 * <ul class='seealso'>
060 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
061 * </ul>
062 */
063public class RequestVar extends MultipartResolvingVar {
064
065   private static final String SESSION_req = "req";
066   private static final String SESSION_res = "res";
067
068
069   /** The name of this variable. */
070   public static final String NAME = "R";
071
072   /**
073    * Constructor.
074    */
075   public RequestVar() {
076      super(NAME);
077   }
078
079   @Override /* Var */
080   protected boolean allowNested() {
081      return false;
082   }
083
084   @Override /* Var */
085   protected boolean allowRecurse() {
086      return false;
087   }
088
089   @Override /* Var */
090   public String resolve(VarResolverSession session, String key) {
091      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
092      char c = StringUtils.charAt(key, 0);
093      if (c == 'a') {
094         if ("authorityPath".equals(key))
095            return req.getAuthorityPath();
096      } else if (c == 'c') {
097         if ("contextPath".equals(key))
098            return req.getContextPath();
099      } else if (c == 'm') {
100         if ("method".equals(key))
101            return req.getMethod();
102         if ("methodDescription".equals(key))
103            return req.getMethodDescription();
104         if ("methodSummary".equals(key))
105            return req.getMethodSummary();
106      } else if (c == 'p') {
107         if ("pathInfo".equals(key))
108            return req.getPathInfo();
109      } else if (c == 'r') {
110         if ("requestParentURI".equals(key))
111            return req.getUriContext().getRootRelativePathInfoParent();
112         if ("requestURI".equals(key))
113            return req.getRequestURI();
114         if ("resourceDescription".equals(key))
115            return req.getResourceDescription();
116         if ("resourceTitle".equals(key))
117            return req.getResourceTitle();
118      } else if (c == 's') {
119         if ("servletClass".equals(key))
120            return req.getContext().getResource().getClass().getName();
121         if ("servletClassSimple".equals(key))
122            return req.getContext().getResource().getClass().getSimpleName();
123         if ("servletParentURI".equals(key))
124            return req.getUriContext().getRootRelativeServletPathParent();
125         if ("servletPath".equals(key))
126            return req.getServletPath();
127         if ("servletURI".equals(key))
128            return req.getUriContext().getRootRelativeServletPath();
129         if ("siteName".equals(key))
130            return req.getSiteName();
131      }
132      return req.getAttributes().getString(key);
133   }
134
135   @Override /* Var */
136   public boolean canResolve(VarResolverSession session) {
137      return session.hasSessionObject(SESSION_req) && session.hasSessionObject(SESSION_res);
138   }
139}