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