![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.rest.vars.RequestSwaggerVar Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.rest.vars;
import java.util.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.dto.swagger.*;
import org.apache.juneau.json.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.http.response.*;
import org.apache.juneau.serializer.*;
import org.apache.juneau.svl.*;
/**
* Rest info variable resolver.
*
*
* The format for this var is "$RS{key1[,key2...]}" .
*
*
* Used to resolve values returned by {@link RestRequest#getSwagger()}..
*
When multiple keys are used, returns the first non-null/empty value.
*
*
* The possible values are:
*
* "contact" - Value returned by {@link Info#getContact()}
* "description" - Value returned by {@link Info#getDescription()}
* "externalDocs" - Value returned by {@link Swagger#getExternalDocs()}
* "license" - Value returned by {@link Info#getLicense()}
* "operationDescription" - Value returned by {@link Operation#getDescription()}
* "operationSummary" - Value returned by {@link Operation#getSummary()}
* "siteName" - Value returned by {@link Info#getSiteName()}
* "tags" - Value returned by {@link Swagger#getTags()}
* "termsOfService" - Value returned by {@link Info#getTermsOfService()}
* "title" - See {@link Info#getTitle()}
* "version" - See {@link Info#getVersion()}
*
*
* Example:
*
* String title = restRequest .getVarResolver().resolve("$RS{title}" );
* String titleOrDescription = restRequest .getVarResolver().resolve("$RS{title,description}" );
*
*
* Notes:
* -
* This variable resolver requires that a {@link RestRequest} bean be available in the session bean store.
*
-
* For security reasons, nested and recursive variables are not resolved.
*
*
* See Also:
* - SVL Variables
*
*/
public class RequestSwaggerVar extends MultipartResolvingVar {
/** The name of this variable. */
public static final String NAME = "RS";
/**
* Constructor.
*/
public RequestSwaggerVar() {
super(NAME);
}
@Override /* Var */
protected boolean allowNested() {
return false;
}
@Override /* Var */
protected boolean allowRecurse() {
return false;
}
@Override /* Var */
public String resolve(VarResolverSession session, String key) throws BasicHttpException, InternalServerError {
try {
RestRequest req = session.getBean(RestRequest.class).orElseThrow(InternalServerError::new);
Optional swagger = req.getSwagger();
WriterSerializer s = Json5Serializer.DEFAULT;
Optional methodSwagger = req.getOperationSwagger();
char c = StringUtils.charAt(key, 0);
if (c == 'c') {
if ("contact".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getContact()).map(StringUtils::stringify).orElse(null);
} else if (c == 'd') {
if ("description".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getDescription()).orElse(null);
} else if (c == 'e') {
if ("externalDocs".equals(key))
return swagger.map(Swagger::getExternalDocs).map(ExternalDocumentation::toString).orElse(null);
} else if (c == 'l') {
if ("license".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getLicense()).map(StringUtils::stringify).orElse(null);
} else if (c == 'o') {
if ("operationDescription".equals(key))
return methodSwagger.map(Operation::getDescription).orElse(null);
if ("operationSummary".equals(key))
return methodSwagger.map(Operation::getSummary).orElse(null);
} else if (c == 'r') {
if ("siteName".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getSiteName()).orElse(null);
} else if (c == 't') {
if ("tags".equals(key))
return swagger.map(Swagger::getTags).map(x -> s.toString(x)).orElse(null);
if ("termsOfService".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getTermsOfService()).orElse(null);
if ("title".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getTitle()).orElse(null);
} else if (c == 'v') {
if ("version".equals(key))
return swagger.map(Swagger::getInfo).map(x -> x == null ? null : x.getVersion()).orElse(null);
}
return null;
} catch (Exception e) {
throw new InternalServerError(e);
}
}
@Override /* Var */
public boolean canResolve(VarResolverSession session) {
return session.getBean(RestRequest.class).isPresent();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy