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 static org.apache.juneau.rest.HttpRuntimeException.*;
016
017import java.lang.reflect.*;
018import java.util.*;
019
020import org.apache.juneau.dto.swagger.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.json.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.http.exception.*;
025import org.apache.juneau.serializer.*;
026import org.apache.juneau.svl.*;
027
028/**
029 * Rest info variable resolver.
030 *
031 * <p>
032 * The format for this var is <js>"$RI{key1[,key2...]}"</js>.
033 *
034 * <p>
035 * Used to resolve values returned by {@link RestRequest#getInfoProvider()}..
036 * <br>When multiple keys are used, returns the first non-null/empty value.
037 *
038 * <p>
039 * The possible values are:
040 * <ul>
041 *    <li><js>"contact"</js> - Value returned by {@link Info#getContact()}
042 *    <li><js>"description"</js> - Value returned by {@link RestInfoProvider#getDescription(RestRequest)}
043 *    <li><js>"externalDocs"</js> - Value returned by {@link Swagger#getExternalDocs()}
044 *    <li><js>"license"</js> - Value returned by {@link Info#getLicense()}
045 *    <li><js>"methodDescription"</js> - Value returned by {@link RestInfoProvider#getMethodDescription(Method,RestRequest)}
046 *    <li><js>"methodSummary"</js> - Value returned by {@link RestInfoProvider#getMethodSummary(Method,RestRequest)}
047 *    <li><js>"siteName"</js> - Value returned by {@link RestInfoProvider#getSiteName(RestRequest)}
048 *    <li><js>"tags"</js> - Value returned by {@link Swagger#getTags()}
049 *    <li><js>"termsOfService"</js> - Value returned by {@link Info#getTermsOfService()}
050 *    <li><js>"title"</js> - See {@link RestInfoProvider#getTitle(RestRequest)}
051 *    <li><js>"version"</js> - See {@link Info#getVersion()}
052 * </ul>
053 *
054 * <h5 class='section'>Example:</h5>
055 * <p class='bcode w800'>
056 *    String title = restRequest.resolveVars(<js>"$RI{title}"</js>);
057 *    String titleOrDescription = restRequest.resolveVars(<js>"$RI{title,description}"</js>);
058 * </p>
059 *
060 * <ul class='notes'>
061 *    <li>
062 *       This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver
063 *       or a session object on the resolver session.
064 *    <li>
065 *       For security reasons, nested and recursive variables are not resolved.
066 * </ul>
067 *
068 * <ul class='seealso'>
069 *    <li class='link'>{@doc RestSvlVariables}
070 * </ul>
071 */
072public class RestInfoVar extends MultipartResolvingVar {
073
074   private static final String SESSION_req = "req";
075
076   /** The name of this variable. */
077   public static final String NAME = "RI";
078
079   /**
080    * Constructor.
081    */
082   public RestInfoVar() {
083      super(NAME);
084   }
085
086   @Override /* Var */
087   protected boolean allowNested() {
088      return false;
089   }
090
091   @Override /* Var */
092   protected boolean allowRecurse() {
093      return false;
094   }
095
096   @Override /* Var */
097   public String resolve(VarResolverSession session, String key) throws HttpException, InternalServerError {
098      try {
099         RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true);
100         Swagger swagger = req.getSwagger();
101         RestInfoProvider rip = req.getInfoProvider();
102         WriterSerializer s = SimpleJsonSerializer.DEFAULT;
103         char c = StringUtils.charAt(key, 0);
104         if (c == 'c') {
105            if ("contact".equals(key)) {
106               Contact x = swagger.getInfo().getContact();
107               return x == null ? null : s.toString(x);
108            }
109         } else if (c == 'd') {
110            if ("description".equals(key))
111               return rip.getDescription(req);
112         } else if (c == 'e') {
113            if ("externalDocs".equals(key)) {
114               ExternalDocumentation x = swagger.getExternalDocs();
115               return x == null ? null : s.toString(x);
116            }
117         } else if (c == 'l') {
118            if ("license".equals(key)) {
119               License x = swagger.getInfo().getLicense();
120               return x == null ? null : s.toString(x);
121            }
122         } else if (c == 'm') {
123            if ("methodDescription".equals(key))
124               return rip.getMethodDescription(req.getJavaMethod(), req);
125            if ("methodSummary".equals(key))
126               return rip.getMethodSummary(req.getJavaMethod(), req);
127         } else if (c == 's') {
128            if ("siteName".equals(key))
129               return rip.getSiteName(req);
130         } else if (c == 't') {
131            if ("tags".equals(key)) {
132               List<Tag> x = swagger.getTags();
133               return x == null ? null : s.toString(x);
134            } else if ("termsOfService".equals(key)) {
135               return swagger.getInfo().getTermsOfService();
136            } else if ("title".equals(key)) {
137               return swagger.getInfo().getTitle();
138            }
139         } else if (c == 'v') {
140            if ("version".equals(key))
141               return swagger.getInfo().getVersion();
142         }
143         return null;
144      } catch (Exception e) {
145         throw toHttpException(e, InternalServerError.class);
146      }
147   }
148
149   @Override /* Var */
150   public boolean canResolve(VarResolverSession session) {
151      return session.hasSessionObject(SESSION_req);
152   }
153}