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.
io.swagger.v3.jaxrs2.util.ReaderUtils Maven / Gradle / Ivy
package io.swagger.v3.jaxrs2.util;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.core.util.ParameterProcessor;
import io.swagger.v3.core.util.ReflectionUtils;
import io.swagger.v3.jaxrs2.ext.OpenAPIExtension;
import io.swagger.v3.jaxrs2.ext.OpenAPIExtensions;
import io.swagger.v3.oas.integration.api.OpenAPIConfiguration;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.commons.lang3.StringUtils;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.Context;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
public class ReaderUtils {
private static final String GET_METHOD = "get";
private static final String POST_METHOD = "post";
private static final String PUT_METHOD = "put";
private static final String DELETE_METHOD = "delete";
private static final String HEAD_METHOD = "head";
private static final String OPTIONS_METHOD = "options";
private static final String PATH_DELIMITER = "/";
/**
* Collects constructor-level parameters from class.
*
* @param cls is a class for collecting
* @param components
* @return the collection of supported parameters
*/
public static List collectConstructorParameters(Class> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) {
if (cls.isLocalClass() || (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()))) {
return Collections.emptyList();
}
List selected = Collections.emptyList();
int maxParamsCount = 0;
for (Constructor> constructor : cls.getDeclaredConstructors()) {
if (!ReflectionUtils.isConstructorCompatible(constructor)
&& !ReflectionUtils.isInject(Arrays.asList(constructor.getDeclaredAnnotations()))) {
continue;
}
final Type[] genericParameterTypes = constructor.getGenericParameterTypes();
final Annotation[][] annotations = constructor.getParameterAnnotations();
int paramsCount = 0;
final List parameters = new ArrayList<>();
for (int i = 0; i < genericParameterTypes.length; i++) {
final List tmpAnnotations = Arrays.asList(annotations[i]);
if (isContext(tmpAnnotations)) {
paramsCount++;
} else {
final Type genericParameterType = genericParameterTypes[i];
final List tmpParameters = collectParameters(genericParameterType, tmpAnnotations, components, classConsumes, jsonViewAnnotation);
if (! tmpParameters.isEmpty()) {
for (Parameter tmpParameter : tmpParameters) {
Parameter processedParameter = ParameterProcessor.applyAnnotations(
tmpParameter,
genericParameterType,
tmpAnnotations,
components,
classConsumes == null ? new String[0] : classConsumes.value(),
null,
jsonViewAnnotation);
if (processedParameter != null) {
parameters.add(processedParameter);
}
}
paramsCount++;
}
}
}
if (paramsCount >= maxParamsCount) {
maxParamsCount = paramsCount;
selected = parameters;
}
}
return selected;
}
/**
* Collects field-level parameters from class.
*
* @param cls is a class for collecting
* @param components
* @return the collection of supported parameters
*/
public static List collectFieldParameters(Class> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) {
final List parameters = new ArrayList<>();
for (Field field : ReflectionUtils.getDeclaredFields(cls)) {
final List annotations = Arrays.asList(field.getAnnotations());
final Type genericType = field.getGenericType();
parameters.addAll(collectParameters(genericType, annotations, components, classConsumes, jsonViewAnnotation));
}
return parameters;
}
private static List collectParameters(Type type, List annotations, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) {
final Iterator chain = OpenAPIExtensions.chain();
return chain.hasNext() ? chain.next().extractParameters(annotations, type, new HashSet<>(), components, classConsumes, null, false, jsonViewAnnotation, chain).parameters :
Collections.emptyList();
}
private static boolean isContext(List annotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof Context) {
return true;
}
}
return false;
}
public static Optional> getStringListFromStringArray(String[] array) {
if (array == null) {
return Optional.empty();
}
List list = new ArrayList<>();
boolean isEmpty = true;
for (String value : array) {
if (StringUtils.isNotBlank(value)) {
isEmpty = false;
}
list.add(value);
}
if (isEmpty) {
return Optional.empty();
}
return Optional.of(list);
}
public static boolean isIgnored(String path, OpenAPIConfiguration config) {
if (config.getIgnoredRoutes() == null) {
return false;
}
for (String item : config.getIgnoredRoutes()) {
final int length = item.length();
if (path.startsWith(item) && (path.length() == length || path.startsWith(PATH_DELIMITER, length))) {
return true;
}
}
return false;
}
public static String getPath(javax.ws.rs.Path classLevelPath, javax.ws.rs.Path methodLevelPath, String parentPath, boolean isSubresource) {
if (classLevelPath == null && methodLevelPath == null && StringUtils.isEmpty(parentPath)) {
return null;
}
StringBuilder b = new StringBuilder();
appendPathComponent(parentPath, b);
if (classLevelPath != null && !isSubresource) {
appendPathComponent(classLevelPath.value(), b);
}
if (methodLevelPath != null) {
appendPathComponent(methodLevelPath.value(), b);
}
return b.length() == 0 ? "/" : b.toString();
}
/**
* appends a path component string to a StringBuilder
* guarantees:
*
* nulls, empty strings and "/" are nops
* output will always start with "/" and never end with "/"
*
* @param component component to be added
* @param to output
*/
private static void appendPathComponent(String component, StringBuilder to) {
if (component == null || component.isEmpty() || "/".equals(component)) {
return;
}
if (!component.startsWith("/") && (to.length() == 0 || '/' != to.charAt(to.length() - 1))) {
to.append("/");
}
if (component.endsWith("/")) {
to.append(component, 0, component.length() - 1);
} else {
to.append(component);
}
}
public static String extractOperationMethod(Method method, Iterator chain) {
if (method.getAnnotation(javax.ws.rs.GET.class) != null) {
return GET_METHOD;
} else if (method.getAnnotation(javax.ws.rs.PUT.class) != null) {
return PUT_METHOD;
} else if (method.getAnnotation(javax.ws.rs.POST.class) != null) {
return POST_METHOD;
} else if (method.getAnnotation(javax.ws.rs.DELETE.class) != null) {
return DELETE_METHOD;
} else if (method.getAnnotation(javax.ws.rs.OPTIONS.class) != null) {
return OPTIONS_METHOD;
} else if (method.getAnnotation(javax.ws.rs.HEAD.class) != null) {
return HEAD_METHOD;
} else if (method.getAnnotation(HttpMethod.class) != null) {
HttpMethod httpMethod = method.getAnnotation(HttpMethod.class);
return httpMethod.value().toLowerCase();
} else if (!StringUtils.isEmpty(getHttpMethodFromCustomAnnotations(method))) {
return getHttpMethodFromCustomAnnotations(method);
} else if ((ReflectionUtils.getOverriddenMethod(method)) != null) {
return extractOperationMethod(ReflectionUtils.getOverriddenMethod(method), chain);
} else if (chain != null && chain.hasNext()) {
return chain.next().extractOperationMethod(method, chain);
} else {
return null;
}
}
public static String getHttpMethodFromCustomAnnotations(Method method) {
for (Annotation methodAnnotation : method.getAnnotations()) {
HttpMethod httpMethod = methodAnnotation.annotationType().getAnnotation(HttpMethod.class);
if (httpMethod != null) {
return httpMethod.value().toLowerCase();
}
}
return null;
}
}