org.glassfish.jersey.server.model.IntrospectionModeller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jersey-server Show documentation
Show all versions of jersey-server Show documentation
Jersey core server implementation
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.server.model;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.Encoded;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.NameBinding;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.SseEventSink;
import org.glassfish.jersey.internal.Errors;
import org.glassfish.jersey.internal.util.Producer;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.Tokenizer;
import org.glassfish.jersey.server.ManagedAsync;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.server.model.internal.ModelHelper;
/**
* Utility class for constructing resource model from JAX-RS annotated POJO.
*
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
*/
final class IntrospectionModeller {
private static final Logger LOGGER = Logger.getLogger(IntrospectionModeller.class.getName());
// introspected annotated JAX-RS resource class
private final Class handlerClass;
// validation flag
private final boolean disableValidation;
/**
* Create a new introspection modeller for a given JAX-RS resource class.
*
* @param handlerClass JAX-RS resource (handler) class.
* @param disableValidation if set to {@code true}, then any model validation checks will be disabled.
*/
public IntrospectionModeller(Class handlerClass, boolean disableValidation) {
this.handlerClass = handlerClass;
this.disableValidation = disableValidation;
}
/**
* Create a new resource model builder for the introspected class.
*
* The model returned is filled with the introspected data.
*
*
* @return new resource model builder for the introspected class.
*/
public Resource.Builder createResourceBuilder() {
return Errors.processWithException(new Producer() {
@Override
public Resource.Builder call() {
return doCreateResourceBuilder();
}
});
}
private Resource.Builder doCreateResourceBuilder() {
if (!disableValidation) {
checkForNonPublicMethodIssues();
}
final Class annotatedResourceClass = ModelHelper.getAnnotatedResourceClass(handlerClass);
final Path rPathAnnotation = annotatedResourceClass.getAnnotation(Path.class);
final boolean keepEncodedParams =
(null != annotatedResourceClass.getAnnotation(Encoded.class));
final List defaultConsumedTypes =
extractMediaTypes(annotatedResourceClass.getAnnotation(Consumes.class));
final List defaultProducedTypes =
extractMediaTypes(annotatedResourceClass.getAnnotation(Produces.class));
final Collection> defaultNameBindings =
ReflectionHelper.getAnnotationTypes(annotatedResourceClass, NameBinding.class);
final MethodList methodList = new MethodList(handlerClass);
final List resourceClassParameters = new LinkedList<>();
checkResourceClassSetters(methodList, keepEncodedParams, resourceClassParameters);
checkResourceClassFields(keepEncodedParams, InvocableValidator.isSingleton(handlerClass), resourceClassParameters);
Resource.Builder resourceBuilder;
if (null != rPathAnnotation) {
resourceBuilder = Resource.builder(rPathAnnotation.value());
} else {
resourceBuilder = Resource.builder();
}
boolean extended = false;
if (handlerClass.isAnnotationPresent(ExtendedResource.class)) {
resourceBuilder.extended(true);
extended = true;
}
resourceBuilder.name(handlerClass.getName());
addResourceMethods(resourceBuilder, methodList, resourceClassParameters, keepEncodedParams,
defaultConsumedTypes, defaultProducedTypes, defaultNameBindings, extended);
addSubResourceMethods(resourceBuilder, methodList, resourceClassParameters, keepEncodedParams,
defaultConsumedTypes, defaultProducedTypes, defaultNameBindings, extended);
addSubResourceLocators(resourceBuilder, methodList, resourceClassParameters, keepEncodedParams, extended);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(LocalizationMessages.NEW_AR_CREATED_BY_INTROSPECTION_MODELER(
resourceBuilder.toString()));
}
return resourceBuilder;
}
private void checkForNonPublicMethodIssues() {
final MethodList allDeclaredMethods = new MethodList(getAllDeclaredMethods(handlerClass));
// non-public resource methods
for (AnnotatedMethod m : allDeclaredMethods.withMetaAnnotation(HttpMethod.class)
.withoutAnnotation(Path.class).isNotPublic()) {
Errors.warning(handlerClass, LocalizationMessages.NON_PUB_RES_METHOD(m.getMethod().toGenericString()));
}
// non-public sub-resource methods
for (AnnotatedMethod m : allDeclaredMethods.withMetaAnnotation(HttpMethod.class)
.withAnnotation(Path.class).isNotPublic()) {
Errors.warning(handlerClass, LocalizationMessages.NON_PUB_SUB_RES_METHOD(m.getMethod().toGenericString()));
}
// non-public sub-resource locators
for (AnnotatedMethod m : allDeclaredMethods.withoutMetaAnnotation(HttpMethod.class)
.withAnnotation(Path.class).isNotPublic()) {
Errors.warning(handlerClass, LocalizationMessages.NON_PUB_SUB_RES_LOC(m.getMethod().toGenericString()));
}
}
private void checkResourceClassSetters(final MethodList methodList,
final boolean encodedFlag,
Collection injectableParameters) {
for (AnnotatedMethod method : methodList.withoutMetaAnnotation(HttpMethod.class)
.withoutAnnotation(Path.class)
.hasNumParams(1)
.hasReturnType(void.class)
.nameStartsWith("set")) {
Parameter p = Parameter.create(
handlerClass,
method.getMethod().getDeclaringClass(),
encodedFlag || method.isAnnotationPresent(Encoded.class),
method.getParameterTypes()[0],
method.getGenericParameterTypes()[0],
method.getAnnotations());
if (null != p) {
if (!disableValidation) {
ResourceMethodValidator.validateParameter(p, method.getMethod(), method.getMethod().toGenericString(), "1",
InvocableValidator.isSingleton(handlerClass));
}
// we do not inject entity parameters into class instance fields and properties.
if (p.getSource() != Parameter.Source.ENTITY) {
injectableParameters.add(p);
}
}
}
}
private void checkResourceClassFields(final boolean encodedFlag,
boolean isInSingleton,
Collection injectableParameters) {
for (Field field : AccessController.doPrivileged(ReflectionHelper.getDeclaredFieldsPA(handlerClass))) {
if (field.getDeclaredAnnotations().length > 0) {
Parameter p = Parameter.create(
handlerClass,
field.getDeclaringClass(),
encodedFlag || field.isAnnotationPresent(Encoded.class),
field.getType(),
field.getGenericType(),
field.getAnnotations());
if (null != p) {
if (!disableValidation) {
ResourceMethodValidator.validateParameter(p, field, field.toGenericString(), field.getName(),
isInSingleton);
}
// we do not inject entity and unknown parameters into class instance fields and properties.
if (p.getSource() != Parameter.Source.ENTITY) {
injectableParameters.add(p);
}
}
}
}
}
private List getAllDeclaredMethods(final Class clazz) {
final List result = new LinkedList<>();
AccessController.doPrivileged(new PrivilegedAction