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.*;
017import org.apache.juneau.utils.*;
018
019/**
020 * Swagger attribute variable resolver.
021 *
022 * <p>
023 * The format for this var is <js>"$SS{key1[,key2...]}"</js>.
024 * <br>When multiple keys are used, returns the first non-null/empty value.
025 *
026 * <p>
027 * Example values:
028 * <ul>
029 *    <li><js>"info/title"</js>
030 *    <li><js>"info/description"</js>
031 *    <li><js>"info/contact/name"</js>
032 * </ul>
033 *
034 * <ul class='notes'>
035 *    <li>
036 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
037 *       or a session object on the resolver session.
038 *    <li>
039 *       For security reasons, nested and recursive variables are not resolved.
040 * </ul>
041 *
042 * <ul class='seealso'>
043 *    <li class='link'>{@doc juneau-rest-server.SvlVariables}
044 * </ul>
045 */
046public class SwaggerVar extends MultipartResolvingVar {
047
048   private static final String SESSION_req = "req";
049
050   /** The name of this variable. */
051   public static final String NAME = "SS";
052
053   /**
054    * Constructor.
055    */
056   public SwaggerVar() {
057      super(NAME);
058   }
059
060   @Override /* Var */
061   protected boolean allowNested() {
062      return false;
063   }
064
065   @Override /* Var */
066   protected boolean allowRecurse() {
067      return false;
068   }
069
070   @Override /* Var */
071   public String resolve(VarResolverSession session, String key) {
072      RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
073      return new PojoRest(req.getSwagger()).getString(key);
074   }
075
076   @Override /* Var */
077   public boolean canResolve(VarResolverSession session) {
078      return session.hasSessionObject(SESSION_req);
079   }
080}