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
019
020import org.apache.juneau.http.response.*;
021import org.apache.juneau.rest.*;
022import org.apache.juneau.svl.*;
023
024/**
025 * Request form data variable resolver.
026 *
027 * <p>
028 * The format for this var is <js>"$RF{key1[,key2...]}"</js>.
029 *
030 * <p>
031 * Used to resolve values returned by {@link RestRequest#getFormParam(String)}.
032 * <br>When multiple keys are used, returns the first non-null/empty value.
033 *
034 * <h5 class='section'>Example:</h5>
035 * <p class='bjava'>
036 *    String <jv>foo</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RF{foo}"</js>);
037 *    String <jv>fooOrBar</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RF{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 RequestFormDataVar extends MultipartResolvingVar {
052
053   /** The name of this variable. */
054   public static final String NAME = "RF";
055
056   /**
057    * Constructor.
058    */
059   public RequestFormDataVar() {
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      return session.getBean(RestRequest.class).orElseThrow(InternalServerError::new).getFormParam(key).orElse(null);
076   }
077
078   @Override /* Var */
079   public boolean canResolve(VarResolverSession session) {
080      return session.getBean(RestRequest.class).isPresent();
081   }
082}