Search in sources :

Example 1 with MappableException

use of org.glassfish.jersey.server.internal.process.MappableException in project jersey by jersey.

the class ParameterValueHelper method getParameterValues.

/**
     * Get the array of parameter values.
     *
     * @param valueProviders a list of value providers.
     * @return array of parameter values provided by the value providers.
     */
public static Object[] getParameterValues(List<ParamValueFactoryWithSource<?>> valueProviders) {
    final Object[] params = new Object[valueProviders.size()];
    try {
        int entityProviderIndex = -1;
        int index = 0;
        for (ParamValueFactoryWithSource<?> paramValProvider : valueProviders) {
            // entity provider has to be called last; see JERSEY-2642
            if (paramValProvider.getSource().equals(Parameter.Source.ENTITY)) {
                entityProviderIndex = index++;
                continue;
            }
            params[index++] = paramValProvider.get();
        }
        if (entityProviderIndex != -1) {
            params[entityProviderIndex] = valueProviders.get(entityProviderIndex).get();
        }
        return params;
    } catch (WebApplicationException e) {
        throw e;
    } catch (MessageBodyProviderNotFoundException e) {
        throw new NotSupportedException(e);
    } catch (ProcessingException e) {
        throw e;
    } catch (RuntimeException e) {
        if (e.getCause() instanceof WebApplicationException) {
            throw (WebApplicationException) e.getCause();
        }
        throw new MappableException("Exception obtaining parameters", e);
    }
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) NotSupportedException(javax.ws.rs.NotSupportedException) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with MappableException

use of org.glassfish.jersey.server.internal.process.MappableException in project jersey by jersey.

the class AbstractJavaResourceMethodDispatcher method invoke.

/**
     * Use the underlying invocation handler to invoke the underlying Java method
     * with the supplied input method argument values on a given resource instance.
     *
     * @param containerRequest container request.
     * @param resource         resource class instance.
     * @param args             input argument values for the invoked Java method.
     * @return invocation result.
     * @throws ProcessingException (possibly {@link MappableException mappable})
     *                             container exception in case the invocation failed.
     */
final Object invoke(final ContainerRequest containerRequest, final Object resource, final Object... args) throws ProcessingException {
    try {
        // Validate resource class & method input parameters.
        if (validator != null) {
            validator.validateResourceAndInputParams(resource, resourceMethod, args);
        }
        final PrivilegedAction invokeMethodAction = new PrivilegedAction() {

            @Override
            public Object run() {
                final TracingLogger tracingLogger = TracingLogger.getInstance(containerRequest);
                final long timestamp = tracingLogger.timestamp(ServerTraceEvent.METHOD_INVOKE);
                try {
                    return methodHandler.invoke(resource, method, args);
                } catch (IllegalAccessException | IllegalArgumentException | UndeclaredThrowableException ex) {
                    throw new ProcessingException(LocalizationMessages.ERROR_RESOURCE_JAVA_METHOD_INVOCATION(), ex);
                } catch (InvocationTargetException ex) {
                    throw mapTargetToRuntimeEx(ex.getCause());
                } catch (Throwable t) {
                    throw new ProcessingException(t);
                } finally {
                    tracingLogger.logDuration(ServerTraceEvent.METHOD_INVOKE, timestamp, resource, method);
                }
            }
        };
        final SecurityContext securityContext = containerRequest.getSecurityContext();
        final Object invocationResult = (securityContext instanceof SubjectSecurityContext) ? ((SubjectSecurityContext) securityContext).doAsSubject(invokeMethodAction) : invokeMethodAction.run();
        // Validate response entity.
        if (validator != null) {
            validator.validateResult(resource, resourceMethod, invocationResult);
        }
        return invocationResult;
    } catch (ValidationException ex) {
        // handle validation exceptions -> potentially mappable
        throw new MappableException(ex);
    }
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) ValidationException(javax.validation.ValidationException) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) InvocationTargetException(java.lang.reflect.InvocationTargetException) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecurityContext(javax.ws.rs.core.SecurityContext) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with MappableException

use of org.glassfish.jersey.server.internal.process.MappableException in project jersey by jersey.

the class ContainerFilteringStage method apply.

