001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.vars; 018 019import org.apache.juneau.http.response.*; 020import org.apache.juneau.rest.*; 021import org.apache.juneau.svl.*; 022 023/** 024 * Request path variable resolver. 025 * 026 * <p> 027 * The format for this var is <js>"$RP{key1[,key2...]}"</js>. 028 * 029 * <p> 030 * Used to resolve values returned by {@link RestRequest#getPathParam(String)}. 031 * <br>When multiple keys are used, returns the first non-null/empty value. 032 * 033 * <h5 class='section'>Example:</h5> 034 * <p class='bjava'> 035 * <jc>// URI path pattern = "/foo/{foo}/bar/{bar}"</jc> 036 * String <jv>foo</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RP{foo}"</js>); 037 * String <jv>fooOrBar</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RP{foo,bar}"</js>); 038 * </p> 039 * 040 * <h5 class='section'>Notes:</h5><ul> 041 * <li class='note'> 042 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 043 * <li class='note'> 044 * For security reasons, nested and recursive variables are not resolved. 045 * </ul> 046 * 047 * <h5 class='section'>See Also:</h5><ul> 048 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a> 049 * </ul> 050 */ 051public class RequestPathVar extends MultipartResolvingVar { 052 053 /** The name of this variable. */ 054 public static final String NAME = "RP"; 055 056 /** 057 * Constructor. 058 */ 059 public RequestPathVar() { 060 super(NAME); 061 } 062 063 @Override /* Var */ 064 protected boolean allowNested() { 065 return false; 066 } 067 068 @Override /* Var */ 069 protected boolean allowRecurse() { 070 return false; 071 } 072 073 @Override /* Var */ 074 public String resolve(VarResolverSession session, String key) { 075 RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new); 076 if ("REMAINDER".equals(key)) 077 return req.getPathParams().getRemainder().orElse(null); 078 return req.getPathParam(key).orElse(null); 079 } 080 081 @Override /* Var */ 082 public boolean canResolve(VarResolverSession session) { 083 return session.getBean(RestRequest.class).isPresent(); 084 } 085}