All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.lastaflute.doc.SwaggerOption Maven / Gradle / Ivy
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed 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.lastaflute.doc;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import org.dbflute.optional.OptionalThing;
import org.dbflute.util.DfCollectionUtil;
/**
* @author p1us2er0
* @author jflute
*/
public class SwaggerOption {
// ===================================================================================
// Attribute
// =========
protected Function basePathLambda;
protected List> headerParameterList;
protected List> securityDefinitionList;
// ===================================================================================
// Basic
// =====
/**
* Derive application base path (e.g. /showbase/) by filter.
*
* op.derivedBasePath(basePath -> basePath + "api/");
*
* @param oneArgLambda The callback of base path filter. (NotNull)
*/
public void derivedBasePath(Function oneArgLambda) {
this.basePathLambda = oneArgLambda;
}
// ===================================================================================
// Header Parameter
// ================
public void addHeaderParameter(String name, String value) {
if (headerParameterList == null) {
headerParameterList = DfCollectionUtil.newArrayList();
}
headerParameterList.add(createHeaderParameterMap(name, value));
}
public void addHeaderParameter(String name, String value, Consumer resourceLambda) {
final Map parameterMap = createHeaderParameterMap(name, value);
resourceLambda.accept(new SwaggerHeaderParameterResource(parameterMap));
if (headerParameterList == null) {
headerParameterList = DfCollectionUtil.newArrayList();
}
headerParameterList.add(parameterMap);
}
protected Map createHeaderParameterMap(String name, String value) {
final Map parameterMap = DfCollectionUtil.newLinkedHashMap();
parameterMap.put("in", "header");
parameterMap.put("type", "string");
parameterMap.put("required", true);
parameterMap.put("name", name);
parameterMap.put("default", value);
return parameterMap;
}
public static class SwaggerHeaderParameterResource {
protected final Map headerParameterMap;
public SwaggerHeaderParameterResource(Map headerParameterMap) {
this.headerParameterMap = headerParameterMap;
}
public void registerAttribute(String key, Object value) {
if (key == null) {
throw new IllegalArgumentException("The argument 'key' should not be null.");
}
if (value == null) {
throw new IllegalArgumentException("The argument 'value' should not be null.");
}
if (key.equalsIgnoreCase("name") || key.equalsIgnoreCase("default")) {
throw new IllegalArgumentException("Cannot add '" + key + "' key here: " + key + ", " + value);
}
headerParameterMap.put(key, value);
}
}
// ===================================================================================
// Security Definition
// ===================
public void addSecurityDefinition(String name) {
if (securityDefinitionList == null) {
securityDefinitionList = DfCollectionUtil.newArrayList();
}
securityDefinitionList.add(createSecurityDefinitionMap(name));
}
public void addSecurityDefinition(String name, Consumer resourceLambda) {
final Map definitionMap = createSecurityDefinitionMap(name);
resourceLambda.accept(new SwaggerSecurityDefinitionResource(definitionMap));
if (securityDefinitionList == null) {
securityDefinitionList = DfCollectionUtil.newArrayList();
}
securityDefinitionList.add(definitionMap);
}
protected Map createSecurityDefinitionMap(String name) {
final Map definitionMap = DfCollectionUtil.newLinkedHashMap();
definitionMap.put("in", "header");
definitionMap.put("type", "apiKey");
definitionMap.put("name", name);
return definitionMap;
}
public static class SwaggerSecurityDefinitionResource {
protected final Map securityDefinitionMap;
public SwaggerSecurityDefinitionResource(Map securityDefinitionMap) {
this.securityDefinitionMap = securityDefinitionMap;
}
public void registerAttribute(String key, Object value) {
if (key == null) {
throw new IllegalArgumentException("The argument 'key' should not be null.");
}
if (value == null) {
throw new IllegalArgumentException("The argument 'value' should not be null.");
}
if (key.equalsIgnoreCase("name")) {
throw new IllegalArgumentException("Cannot add '" + key + "' key here: " + key + ", " + value);
}
securityDefinitionMap.put(key, value);
}
}
// ===================================================================================
// Accessor
// ========
public OptionalThing> getDerivedBasePath() {
return OptionalThing.ofNullable(basePathLambda, () -> {
throw new IllegalStateException("Not set derivedBasePathLamda.");
});
}
public OptionalThing>> getHeaderParameterList() {
return OptionalThing.ofNullable(headerParameterList, () -> {
throw new IllegalStateException("Not set headerParameterList.");
});
}
public OptionalThing>> getSecurityDefinitionList() {
return OptionalThing.ofNullable(securityDefinitionList, () -> {
throw new IllegalStateException("Not set securityDefinitionList.");
});
}
}