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 static org.apache.juneau.internal.StringUtils.*;
016
017import javax.servlet.http.*;
018
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>"$RA{key1[,key2...]}"</js>.
027 *
028 * <p>
029 * Used to resolve values returned by {@link HttpServletRequest#getAttribute(String)}.
030 * <br>When multiple keys are used, returns the first non-null/empty value.
031 *
032 * <h5 class='section'>Example:</h5>
033 * <p class='bcode w800'>
034 *    String foo = restRequest.resolveVars(<js>"$RA{foo}"</js>);
035 *    String fooOrBar = restRequest.resolveVars(<js>"$RA{foo,bar}"</js>);
036 * </p>
037 *
038 * <h5 class='section'>Notes:</h5>
039 * <ul class='spaced-list'>
040 *    <li>
041 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
042 *       or a session object on the resolver session.
043 *    <li>
044 *       For security reasons, nested and recursive variables are not resolved.
045 * </ul>
046 *
047 * <h5 class='section'>See Also:</h5>
048 * <ul>
049 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
050 * </ul>
051 */
052public class RequestAttributeVar extends MultipartResolvingVar {
053
054   /**
055    * The name of the session or context object that identifies the {@link RestRequest} object.
056    */
057   public static final String SESSION_req = "req";
058
059
060   /** The name of this variable. */
061   public static final String NAME = "RA";
062
063   /**
064    * Constructor.
065    */
066   public RequestAttributeVar() {
067      super(NAME);
068   }
069
070   @Override /* Var */
071   protected boolean allowNested() {
072      return false;
073   }
074
075   @Override /* Var */
076   protected boolean allowRecurse() {
077      return false;
078   }
079
080   @Override /* Parameter */
081   public String resolve(VarResolverSession session, String key) {
082      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
083      return asString(req.getAttribute(key));
084   }
085}