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.server.vars;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import org.apache.juneau.commons.svl.*;
022import org.apache.juneau.http.response.*;
023import org.apache.juneau.rest.server.*;
024
025/**
026 * Request path variable resolver.
027 *
028 * <p>
029 * The format for this var is <js>"$RP{key1[,key2...]}"</js>.
030 *
031 * <p>
032 * Used to resolve values returned by {@link RestRequest#getPathParam(String)}.
033 * <br>When multiple keys are used, returns the first non-null/empty value.
034 *
035 * <h5 class='section'>Example:</h5>
036 * <p class='bjava'>
037 *    <jc>// URI path pattern = "/foo/{foo}/bar/{bar}"</jc>
038 *    String <jv>foo</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RP{foo}"</js>);
039 *    String <jv>fooOrBar</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RP{foo,bar}"</js>);
040 * </p>
041 *
042 * <h5 class='section'>Notes:</h5><ul>
043 *    <li class='note'>
044 *       This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
045 *    <li class='note'>
046 *       For security reasons, nested and recursive variables are not resolved.
047 * </ul>
048 *
049 * <h5 class='section'>See Also:</h5><ul>
050 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">REST Server SVL Variables</a>
051 * </ul>
052 */
053public class RequestPathVar extends MultipartResolvingVar {
054
055   /** The name of this variable. */
056   public static final String NAME = "RP";
057
058   /**
059    * Constructor.
060    */
061   public RequestPathVar() {
062      super(NAME);
063   }
064
065   @Override /* Overridden from Var */
066   public boolean canResolve(VarResolverSession session) {
067      return session.getBean(RestRequest.class).isPresent();
068   }
069
070   @Override /* Overridden from Var */
071   public String resolve(VarResolverSession session, String key) {
072      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
073      if (eq(key, "REMAINDER"))
074         return req.getPathParams().getRemainder().orElse(null);
075      return req.getPathParam(key).orElse(null);
076   }
077
078   @Override /* Overridden from Var */
079   protected boolean allowNested() {
080      return false;
081   }
082
083   @Override /* Overridden from Var */
084   protected boolean allowRecurse() {
085      return false;
086   }
087}