@Override
@SuppressWarnings("unchecked")
public Continuation<RequestProcessingContext> apply(RequestProcessingContext context) {
    Iterable<ContainerRequestFilter> sortedRequestFilters;
    final boolean postMatching = responseFilters == null;
    final ContainerRequest request = context.request();
    final TracingLogger tracingLogger = TracingLogger.getInstance(request);
    if (postMatching) {
        // post-matching
        final ArrayList<Iterable<RankedProvider<ContainerRequestFilter>>> rankedProviders = new ArrayList<>(2);
        rankedProviders.add(requestFilters);
        rankedProviders.add(request.getRequestFilters());
        sortedRequestFilters = Providers.mergeAndSortRankedProviders(new RankedComparator<ContainerRequestFilter>(), rankedProviders);
        context.monitoringEventBuilder().setContainerRequestFilters(sortedRequestFilters);
        context.triggerEvent(RequestEvent.Type.REQUEST_MATCHED);
    } else {
        // pre-matching (response filter stage is pushed in pre-matching phase, so that if pre-matching filter
        // throws exception, response filters get still invoked)
        context.push(new ResponseFilterStage(context, responseFilters, tracingLogger));
        sortedRequestFilters = Providers.sortRankedProviders(new RankedComparator<ContainerRequestFilter>(), requestFilters);
    }
    final TracingLogger.Event summaryEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER_SUMMARY : ServerTraceEvent.PRE_MATCH_SUMMARY);
    final long timestamp = tracingLogger.timestamp(summaryEvent);
    int processedCount = 0;
    try {
        final TracingLogger.Event filterEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER : ServerTraceEvent.PRE_MATCH);
        for (ContainerRequestFilter filter : sortedRequestFilters) {
            final long filterTimestamp = tracingLogger.timestamp(filterEvent);
            try {
                filter.filter(request);
            } catch (Exception exception) {
                throw new MappableException(exception);
            } finally {
                processedCount++;
                tracingLogger.logDuration(filterEvent, filterTimestamp, filter);
            }
            final Response abortResponse = request.getAbortResponse();
            if (abortResponse != null) {
                // abort accepting & return response
                return Continuation.of(context, Stages.asStage(new Endpoint() {

                    @Override
                    public ContainerResponse apply(final RequestProcessingContext requestContext) {
                        return new ContainerResponse(requestContext.request(), abortResponse);
                    }
                }));
            }
        }
    } finally {
        if (postMatching) {
            context.triggerEvent(RequestEvent.Type.REQUEST_FILTERED);
        }
        tracingLogger.logDuration(summaryEvent, timestamp, processedCount);
    }
    return Continuation.of(context, getDefaultNext());
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) ArrayList(java.util.ArrayList) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) MappableException(org.glassfish.jersey.server.internal.process.MappableException) RankedComparator(org.glassfish.jersey.model.internal.RankedComparator) Response(javax.ws.rs.core.Response) RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 4 with MappableException

use of org.glassfish.jersey.server.internal.process.MappableException in project jersey by jersey.

the class SubResourceLocatorRouter method getResource.

private Object getResource(final RequestProcessingContext context) {
    final Object resource = context.routingContext().peekMatchedResource();
    final Method handlingMethod = locatorModel.getInvocable().getHandlingMethod();
    final Object[] parameterValues = ParameterValueHelper.getParameterValues(valueProviders);
    context.triggerEvent(RequestEvent.Type.LOCATOR_MATCHED);
    final PrivilegedAction invokeMethodAction = new PrivilegedAction() {

        @Override
        public Object run() {
            try {
                return handlingMethod.invoke(resource, parameterValues);
            } catch (IllegalAccessException | IllegalArgumentException | UndeclaredThrowableException ex) {
                throw new ProcessingException(LocalizationMessages.ERROR_RESOURCE_JAVA_METHOD_INVOCATION(), ex);
            } catch (final InvocationTargetException ex) {
                final Throwable cause = ex.getCause();
                if (cause instanceof WebApplicationException) {
                    throw (WebApplicationException) cause;
                }
                // handle all exceptions as potentially mappable (incl. ProcessingException)
                throw new MappableException(cause);
            } catch (final Throwable t) {
                throw new ProcessingException(t);
            }
        }
    };
    final SecurityContext securityContext = context.request().getSecurityContext();
    return (securityContext instanceof SubjectSecurityContext) ? ((SubjectSecurityContext) securityContext).doAsSubject(invokeMethodAction) : invokeMethodAction.run();
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecurityContext(javax.ws.rs.core.SecurityContext) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

MappableException (org.glassfish.jersey.server.internal.process.MappableException)4 ProcessingException (javax.ws.rs.ProcessingException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 PrivilegedAction (java.security.PrivilegedAction)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 TracingLogger (org.glassfish.jersey.message.internal.TracingLogger)2 SubjectSecurityContext (org.glassfish.jersey.server.SubjectSecurityContext)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ValidationException (javax.validation.ValidationException)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)1 Response (javax.ws.rs.core.Response)1 MessageBodyProviderNotFoundException (org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException)1 RankedComparator (org.glassfish.jersey.model.internal.RankedComparator)1 Endpoint (org.glassfish.jersey.server.internal.process.Endpoint)1 RequestProcessingContext (org.glassfish.jersey.server.internal.process.RequestProcessingContext)1 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)1