Search in sources :

Example 1 with Invocable

use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.

the class MethodSelectingRouter method isWriteable.

private boolean isWriteable(final RequestSpecificConsumesProducesAcceptor candidate) {
    final Invocable invocable = candidate.methodRouting.method.getInvocable();
    final Class<?> responseType = Primitives.wrap(invocable.getRawRoutingResponseType());
    if (Response.class.isAssignableFrom(responseType) || Void.class.isAssignableFrom(responseType)) {
        return true;
    }
    final Type genericType = invocable.getRoutingResponseType();
    final Type genericReturnType = genericType instanceof GenericType ? ((GenericType) genericType).getType() : genericType;
    for (final WriterModel model : workers.getWritersModelsForType(responseType)) {
        if (model.isWriteable(responseType, genericReturnType, invocable.getHandlingMethod().getDeclaredAnnotations(), candidate.produces.combinedType)) {
            return true;
        }
    }
    return false;
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) Invocable(org.glassfish.jersey.server.model.Invocable) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) GenericType(javax.ws.rs.core.GenericType) WriterModel(org.glassfish.jersey.message.WriterModel)

Example 2 with Invocable

use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.

the class MethodSelectingRouter method fillMediaTypes.

private boolean fillMediaTypes(final Set<MediaType> effectiveTypes, final ResourceMethod resourceMethod, final List<MediaType> methodTypes, final boolean inputTypes) {
    // Add method types to the resulting list iff there is more than just */*
    if (methodTypes.size() > 1 || !methodTypes.contains(MediaType.WILDCARD_TYPE)) {
        effectiveTypes.addAll(methodTypes);
    }
    boolean mediaTypesFromWorkers = effectiveTypes.isEmpty();
    if (mediaTypesFromWorkers) {
        final Invocable invocableMethod = resourceMethod.getInvocable();
        // If not predefined from method - get it from workers.
        if (inputTypes) {
            fillInputTypesFromWorkers(effectiveTypes, invocableMethod);
        } else {
            fillOutputTypesFromWorkers(effectiveTypes, invocableMethod.getRawResponseType());
        }
        mediaTypesFromWorkers = !effectiveTypes.isEmpty();
        // If still empty - get all available.
        if (!mediaTypesFromWorkers) {
            if (inputTypes) {
                effectiveTypes.addAll(workers.getMessageBodyReaderMediaTypesByType(Object.class));
            } else {
                effectiveTypes.addAll(workers.getMessageBodyWriterMediaTypesByType(Object.class));
            }
            mediaTypesFromWorkers = true;
        }
    }
    return mediaTypesFromWorkers;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable)

Example 3 with Invocable

use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.

the class NaiveResourceMappingContext method buildMappings.

