use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.
the class MethodSelectingRouter method determineResponseMediaType.
/**
* Determine the {@link MediaType} of the {@link Response} based on writers suitable for the given entity class,
* pre-selected method and acceptable media types.
*
* @param entityClass entity class to determine the media type for.
* @param entityType entity type for writers.
* @param selectedMethod pre-selected (invoked) method.
* @param acceptableMediaTypes acceptable media types from request.
* @return media type of the response.
*/
private MediaType determineResponseMediaType(final Class<?> entityClass, final Type entityType, final RequestSpecificConsumesProducesAcceptor selectedMethod, final List<AcceptableMediaType> acceptableMediaTypes) {
// Return pre-selected MediaType.
if (usePreSelectedMediaType(selectedMethod, acceptableMediaTypes)) {
return selectedMethod.produces.combinedType;
}
final ResourceMethod resourceMethod = selectedMethod.methodRouting.method;
final Invocable invocable = resourceMethod.getInvocable();
// Entity class can be null when considering HEAD method || empty entity.
final Class<?> responseEntityClass = entityClass == null ? invocable.getRawRoutingResponseType() : entityClass;
final Method handlingMethod = invocable.getHandlingMethod();
// Media types producible by method.
final List<MediaType> methodProducesTypes = !resourceMethod.getProducedTypes().isEmpty() ? resourceMethod.getProducedTypes() : Collections.singletonList(MediaType.WILDCARD_TYPE);
// Applicable entity providers
final List<WriterModel> writersForEntityType = workers.getWritersModelsForType(responseEntityClass);
CombinedMediaType selected = null;
for (final MediaType acceptableMediaType : acceptableMediaTypes) {
for (final MediaType methodProducesType : methodProducesTypes) {
if (!acceptableMediaType.isCompatible(methodProducesType)) {
// no need to go deeper if acceptable and method produces type are incompatible
continue;
}
// Use writers suitable for entity class to determine the media type.
for (final WriterModel model : writersForEntityType) {
for (final MediaType writerProduces : model.declaredTypes()) {
if (!writerProduces.isCompatible(acceptableMediaType) || !methodProducesType.isCompatible(writerProduces)) {
continue;
}
final CombinedMediaType.EffectiveMediaType effectiveProduces = new CombinedMediaType.EffectiveMediaType(MediaTypes.mostSpecific(methodProducesType, writerProduces), false);
final CombinedMediaType candidate = CombinedMediaType.create(acceptableMediaType, effectiveProduces);
if (candidate != CombinedMediaType.NO_MATCH) {
// Look for a better compatible worker.
if (selected == null || CombinedMediaType.COMPARATOR.compare(candidate, selected) < 0) {
if (model.isWriteable(responseEntityClass, entityType, handlingMethod.getDeclaredAnnotations(), candidate.combinedType)) {
selected = candidate;
}
}
}
}
}
}
}
// Found media type for current writer.
if (selected != null) {
return selected.combinedType;
}
// so it can be written.
return selectedMethod.produces.combinedType;
}
use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.
the class MethodSelectingRouter method isReadable.
private boolean isReadable(final RequestSpecificConsumesProducesAcceptor candidate) {
final Invocable invocable = candidate.methodRouting.method.getInvocable();
final Method handlingMethod = invocable.getHandlingMethod();
final Parameter entityParam = getEntityParam(invocable);
if (entityParam == null) {
return true;
} else {
final Class<?> entityType = entityParam.getRawType();
for (final ReaderModel model : workers.getReaderModelsForType(entityType)) {
if (model.isReadable(entityType, entityParam.getType(), handlingMethod.getDeclaredAnnotations(), candidate.consumes.combinedType)) {
return true;
}
}
}
return false;
}
use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.
the class DefaultConfiguredValidator method onValidate.
// Invoked as the last validation interceptor method in the chain.
@Override
public void onValidate(final ValidationInterceptorContext ctx) {
final Object resource = ctx.getResource();
final Invocable resourceMethod = ctx.getInvocable();
final Object[] args = ctx.getArgs();
final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
// Resource validation.
if (beanDescriptor.isBeanConstrained()) {
constraintViolations.addAll(validate(resource));
}
if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
final Method handlingMethod = resourceMethod.getHandlingMethod();
// Resource method validation - input parameters.
final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters()) {
constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args));
}
}
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
}
use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.
the class AbstractErrorTemplateMapper method getErrorTemplate.
/**
* Get an {@link ErrorTemplate} annotation from resource method / class the throwable was raised from.
*
* @return an error template annotation or {@code null} if the method is not annotated.
*/
private ErrorTemplate getErrorTemplate() {
final ExtendedUriInfo uriInfo = uriInfoProvider.get();
final ResourceMethod matchedResourceMethod = uriInfo.getMatchedResourceMethod();
if (matchedResourceMethod != null) {
final Invocable invocable = matchedResourceMethod.getInvocable();
ErrorTemplate errorTemplate = invocable.getHandlingMethod().getAnnotation(ErrorTemplate.class);
if (errorTemplate == null) {
Class<?> handlerClass = invocable.getHandler().getHandlerClass();
if (invocable.isInflector() && TemplateInflector.class.isAssignableFrom(invocable.getHandler().getHandlerClass())) {
handlerClass = ((TemplateInflector) invocable.getHandler().getInstance(null)).getModelClass();
}
errorTemplate = handlerClass.getAnnotation(ErrorTemplate.class);
}
return errorTemplate;
}
return null;
}
use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.
the class ResourceMethodDispatcherFactoryTest method testBasicDispatchers.
@Test
public void testBasicDispatchers() throws InterruptedException, ExecutionException {
final Resource.Builder rb = Resource.builder();
final Method[] methods = this.getClass().getDeclaredMethods();
for (Method method : methods) {
if (Modifier.isPrivate(method.getModifiers())) {
// class-based
rb.addMethod("GET").handledBy(this.getClass(), method);
// instance-based
rb.addMethod("GET").handledBy(this, method);
}
}
for (ResourceModelComponent component : rb.build().getComponents()) {
if (component instanceof ResourceMethod) {
Invocable invocable = ((ResourceMethod) component).getInvocable();
assertNotNull("No dispatcher found for invocable " + invocable.toString(), rmdf.create(invocable, rmihf.create(invocable), null));
}
}
}
Aggregations