Search in sources :

Example 6 with Invocable

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;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) WriterModel(org.glassfish.jersey.message.WriterModel) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 7 with Invocable

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;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Parameter(org.glassfish.jersey.server.model.Parameter) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ReaderModel(org.glassfish.jersey.message.ReaderModel)

Example 8 with Invocable

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);
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) MethodDescriptor(javax.validation.metadata.MethodDescriptor) HashSet(java.util.HashSet)

Example 9 with Invocable

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;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) ErrorTemplate(org.glassfish.jersey.server.mvc.ErrorTemplate) TemplateInflector(org.glassfish.jersey.server.mvc.internal.TemplateInflector) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Example 10 with Invocable

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));
        }
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceModelComponent(org.glassfish.jersey.server.model.ResourceModelComponent) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Test(org.junit.Test)

Aggregations

Invocable (org.glassfish.jersey.server.model.Invocable)10 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)6 Method (java.lang.reflect.Method)4 Type (java.lang.reflect.Type)2 ConstraintViolation (javax.validation.ConstraintViolation)2 HttpMethod (javax.ws.rs.HttpMethod)2 MediaType (javax.ws.rs.core.MediaType)2 Response (javax.ws.rs.core.Response)2 WriterModel (org.glassfish.jersey.message.WriterModel)2 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)2 ExtendedUriInfo (org.glassfish.jersey.server.ExtendedUriInfo)2 Resource (org.glassfish.jersey.server.model.Resource)2 ResourceModelComponent (org.glassfish.jersey.server.model.ResourceModelComponent)2 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1