
io.micronaut.openapi.visitor.EndpointsConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-openapi Show documentation
Show all versions of micronaut-openapi Show documentation
Configuration to integrate Micronaut and OpenAPI/Swagger
The newest version!
/*
* Copyright 2017-2020 original 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
*
* https://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 io.micronaut.openapi.visitor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.inject.visitor.VisitorContext;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.tags.Tag;
/**
* Endpoints configuration.
*
* @author croudet
*/
class EndpointsConfiguration {
private static final String ENDPOINTS_PREFIX = "endpoints.";
public static final String ENDPOINTS_ENABLED = ENDPOINTS_PREFIX + "enabled";
public static final String ENDPOINTS_TAGS = ENDPOINTS_PREFIX + "tags";
public static final String ENDPOINTS_PATH = ENDPOINTS_PREFIX + "path";
public static final String ENDPOINTS_SERVERS = ENDPOINTS_PREFIX + "servers";
public static final String ENDPOINTS_SECURITY_REQUIREMENTS = ENDPOINTS_PREFIX + "security-requirements";
private final boolean enabled;
private String path;
private List tags;
private List servers;
private List securityRequirements;
private Map endpoints;
/**
* List of Endpoints to process.
*
* @param context The VisitorContext.
* @param properties The properties to process.
*/
EndpointsConfiguration(VisitorContext context, Properties properties) {
enabled = Boolean.parseBoolean(properties.getProperty(ENDPOINTS_ENABLED, Boolean.FALSE.toString()));
if (!enabled) {
return;
}
path = properties.getProperty(ENDPOINTS_PATH, "");
tags = parseTags(properties.getProperty(ENDPOINTS_TAGS, "").split(","));
servers = parseServers(properties.getProperty(ENDPOINTS_SERVERS, ""), context);
securityRequirements = parseSecurityRequirements(properties.getProperty(ENDPOINTS_SECURITY_REQUIREMENTS, ""), context);
endpoints = new LinkedHashMap<>();
Map map = new HashMap<>(properties.size());
properties.forEach((key, value) -> map.put((String) key, (String) value));
map.entrySet().stream()
.filter(EndpointsConfiguration::validEntry)
.forEach(entry -> {
int idx = entry.getKey().lastIndexOf('.');
if (idx <= 0 || idx == entry.getKey().length() || entry.getValue() == null) {
return;
}
String entryType = entry.getKey().substring(idx + 1);
String name = entry.getKey().substring(ENDPOINTS_PREFIX.length(), idx);
if ("security-requirements".equals(entryType)) {
Endpoint endpoint = endpoints.computeIfAbsent(name, key -> new Endpoint());
endpoint.setSecurityRequirements(parseSecurityRequirements(entry.getValue(), context));
} else if ("servers".equals(entryType)) {
Endpoint endpoint = endpoints.computeIfAbsent(name, key -> new Endpoint());
endpoint.setServers(parseServers(entry.getValue(), context));
} else if ("tags".equals(entryType)) {
Endpoint endpoint = endpoints.computeIfAbsent(name, key -> new Endpoint());
endpoint.setTags(parseTags(entry.getValue().split(",")));
} else if ("class".equals(entryType)) {
Endpoint endpoint = endpoints.computeIfAbsent(name, key -> new Endpoint());
endpoint.setClassElement(context.getClassElement(entry.getValue()));
} else {
return;
}
});
}
/**
* Returns the base path for all Endpoints.
* @return A path.
*/
String getPath() {
return path;
}
/**
* Returns true if processing of Endpoints is enabled.
* @return true if processing of Endpoints is enabled.
*/
boolean isEnabled() {
return enabled;
}
/**
* The list of global tags to add to all Endpoints.
* @return the list of global tags to add to all Endpoints.
*/
List getTags() {
return tags;
}
/**
* The Endpoints to process.
* @return The Endpoints to process.
*/
Map getEndpoints() {
return endpoints;
}
/**
* The list of global servers to add to all Endpoints.
* @return the list of global servers to add to all Endpoints.
*/
List getServers() {
return servers;
}
/**
* The list of global security requirements to add to all Endpoints.
* @return the list of global security requirements to add to all Endpoints.
*/
List getSecurityRequirements() {
return securityRequirements;
}
@Override
public String toString() {
return "EndpointsConfiguration [enabled=" + enabled + ", path=" + path + ", tags=" + tags + ", endpoints="
+ endpoints + "]";
}
private static List parseServers(String servers, VisitorContext context) {
return parseModel(servers, context, new TypeReference>() { });
}
private static List parseSecurityRequirements(String securityRequirements, VisitorContext context) {
return parseModel(securityRequirements, context, new TypeReference>() { });
}
private static List parseModel(String s, VisitorContext context, TypeReference> typeReference) {
if (s == null || s.isEmpty() || (! s.startsWith("[") && ! s.endsWith("]"))) {
return Collections.emptyList();
}
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(s, typeReference);
} catch (JsonProcessingException e) {
context.warn("Fail to parse " + typeReference.getType().toString() + ": " + s + " - " + e.getMessage(), null);
}
return Collections.emptyList();
}
private static boolean validEntry(Map.Entry entry) {
return entry.getKey().startsWith(ENDPOINTS_PREFIX)
&& !entry.getKey().equals(ENDPOINTS_ENABLED)
&& !entry.getKey().equals(ENDPOINTS_TAGS)
&& !entry.getKey().equals(ENDPOINTS_SERVERS)
&& !entry.getKey().equals(ENDPOINTS_SECURITY_REQUIREMENTS);
}
private static List parseTags(String... stringTags) {
if (stringTags.length == 0) {
return Collections.emptyList();
}
List tags = new ArrayList<>(stringTags.length);
for (String name : stringTags) {
if (name == null || name.isEmpty()) {
continue;
}
Tag tag = new Tag();
tag.setName(name);
tags.add(tag);
}
return tags;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy