/** * Validates that the declared content-types can actually be generated by Pippo. * * @param contentTypes */ protected void validateProduces(Collection<String> contentTypes) { Set<String> ignoreProduces = new TreeSet<>(); ignoreProduces.add(Produces.TEXT); ignoreProduces.add(Produces.HTML); ignoreProduces.add(Produces.XHTML); for (String produces : declaredProduces) { if (ignoreProduces.contains(produces)) { continue; } if (!contentTypes.contains(produces)) { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!", LangUtils.toString(controllerMethod), Produces.class.getSimpleName(), produces); } } }
if (consume.equals(declaredConsume)) { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!", LangUtils.toString(controllerMethod), Consumes.class.getSimpleName(), declaredConsume); } else { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for \"{}\"!", LangUtils.toString(controllerMethod), Consumes.class.getSimpleName(), declaredConsume, consume);
log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), Consumes.ALL); return true; log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), "*/*"); return true; if (declaredConsumes.contains(type)) { log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), type); return true; } else { String fuzzyType = declaredType.substring(0, fuzz); if (type.startsWith(fuzzyType)) { log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), type); return true; LangUtils.toString(controllerMethod), declaredConsumes); } else { log.warn("{} can not handle Request for '{}' because Route @Consumes '{}'", LangUtils.toString(controllerMethod), types, declaredConsumes);
/** * Sort the controller's methods by their preferred order, if specified. * * @param controllerMethods * @return a sorted list of methods */ private Collection<Method> sortControllerMethods(Set<Method> controllerMethods) { List<Method> list = new ArrayList<>(controllerMethods); list.sort((m1, m2) -> { int o1 = Integer.MAX_VALUE; Order order1 = ClassUtils.getAnnotation(m1, Order.class); if (order1 != null) { o1 = order1.value(); } int o2 = Integer.MAX_VALUE; Order order2 = ClassUtils.getAnnotation(m2, Order.class); if (order2 != null) { o2 = order2.value(); } if (o1 == o2) { // same or unsorted, compare controller+controllerMethod String s1 = LangUtils.toString(m1); String s2 = LangUtils.toString(m2); return s1.compareTo(s2); } return (o1 < o2) ? -1 : 1; }); return list; }
public Class<?> getParameterGenericType() { if (parameterGenericType == null) { Parameter parameter = getParameter(); Type parameterType = parameter.getParameterizedType(); if (!ParameterizedType.class.isAssignableFrom(parameterType.getClass())) { throw new PippoRuntimeException("Please specify a generic parameter type for '{}' of '{}'", getParameterName(), LangUtils.toString(method)); } ParameterizedType parameterizedType = (ParameterizedType) parameterType; try { parameterGenericType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; } catch (ClassCastException e) { throw new PippoRuntimeException("Please specify a generic parameter type for '{}' of '{}'", getParameterName(), LangUtils.toString(method)); } } return parameterGenericType; }
/** * Init extractors from controller method. */ protected void initExtractors() { Parameter[] parameters = controllerMethod.getParameters(); extractors = new MethodParameterExtractor[parameters.length]; for (int i = 0; i < parameters.length; i++) { MethodParameter parameter = new MethodParameter(controllerMethod, i); MethodParameterExtractor extractor = application.getExtractors().stream() .filter(e -> e.isApplicable(parameter)) .findFirst() .orElse(null); if (extractor == null) { throw new PippoRuntimeException( "Method '{}' parameter {} of type '{}' does not specify a extractor", LangUtils.toString(controllerMethod), i + 1, parameter.getParameterType()); } extractors[i] = extractor; } }
log.debug("Found '{}' annotation on method '{}'", Metered.class.getSimpleName(), LangUtils.toString(method)); Metered metered = method.getAnnotation(Metered.class); String metricName = !metered.value().isEmpty() ? metered.value() : getMetricName(route, method); handler = new MeteredHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Timed.class)) { log.debug("Found '{}' annotation on method '{}'", Timed.class.getSimpleName(), LangUtils.toString(method)); Timed timed = method.getAnnotation(Timed.class); String metricName = !timed.value().isEmpty() ? timed.value() : getMetricName(route, method); handler = new TimedHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Counted.class)) { log.debug("Found '{}' annotation on method '{}'", Counted.class.getSimpleName(), LangUtils.toString(method)); Counted counted = method.getAnnotation(Counted.class); String metricName = !counted.value().isEmpty() ? counted.value() : getMetricName(route, method);
private String getParameterName(MethodParameter parameter, Param annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
private String getParameterName(MethodParameter parameter, Session annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
private String getParameterName(MethodParameter parameter, Header annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
log.trace("Processing '{}' interceptors", LangUtils.toString(controllerMethod)); int preInterceptStatus = routeContext.getResponse().getStatus(); processRouteInterceptors(routeContext); log.trace("Preparing '{}' parameters from request", LangUtils.toString(controllerMethod)); Object[] values = prepareMethodParameters(routeContext); log.trace("Invoking '{}'", LangUtils.toString(controllerMethod)); log.debug("Response committed in {}", LangUtils.toString(controllerMethod)); } else { if (!controllerMethod.getReturnType().equals(Void.TYPE)) {
/** * Validates that the declared content-types can actually be generated by Pippo. * * @param contentTypes */ protected void validateProduces(Collection<String> contentTypes) { Set<String> ignoreProduces = new TreeSet<>(); ignoreProduces.add(Produces.TEXT); ignoreProduces.add(Produces.HTML); ignoreProduces.add(Produces.XHTML); for (String produces : declaredProduces) { if (ignoreProduces.contains(produces)) { continue; } if (!contentTypes.contains(produces)) { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!", LangUtils.toString(controllerMethod), Produces.class.getSimpleName(), produces); } } }
log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), Consumes.ALL); return true; log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), "*/*"); return true; if (declaredConsumes.contains(type)) { log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), type); return true; } else { String fuzzyType = declaredType.substring(0, fuzz); if (type.startsWith(fuzzyType)) { log.debug("{} will handle Request because it consumes '{}'", LangUtils.toString(controllerMethod), type); return true; LangUtils.toString(controllerMethod), declaredConsumes); } else { log.warn("{} can not handle Request for '{}' because Route @Consumes '{}'", LangUtils.toString(controllerMethod), types, declaredConsumes);
if (consume.equals(declaredConsume)) { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for that type!", LangUtils.toString(controllerMethod), Consumes.class.getSimpleName(), declaredConsume); } else { throw new PippoRuntimeException("{} declares @{}(\"{}\") but there is no registered ContentTypeEngine for \"{}\"!", LangUtils.toString(controllerMethod), Consumes.class.getSimpleName(), declaredConsume, consume);
/** * Sort the controller's methods by their preferred order, if specified. * * @param controllerMethods * @return a sorted list of methods */ private Collection<Method> sortControllerMethods(Set<Method> controllerMethods) { List<Method> list = new ArrayList<>(controllerMethods); list.sort((m1, m2) -> { int o1 = Integer.MAX_VALUE; Order order1 = ClassUtils.getAnnotation(m1, Order.class); if (order1 != null) { o1 = order1.value(); } int o2 = Integer.MAX_VALUE; Order order2 = ClassUtils.getAnnotation(m2, Order.class); if (order2 != null) { o2 = order2.value(); } if (o1 == o2) { // same or unsorted, compare controller+controllerMethod String s1 = LangUtils.toString(m1); String s2 = LangUtils.toString(m2); return s1.compareTo(s2); } return (o1 < o2) ? -1 : 1; }); return list; }
/** * Init extractors from controller method. */ protected void initExtractors() { Parameter[] parameters = controllerMethod.getParameters(); extractors = new MethodParameterExtractor[parameters.length]; for (int i = 0; i < parameters.length; i++) { MethodParameter parameter = new MethodParameter(controllerMethod, i); MethodParameterExtractor extractor = application.getExtractors().stream() .filter(e -> e.isApplicable(parameter)) .findFirst() .orElse(null); if (extractor == null) { throw new PippoRuntimeException( "Method '{}' parameter {} of type '{}' does not specify a extractor", LangUtils.toString(controllerMethod), i + 1, parameter.getParameterType()); } extractors[i] = extractor; } }
public Class<?> getParameterGenericType() { if (parameterGenericType == null) { Parameter parameter = getParameter(); Type parameterType = parameter.getParameterizedType(); if (!ParameterizedType.class.isAssignableFrom(parameterType.getClass())) { throw new PippoRuntimeException("Please specify a generic parameter type for '{}' of '{}'", getParameterName(), LangUtils.toString(method)); } ParameterizedType parameterizedType = (ParameterizedType) parameterType; try { parameterGenericType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; } catch (ClassCastException e) { throw new PippoRuntimeException("Please specify a generic parameter type for '{}' of '{}'", getParameterName(), LangUtils.toString(method)); } } return parameterGenericType; }
log.debug("Found '{}' annotation on method '{}'", Metered.class.getSimpleName(), LangUtils.toString(method)); Metered metered = method.getAnnotation(Metered.class); String metricName = !metered.value().isEmpty() ? metered.value() : getMetricName(route, method); handler = new MeteredHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Timed.class)) { log.debug("Found '{}' annotation on method '{}'", Timed.class.getSimpleName(), LangUtils.toString(method)); Timed timed = method.getAnnotation(Timed.class); String metricName = !timed.value().isEmpty() ? timed.value() : getMetricName(route, method); handler = new TimedHandler(metricName, metricRegistry, route.getRouteHandler()); } else if (method.isAnnotationPresent(Counted.class)) { log.debug("Found '{}' annotation on method '{}'", Counted.class.getSimpleName(), LangUtils.toString(method)); Counted counted = method.getAnnotation(Counted.class); String metricName = !counted.value().isEmpty() ? counted.value() : getMetricName(route, method);
private String getParameterName(MethodParameter parameter, Session annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }
private String getParameterName(MethodParameter parameter, Header annotation) { String name = annotation.value(); if (StringUtils.isNullOrEmpty(name)) { name = parameter.getParameterName(); } if (name == null) { throw new PippoRuntimeException( "Method '{}' parameter {} does not specify a name!", LangUtils.toString(parameter.getMethod()), parameter.getParameterIndex()); } return name; }