private void buildMappings() {
    if (mappings != null) {
        return;
    }
    mappings = new HashMap<>();
    erc.getResourceModel().accept(new ResourceModelVisitor() {

        Deque<PathPattern> stack = new LinkedList<>();

        private void processComponents(final ResourceModelComponent component) {
            final List<? extends ResourceModelComponent> components = component.getComponents();
            if (components != null) {
                for (final ResourceModelComponent rc : components) {
                    rc.accept(this);
                }
            }
        }

        @Override
        public void visitInvocable(final Invocable invocable) {
            processComponents(invocable);
        }

        @Override
        public void visitRuntimeResource(final RuntimeResource runtimeResource) {
            processComponents(runtimeResource);
        }

        @Override
        public void visitResourceModel(final ResourceModel resourceModel) {
            processComponents(resourceModel);
        }

        @Override
        public void visitResourceHandlerConstructor(final HandlerConstructor handlerConstructor) {
            processComponents(handlerConstructor);
        }

        @Override
        public void visitMethodHandler(final MethodHandler methodHandler) {
            processComponents(methodHandler);
        }

        @Override
        public void visitChildResource(final Resource resource) {
            visitResourceIntl(resource, false);
        }

        @Override
        public void visitResource(final Resource resource) {
            visitResourceIntl(resource, true);
        }

        private void visitResourceIntl(final Resource resource, final boolean isRoot) {
            try {
                stack.addLast(resource.getPathPattern());
                processComponents(resource);
                if (isRoot) {
                    Class likelyToBeRoot = null;
                    for (final Class next : resource.getHandlerClasses()) {
                        if (!(Inflector.class.isAssignableFrom(next))) {
                            likelyToBeRoot = next;
                        }
                    }
                    if (likelyToBeRoot != null) {
                        mappings.put(likelyToBeRoot, getMapping(getTemplate()));
                    }
                }
            } finally {
                stack.removeLast();
            }
        }

        @Override
        public void visitResourceMethod(final ResourceMethod resourceMethod) {
            if (resourceMethod.isExtended()) {
                return;
            }
            if (ResourceMethod.JaxrsType.SUB_RESOURCE_LOCATOR.equals(resourceMethod.getType())) {
                if (resourceMethod.getInvocable() != null) {
                    final Invocable i = resourceMethod.getInvocable();
                    final Type type = i.getResponseType();
                    final StringBuilder template = getTemplate();
                    mappings.put((Class) type, getMapping(template));
                    // Process sub resources ?
                    Resource.Builder builder = Resource.builder(i.getRawResponseType());
                    if (builder == null) {
                        // for example in the case the return type of the sub resource locator is Object
                        builder = Resource.builder().path(resourceMethod.getParent().getPath());
                    }
                    final Resource subResource = builder.build();
                    visitChildResource(subResource);
                }
            }
            processComponents(resourceMethod);
        }

        private StringBuilder getTemplate() {
            final StringBuilder template = new StringBuilder();
            for (final PathPattern pp : stack) {
                final String ppTemplate = pp.getTemplate().getTemplate();
                final int tlength = template.length();
                if (tlength > 0) {
                    if (template.charAt(tlength - 1) == '/') {
                        if (ppTemplate.startsWith("/")) {
                            template.append(ppTemplate, 1, ppTemplate.length());
                        } else {
                            template.append(ppTemplate);
                        }
                    } else {
                        if (ppTemplate.startsWith("/")) {
                            template.append(ppTemplate);
                        } else {
                            template.append("/");
                            template.append(ppTemplate);
                        }
                    }
                } else {
                    template.append(ppTemplate);
                }
            }
            return template;
        }
    });
}
Also used : Inflector(org.glassfish.jersey.process.Inflector) Resource(org.glassfish.jersey.server.model.Resource) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) ResourceModelComponent(org.glassfish.jersey.server.model.ResourceModelComponent) LinkedList(java.util.LinkedList) RuntimeResource(org.glassfish.jersey.server.model.RuntimeResource) Invocable(org.glassfish.jersey.server.model.Invocable) PathPattern(org.glassfish.jersey.uri.PathPattern) HandlerConstructor(org.glassfish.jersey.server.model.HandlerConstructor) MethodHandler(org.glassfish.jersey.server.model.MethodHandler) Type(java.lang.reflect.Type) ResourceModelVisitor(org.glassfish.jersey.server.model.ResourceModelVisitor) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) List(java.util.List) LinkedList(java.util.LinkedList) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 4 with Invocable

use of org.glassfish.jersey.server.model.Invocable in project jersey by jersey.

the class ServerScopeProvider method getFilteringScopes.

@Override
public Set<String> getFilteringScopes(final Annotation[] entityAnnotations, final boolean defaultIfNotFound) {
    Set<String> filteringScope = super.getFilteringScopes(entityAnnotations, false);
    if (filteringScope.isEmpty()) {
        final ExtendedUriInfo uriInfo = uriInfoProvider.get();
        final String path = uriInfo.getPath();
        if (uriToContexts.containsKey(path)) {
            return uriToContexts.get(path);
        }
        for (final ResourceMethod method : ServerScopeProvider.getMatchedMethods(uriInfo)) {
            final Invocable invocable = method.getInvocable();
            mergeFilteringScopes(filteringScope, getFilteringScopes(invocable.getHandlingMethod(), invocable.getHandler().getHandlerClass()));
            if (!filteringScope.isEmpty()) {
                uriToContexts.putIfAbsent(path, filteringScope);
                return filteringScope;
            }
        }
    }
    // Use default scope if not in other scope.
    return returnFilteringScopes(filteringScope, defaultIfNotFound);
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Example 5 with Invocable

use of org.glassfish.jersey.server.model.Invocable in project dropwizard by dropwizard.

the class JerseyViolationExceptionMapper method toResponse.

@Override
public Response toResponse(final JerseyViolationException exception) {
    final Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
    final Invocable invocable = exception.getInvocable();
    final ImmutableList<String> errors = FluentIterable.from(exception.getConstraintViolations()).transform(violation -> ConstraintMessage.getMessage(violation, invocable)).toList();
    final int status = ConstraintMessage.determineStatus(violations, invocable);
    return Response.status(status).entity(new ValidationErrorMessage(errors)).build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Response(javax.ws.rs.core.Response) Provider(javax.ws.rs.ext.Provider) FluentIterable(com.google.common.collect.FluentIterable) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) Set(java.util.Set) ConstraintViolation(javax.validation.ConstraintViolation) Invocable(org.glassfish.jersey.server.model.Invocable) Invocable(org.glassfish.jersey.server.model.Invocable) ConstraintViolation(javax.validation.ConstraintViolation)

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