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 path variable resolver. 020 * 021 * <p> 022 * The format for this var is <js>"$RP{key1[,key2...]}"</js>. 023 * 024 * <p> 025 * Used to resolve values returned by {@link RestRequest#getPath(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'> 030 * <jc>// URI path pattern = "/foo/{foo}/bar/{bar}"</jc> 031 * String foo = restRequest.resolveVars(<js>"$RP{foo}"</js>); 032 * String fooOrBar = restRequest.resolveVars(<js>"$RP{foo,bar}"</js>); 033 * </p> 034 * 035 * <h5 class='section'>Notes:</h5> 036 * <ul class='spaced-list'> 037 * <li> 038 * This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver 039 * or a session object on the resolver session. 040 * <li> 041 * For security reasons, nested and recursive variables are not resolved. 042 * </ul> 043 * 044 * <h5 class='section'>See Also:</h5> 045 * <ul> 046 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.SvlVariables">Overview > juneau-rest-server > SVL Variables</a> 047 * </ul> 048 */ 049public class RequestPathVar extends MultipartResolvingVar { 050 051 /** 052 * The name of the session or context object that identifies the {@link RestRequest} object. 053 */ 054 public static final String SESSION_req = "req"; 055 056 057 /** The name of this variable. */ 058 public static final String NAME = "RP"; 059 060 /** 061 * Constructor. 062 */ 063 public RequestPathVar() { 064 super(NAME); 065 } 066 067 @Override /* Var */ 068 protected boolean allowNested() { 069 return false; 070 } 071 072 @Override /* Var */ 073 protected boolean allowRecurse() { 074 return false; 075 } 076 077 @Override /* Parameter */ 078 public String resolve(VarResolverSession session, String key) { 079 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req); 080 return req.getPath(key); 081 } 082}