ro.pippo.controller.util.ControllerUtils Maven / Gradle / Ivy
/*
* Copyright (C) 2016 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 ro.pippo.controller.util;
import ro.pippo.controller.Consumes;
import ro.pippo.controller.Interceptor;
import ro.pippo.controller.Produces;
import ro.pippo.controller.extractor.Param;
import ro.pippo.core.route.RouteHandler;
import ro.pippo.core.util.StringUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author James Moger
*/
public class ControllerUtils {
public static List> collectRouteInterceptors(Method method) {
return ClassUtils.collectNestedAnnotation(method, Interceptor.class).stream()
.map(Interceptor::value)
.collect(Collectors.toList());
}
// TODO one controllerMethod that takes annotation type as parameter
public static List getConsumes(Method method) {
Set types = new LinkedHashSet<>();
Consumes consumes = ClassUtils.getAnnotation(method, Consumes.class);
if (consumes != null) {
for (String value : consumes.value()) {
types.add(value.trim());
}
}
return new ArrayList<>(types);
}
// TODO one controllerMethod that takes annotation type as parameter
public static List getProduces(Method method) {
Set contentTypes = new LinkedHashSet<>();
Produces produces = ClassUtils.getAnnotation(method, Produces.class);
if (produces != null) {
for (String value : produces.value()) {
contentTypes.add(value.trim());
}
}
return new ArrayList<>(contentTypes);
}
public static Collection getSuffixes(Method method) {
Set suffixes = new LinkedHashSet<>();
for (String produces : getProduces(method)) {
int i = produces.lastIndexOf('/') + 1;
String type = StringUtils.removeStart(produces.substring(i).toLowerCase(), "x-");
suffixes.add(type);
}
return suffixes;
}
// TODO
/*
public static Collection getReturns(Method controllerMethod) {
Map returns = new TreeMap<>();
if (controllerMethod.getDeclaringClass().isAnnotationPresent(Returns.class)) {
for (Return aReturn : controllerMethod.getDeclaringClass().getAnnotation(Returns.class).value()) {
returns.put(aReturn.code(), aReturn);
}
}
if (controllerMethod.getDeclaringClass().isAnnotationPresent(Return.class)) {
Return aReturn = controllerMethod.getDeclaringClass().getAnnotation(Return.class);
returns.put(aReturn.code(), aReturn);
}
if (controllerMethod.isAnnotationPresent(Returns.class)) {
for (Return aReturn : controllerMethod.getAnnotation(Returns.class).value()) {
returns.put(aReturn.code(), aReturn);
}
}
if (controllerMethod.isAnnotationPresent(Return.class)) {
Return aReturn = controllerMethod.getAnnotation(Return.class);
returns.put(aReturn.code(), aReturn);
}
return returns.values();
}
*/
/**
* Returns the name of a parameter.
*
* @param parameter
* @return the name of a parameter.
*/
public static String getParameterName(Parameter parameter) {
// identify parameter name and pattern from controllerMethod signature
String methodParameterName = parameter.getName();
if (parameter.isAnnotationPresent(Param.class)) {
Param param = parameter.getAnnotation(Param.class);
if (!StringUtils.isNullOrEmpty(param.value())) {
methodParameterName = param.value();
}
}
return methodParameterName;
}
/**
* Removes trailing wildcards from a content type as long as the content type is not a
* universal wildcard content type like '*' or '*\*'.
*
* @param contentTypes
* @return the list of content types
*/
public static List cleanupFuzzyContentTypes(List contentTypes) {
if (contentTypes == null || contentTypes.isEmpty()) {
return contentTypes;
}
List types = new ArrayList<>();
for (String contentType : contentTypes) {
if (contentType.equals("*") || contentType.equals("*/*")) {
types.add(contentType);
continue;
}
int i = contentType.indexOf('*');
if (i > -1) {
types.add(contentType.substring(0, i));
} else {
types.add(contentType);
}
}
return types;
}
}