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 * <h5 class='section'>Notes:</h5> 059 * <ul class='spaced-list'> 060 * <li> 061 * This variable resolver requires that a {@link RestRequest} object be set as a context object on the resolver 062 * or a session object on the resolver session. 063 * <li> 064 * For security reasons, nested and recursive variables are not resolved. 065 * </ul> 066 * 067 * <h5 class='section'>See Also:</h5> 068 * <ul> 069 * <li class='link'>{@doc juneau-rest-server.SvlVariables} 070 * </ul> 071 */ 072public class RestInfoVar extends MultipartResolvingVar { 073 074 /** 075 * The name of the session or context object that identifies the {@link RestRequest} object. 076 */ 077 public static final String SESSION_req = "req"; 078 079 080 /** The name of this variable. */ 081 public static final String NAME = "RI"; 082 083 /** 084 * Constructor. 085 */ 086 public RestInfoVar() { 087 super(NAME); 088 } 089 090 @Override /* Var */ 091 protected boolean allowNested() { 092 return false; 093 } 094 095 @Override /* Var */ 096 protected boolean allowRecurse() { 097 return false; 098 } 099 100 @Override /* Parameter */ 101 public String resolve(VarResolverSession session, String key) throws RestException, InternalServerError { 102 try { 103 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req, true); 104 Swagger swagger = req.getSwagger(); 105 RestInfoProvider rip = req.getInfoProvider(); 106 WriterSerializer s = SimpleJsonSerializer.DEFAULT; 107 char c = StringUtils.charAt(key, 0); 108 if (c == 'c') { 109 if ("contact".equals(key)) { 110 Contact x = swagger.getInfo().getContact(); 111 return x == null ? null : s.toString(x); 112 } 113 } else if (c == 'd') { 114 if ("description".equals(key)) 115 return rip.getDescription(req); 116 } else if (c == 'e') { 117 if ("externalDocs".equals(key)) { 118 ExternalDocumentation x = swagger.getExternalDocs(); 119 return x == null ? null : s.toString(x); 120 } 121 } else if (c == 'l') { 122 if ("license".equals(key)) { 123 License x = swagger.getInfo().getLicense(); 124 return x == null ? null : s.toString(x); 125 } 126 } else if (c == 'm') { 127 if ("methodDescription".equals(key)) 128 return rip.getMethodDescription(req.getJavaMethod(), req); 129 if ("methodSummary".equals(key)) 130 return rip.getMethodSummary(req.getJavaMethod(), req); 131 } else if (c == 's') { 132 if ("siteName".equals(key)) 133 return rip.getSiteName(req); 134 } else if (c == 't') { 135 if ("tags".equals(key)) { 136 List<Tag> x = swagger.getTags(); 137 return x == null ? null : s.toString(x); 138 } else if ("termsOfService".equals(key)) { 139 return swagger.getInfo().getTermsOfService(); 140 } else if ("title".equals(key)) { 141 return swagger.getInfo().getTitle(); 142 } 143 } else if (c == 'v') { 144 if ("version".equals(key)) 145 return swagger.getInfo().getVersion(); 146 } 147 return null; 148 } catch (RestException e) { 149 throw e; 150 } catch (Exception e) { 151 throw new InternalServerError(e); 152 } 153 } 154}