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.rest.*;
016import org.apache.juneau.svl.*;
017
018/**
019 * Request query variable resolver.
020 *
021 * <p>
022 * The format for this var is <js>"$RQ{key1[,key2...]}"</js>.
023 *
024 * <p>
025 * Used to resolve values returned by {@link RestRequest#getQuery(String)}.
026 * <br>When multiple keys are used, returns the first non-null/empty value.
027 *
028 * <h5 class='section'>Example:</h5>
029 * <p class='bcode w800'>
030 *    <jc>// URI = "...?foo=X&amp;bar=Y"</jc>
031 *    String foo = restRequest.resolveVars(<js>"$RQ{foo}"</js>);
032 *    String fooOrBar = restRequest.resolveVars(<js>"$RQ{foo,bar}"</js>);
033 * </p>
034 *
035 * <ul class='notes'>
036 *    <li>
037 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
038 *       or a session object on the resolver session.
039 *    <li>
040 *       For security reasons, nested and recursive variables are not resolved.
041 * </ul>
042 *
043 * <ul class='seealso'>
044 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
045 * </ul>
046 */
047public class RequestQueryVar extends MultipartResolvingVar {
048
049   private static final String SESSION_req = "req";
050
051   /** The name of this variable. */
052   public static final String NAME = "RQ";
053
054   /**
055    * Constructor.
056    */
057   public RequestQueryVar() {
058      super(NAME);
059   }
060
061   @Override /* Var */
062   protected boolean allowNested() {
063      return false;
064   }
065
066   @Override /* Var */
067   protected boolean allowRecurse() {
068      return false;
069   }
070
071   @Override /* Var */
072   public String resolve(VarResolverSession session, String key) {
073      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
074      return req.getQuery(key);
075   }
076
077   @Override /* Var */
078   public boolean canResolve(VarResolverSession session) {
079      return session.hasSessionObject(SESSION_req);
080   }
081}