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>"contextPath"</js> - Value returned by {@link RestRequest#getContextPath()}
031 *    <li><js>"method"</js> - Value returned by {@link RestRequest#getMethod()}
032 *    <li><js>"methodDescription"</js> - Value returned by {@link RestRequest#getMethodDescription()}
033 *    <li><js>"methodSummary"</js> - Value returned by {@link RestRequest#getMethodSummary()}
034 *    <li><js>"pathInfo"</js> - Value returned by {@link RestRequest#getPathInfo()}
035 *    <li><js>"requestParentURI"</js> - Value returned by {@link UriContext#getRootRelativePathInfoParent()}
036 *    <li><js>"requestURI"</js> - Value returned by {@link RestRequest#getRequestURI()}
037 *    <li><js>"resourceDescription"</js> - Value returned by {@link RestRequest#getResourceDescription()}
038 *    <li><js>"resourceTitle"</js> - See {@link RestRequest#getResourceTitle()}
039 *    <li><js>"servletParentURI"</js> - Value returned by {@link UriContext#getRootRelativeServletPathParent()}
040 *    <li><js>"servletPath"</js> - See {@link RestRequest#getServletPath()}
041 *    <li><js>"servletURI"</js> - See {@link UriContext#getRootRelativeServletPath()}
042 *    <li><js>"siteName"</js> - See {@link RestRequest#getSiteName()}
043 * </ul>
044 * 
045 * <h5 class='section'>Example:</h5>
046 * <p class='bcode'>
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'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.SvlVariables">Overview &gt; juneau-rest-server &gt; SVL Variables</a>
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);
096      char c = StringUtils.charAt(key, 0);
097      if (c == 'c') {
098         if ("contextPath".equals(key))
099            return req.getContextPath();
100      } else if (c == 'm') {
101         if ("method".equals(key))
102            return req.getMethod();
103         if ("methodDescription".equals(key))
104            return req.getMethodDescription();
105         if ("methodSummary".equals(key))
106            return req.getMethodSummary();
107      } else if (c == 'p') {
108         if ("pathInfo".equals(key))
109            return req.getPathInfo();
110      } else if (c == 'r') {
111         if ("requestParentURI".equals(key))
112            return req.getUriContext().getRootRelativePathInfoParent();
113         if ("requestURI".equals(key))
114            return req.getRequestURI();
115         if ("resourceDescription".equals(key))
116            return req.getResourceDescription();
117         if ("resourceTitle".equals(key))
118            return req.getResourceTitle();
119      } else if (c == 's') {
120         if ("servletClass".equals(key))
121            return req.getContext().getResource().getClass().getName();
122         if ("servletClassSimple".equals(key))
123            return req.getContext().getResource().getClass().getSimpleName();
124         if ("servletParentURI".equals(key))
125            return req.getUriContext().getRootRelativeServletPathParent();
126         if ("servletPath".equals(key))
127            return req.getServletPath();
128         if ("servletURI".equals(key))
129            return req.getUriContext().getRootRelativeServletPath();
130         if ("siteName".equals(key))
131            return req.getSiteName();
132      }
133      return req.getProperties().getString(key);
134   }
135}