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 javax.servlet.http.*; 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.serializer.*; 025import org.apache.juneau.svl.*; 026 027/** 028 * Rest info variable resolver. 029 * 030 * <p> 031 * The format for this var is <js>"$RI{key1[,key2...]}"</js>. 032 * 033 * <p> 034 * Used to resolve values returned by {@link RestRequest#getInfoProvider()}.. 035 * <br>When multiple keys are used, returns the first non-null/empty value. 036 * 037 * <p> 038 * The possible values are: 039 * <ul> 040 * <li><js>"contact"</js> - Value returned by {@link Info#getContact()} 041 * <li><js>"description"</js> - Value returned by {@link RestInfoProvider#getDescription(RestRequest)} 042 * <li><js>"externalDocs"</js> - Value returned by {@link Swagger#getExternalDocs()} 043 * <li><js>"license"</js> - Value returned by {@link Info#getLicense()} 044 * <li><js>"methodDescription"</js> - Value returned by {@link RestInfoProvider#getMethodDescription(Method,RestRequest)} 045 * <li><js>"methodSummary"</js> - Value returned by {@link RestInfoProvider#getMethodSummary(Method,RestRequest)} 046 * <li><js>"siteName"</js> - Value returned by {@link RestInfoProvider#getSiteName(RestRequest)} 047 * <li><js>"tags"</js> - Value returned by {@link Swagger#getTags()} 048 * <li><js>"termsOfService"</js> - Value returned by {@link Info#getTermsOfService()} 049 * <li><js>"title"</js> - See {@link RestInfoProvider#getTitle(RestRequest)} 050 * <li><js>"version"</js> - See {@link Info#getVersion()} 051 * </ul> 052 * 053 * <h5 class='section'>Example:</h5> 054 * <p class='bcode'> 055 * String title = restRequest.resolveVars(<js>"$RI{title}"</js>); 056 * String titleOrDescription = restRequest.resolveVars(<js>"$RI{title,description}"</js>); 057 * </p> 058 * 059 * <h5 class='section'>Notes:</h5> 060 * <ul class='spaced-list'> 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 * <h5 class='section'>See Also:</h5> 069 * <ul> 070 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.SvlVariables">Overview > juneau-rest-server > SVL Variables</a> 071 * </ul> 072 */ 073public class RestInfoVar extends MultipartResolvingVar { 074 075 /** 076 * The name of the session or context object that identifies the {@link RestRequest} object. 077 */ 078 public static final String SESSION_req = "req"; 079 080 081 /** The name of this variable. */ 082 public static final String NAME = "RI"; 083 084 /** 085 * Constructor. 086 */ 087 public RestInfoVar() { 088 super(NAME); 089 } 090 091 @Override /* Var */ 092 protected boolean allowNested() { 093 return false; 094 } 095 096 @Override /* Var */ 097 protected boolean allowRecurse() { 098 return false; 099 } 100 101 @Override /* Parameter */ 102 public String resolve(VarResolverSession session, String key) { 103 try { 104 RestRequest req = session.getSessionObject(RestRequest.class, SESSION_req); 105 Swagger swagger = req.getSwagger(); 106 RestInfoProvider rip = req.getInfoProvider(); 107 WriterSerializer s = JsonSerializer.DEFAULT_LAX; 108 char c = StringUtils.charAt(key, 0); 109 if (c == 'c') { 110 if ("contact".equals(key)) { 111 Contact x = swagger.getInfo().getContact(); 112 return x == null ? null : s.toString(x); 113 } 114 } else if (c == 'd') { 115 if ("description".equals(key)) 116 return rip.getDescription(req); 117 } else if (c == 'e') { 118 if ("externalDocs".equals(key)) { 119 ExternalDocumentation x = swagger.getExternalDocs(); 120 return x == null ? null : s.toString(x); 121 } 122 } else if (c == 'l') { 123 if ("license".equals(key)) { 124 License x = swagger.getInfo().getLicense(); 125 return x == null ? null : s.toString(x); 126 } 127 } else if (c == 'm') { 128 if ("methodDescription".equals(key)) 129 return rip.getMethodDescription(req.getJavaMethod(), req); 130 if ("methodSummary".equals(key)) 131 return rip.getMethodSummary(req.getJavaMethod(), req); 132 } else if (c == 's') { 133 if ("siteName".equals(key)) 134 return rip.getSiteName(req); 135 } else if (c == 't') { 136 if ("tags".equals(key)) { 137 List<Tag> x = swagger.getTags(); 138 return x == null ? null : s.toString(x); 139 } else if ("termsOfService".equals(key)) { 140 return swagger.getInfo().getTermsOfService(); 141 } else if ("title".equals(key)) { 142 return swagger.getInfo().getTitle(); 143 } 144 } else if (c == 'v') { 145 if ("version".equals(key)) 146 return swagger.getInfo().getVersion(); 147 } 148 return null; 149 } catch (RestException e) { 150 throw e; 151 } catch (Exception e) { 152 throw new RestException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); 153 } 154 } 155}