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 * <h5 class='section'>Notes:</h5>
052 * <ul class='spaced-list'>
053 *    <li>
054 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
055 *       or a session object on the resolver session.
056 *    <li>
057 *       For security reasons, nested and recursive variables are not resolved.
058 * </ul>
059 *
060 * <h5 class='section'>See Also:</h5>
061 * <ul>
062 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
063 * </ul>
064 */
065public class RequestVar extends MultipartResolvingVar {
066
067   /**
068    * The name of the session or context object that identifies the {@link RestRequest} object.
069    */
070   public static final String SESSION_req = "req";
071
072
073   /** The name of this variable. */
074   public static final String NAME = "R";
075
076   /**
077    * Constructor.
078    */
079   public RequestVar() {
080      super(NAME);
081   }
082
083   @Override /* Var */
084   protected boolean allowNested() {
085      return false;
086   }
087
088   @Override /* Var */
089   protected boolean allowRecurse() {
090      return false;
091   }
092
093   @Override /* Parameter */
094   public String resolve(VarResolverSession session, String key) {
095      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
096      char c = StringUtils.charAt(key, 0);
097      if (c == 'a') {
098         if ("authorityPath".equals(key))
099            return req.getAuthorityPath();
100      } else if (c == 'c') {
101         if ("contextPath".equals(key))
102            return req.getContextPath();
103      } else if (c == 'm') {
104         if ("method".equals(key))
105            return req.getMethod();
106         if ("methodDescription".equals(key))
107            return req.getMethodDescription();
108         if ("methodSummary".equals(key))
109            return req.getMethodSummary();
110      } else if (c == 'p') {
111         if ("pathInfo".equals(key))
112            return req.getPathInfo();
113      } else if (c == 'r') {
114         if ("requestParentURI".equals(key))
115            return req.getUriContext().getRootRelativePathInfoParent();
116         if ("requestURI".equals(key))
117            return req.getRequestURI();
118         if ("resourceDescription".equals(key))
119            return req.getResourceDescription();
120         if ("resourceTitle".equals(key))
121            return req.getResourceTitle();
122      } else if (c == 's') {
123         if ("servletClass".equals(key))
124            return req.getContext().getResource().getClass().getName();
125         if ("servletClassSimple".equals(key))
126            return req.getContext().getResource().getClass().getSimpleName();
127         if ("servletParentURI".equals(key))
128            return req.getUriContext().getRootRelativeServletPathParent();
129         if ("servletPath".equals(key))
130            return req.getServletPath();
131         if ("servletURI".equals(key))
132            return req.getUriContext().getRootRelativeServletPath();
133         if ("siteName".equals(key))
134            return req.getSiteName();
135      }
136      return req.getProperties().getString(key);
137   }
138}