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.util.*; 016 017import org.apache.juneau.common.internal.*; 018import org.apache.juneau.dto.swagger.*; 019import org.apache.juneau.json.*; 020import org.apache.juneau.rest.*; 021import org.apache.juneau.http.response.*; 022import org.apache.juneau.serializer.*; 023import org.apache.juneau.svl.*; 024 025/** 026 * Rest info variable resolver. 027 * 028 * <p> 029 * The format for this var is <js>"$RS{key1[,key2...]}"</js>. 030 * 031 * <p> 032 * Used to resolve values returned by {@link RestRequest#getSwagger()}.. 033 * <br>When multiple keys are used, returns the first non-null/empty value. 034 * 035 * <p> 036 * The possible values are: 037 * <ul> 038 * <li><js>"contact"</js> - Value returned by {@link Info#getContact()} 039 * <li><js>"description"</js> - Value returned by {@link Info#getDescription()} 040 * <li><js>"externalDocs"</js> - Value returned by {@link Swagger#getExternalDocs()} 041 * <li><js>"license"</js> - Value returned by {@link Info#getLicense()} 042 * <li><js>"operationDescription"</js> - Value returned by {@link Operation#getDescription()} 043 * <li><js>"operationSummary"</js> - Value returned by {@link Operation#getSummary()} 044 * <li><js>"siteName"</js> - Value returned by {@link Info#getSiteName()} 045 * <li><js>"tags"</js> - Value returned by {@link Swagger#getTags()} 046 * <li><js>"termsOfService"</js> - Value returned by {@link Info#getTermsOfService()} 047 * <li><js>"title"</js> - See {@link Info#getTitle()} 048 * <li><js>"version"</js> - See {@link Info#getVersion()} 049 * </ul> 050 * 051 * <h5 class='section'>Example:</h5> 052 * <p class='bjava'> 053 * String <jv>title</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RS{title}"</js>); 054 * String <jv>titleOrDescription</jv> = <jv>restRequest</jv>.getVarResolver().resolve(<js>"$RS{title,description}"</js>); 055 * </p> 056 * 057 * <h5 class='section'>Notes:</h5><ul> 058 * <li class='note'> 059 * This variable resolver requires that a {@link RestRequest} bean be available in the session bean store. 060 * <li class='note'> 061 * For security reasons, nested and recursive variables are not resolved. 062 * </ul> 063 * 064 * <h5 class='section'>See Also:</h5><ul> 065 * <li class='link'><a class="doclink" href="../../../../../index.html#jm.SvlVariables">SVL Variables</a> 066 * </ul> 067 */ 068public class RequestSwaggerVar extends MultipartResolvingVar { 069 070 /** The name of this variable. */ 071 public static final String NAME = "RS"; 072 073 /** 074 * Constructor. 075 */ 076 public RequestSwaggerVar() { 077 super(NAME); 078 } 079 080 @Override /* Var */ 081 protected boolean allowNested() { 082 return false; 083 } 084 085 @Override /* Var */ 086 protected boolean allowRecurse() { 087 return false; 088 } 089 090 @Override /* Var */ 091 public String resolve(VarResolverSession session, String key) throws BasicHttpException, InternalServerError { 092 try { 093 RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new); 094 Optional<Swagger> swagger = req.getSwagger(); 095 WriterSerializer s = Json5Serializer.DEFAULT; 096 Optional<Operation> methodSwagger = req.getOperationSwagger(); 097 char c = StringUtils.charAt(key, 0); 098 if (c == 'c') { 099 if ("contact".equals(key)) 100 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getContact()).map(StringUtils::stringify).orElse(null); 101 } else if (c == 'd') { 102 if ("description".equals(key)) 103 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getDescription()).orElse(null); 104 } else if (c == 'e') { 105 if ("externalDocs".equals(key)) 106 return swagger.map(Swagger::getExternalDocs).map(ExternalDocumentation::toString).orElse(null); 107 } else if (c == 'l') { 108 if ("license".equals(key)) 109 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getLicense()).map(StringUtils::stringify).orElse(null); 110 } else if (c == 'o') { 111 if ("operationDescription".equals(key)) 112 return methodSwagger.map(Operation::getDescription).orElse(null); 113 if ("operationSummary".equals(key)) 114 return methodSwagger.map(Operation::getSummary).orElse(null); 115 } else if (c == 'r') { 116 if ("siteName".equals(key)) 117 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getSiteName()).orElse(null); 118 } else if (c == 't') { 119 if ("tags".equals(key)) 120 return swagger.map(Swagger::getTags).map(x -> s.toString(x)).orElse(null); 121 if ("termsOfService".equals(key)) 122 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getTermsOfService()).orElse(null); 123 if ("title".equals(key)) 124 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getTitle()).orElse(null); 125 } else if (c == 'v') { 126 if ("version".equals(key)) 127 return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getVersion()).orElse(null); 128 } 129 return null; 130 } catch (Exception e) { 131 throw new InternalServerError(e); 132 } 133 } 134 135 @Override /* Var */ 136 public boolean canResolve(VarResolverSession session) { 137 return session.getBean(RestRequest.class).isPresent(); 138 } 139}