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
023import jakarta.servlet.http.*;
024
025/**
026 * Request attribute variable resolver.
027 *
028 * <p>
029 * The format for this var is <js>"$RA{key1[,key2...]}"</js>.
030 *
031 * <p>
032 * Used to resolve values returned by {@link HttpServletRequest#getAttribute(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 *    String <jv>foo</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RA{foo}"</js>);
038 *    String <jv>fooOrBar</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RA{foo,bar}"</js>);
039 * </p>
040 *
041 * <h5 class='section'>Notes:</h5><ul>
042 *    <li class='note'>
043 *       This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
044 *    <li class='note'>
045 *       For security reasons, nested and recursive variables are not resolved.
046 * </ul>
047 *
048 * <h5 class='section'>See Also:</h5><ul>
049 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerSvlVariables">SVL Variables</a>
050 * </ul>
051 */
052public class RequestAttributeVar extends MultipartResolvingVar {
053
054   /** The name of this variable. */
055   public static final String NAME = "RA";
056
057   /**
058    * Constructor.
059    */
060   public RequestAttributeVar() {
061      super(NAME);
062   }
063
064   @Override /* Var */
065   protected boolean allowNested() {
066      return false;
067   }
068
069   @Override /* Var */
070   protected boolean allowRecurse() {
071      return false;
072   }
073
074   @Override /* Var */
075   public String resolve(VarResolverSession session, String key) {
076      RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
077      return req.getAttribute(key).asString().orElse(null);
078   }
079
080   @Override /* Var */
081   public boolean canResolve(VarResolverSession session) {
082      return session.getBean(RestRequest.class).isPresent();
083   }
084}