Search in sources :

Example 6 with RequestScope

use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.

the class JerseyInvocation method invoke.

@Override
public Response invoke() throws ProcessingException, WebApplicationException {
    final ClientRuntime runtime = request().getClientRuntime();
    final RequestScope requestScope = runtime.getRequestScope();
    return requestScope.runInScope((Producer<Response>) () -> new InboundJaxrsResponse(runtime.invoke(requestForCall(requestContext)), requestScope));
}
Also used : Response(javax.ws.rs.core.Response) RequestScope(org.glassfish.jersey.process.internal.RequestScope)

Example 7 with RequestScope

use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.

the class JerseyInvocation method invoke.

@Override
public <T> T invoke(final Class<T> responseType) throws ProcessingException, WebApplicationException {
    if (responseType == null) {
        throw new IllegalArgumentException(LocalizationMessages.RESPONSE_TYPE_IS_NULL());
    }
    final ClientRuntime runtime = request().getClientRuntime();
    final RequestScope requestScope = runtime.getRequestScope();
    //noinspection Duplicates
    return requestScope.runInScope(() -> {
        try {
            return translate(runtime.invoke(requestForCall(requestContext)), requestScope, responseType);
        } catch (final ProcessingException ex) {
            if (ex.getCause() instanceof WebApplicationException) {
                throw (WebApplicationException) ex.getCause();
            }
            throw ex;
        }
    });
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RequestScope(org.glassfish.jersey.process.internal.RequestScope) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 8 with RequestScope

use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.

the class JerseyInvocation method submit.

/**
     * Submit the request for an asynchronous invocation and register an
     * {@link InvocationCallback} to process the future result of the invocation.
     * <p>
     * Response type in this case is taken from {@code responseType} param (if not {@code null}) rather
     * than from {@code callback}. This allows to pass callbacks like {@code new InvocationCallback&lt;&gt() {...}}.
     * </p>
     *
     * @param <T>          response type
     * @param responseType response type that is used instead of obtaining types from {@code callback}.
     * @param callback     invocation callback for asynchronous processing of the
     *                     request invocation result.
     * @return future response object of the specified type as a result of the
     * request invocation.
     */
public <T> Future<T> submit(final GenericType<T> responseType, final InvocationCallback<T> callback) {
    final CompletableFuture<T> responseFuture = new CompletableFuture<>();
    try {
        final ReflectionHelper.DeclaringClassInterfacePair pair = ReflectionHelper.getClass(callback.getClass(), InvocationCallback.class);
        final Type callbackParamType;
        final Class<T> callbackParamClass;
        if (responseType == null) {
            // If we don't have response use callback to obtain param types.
            final Type[] typeArguments = ReflectionHelper.getParameterizedTypeArguments(pair);
            if (typeArguments == null || typeArguments.length == 0) {
                callbackParamType = Object.class;
            } else {
                callbackParamType = typeArguments[0];
            }
            callbackParamClass = ReflectionHelper.erasure(callbackParamType);
        } else {
            callbackParamType = responseType.getType();
            callbackParamClass = ReflectionHelper.erasure(responseType.getRawType());
        }
        final ResponseCallback responseCallback = new ResponseCallback() {

            @Override
            public void completed(final ClientResponse response, final RequestScope scope) {
                if (responseFuture.isCancelled()) {
                    response.close();
                    failed(new ProcessingException(new CancellationException(LocalizationMessages.ERROR_REQUEST_CANCELLED())));
                    return;
                }
                final T result;
                if (callbackParamClass == Response.class) {
                    result = callbackParamClass.cast(new InboundJaxrsResponse(response, scope));
                    responseFuture.complete(result);
                    callback.completed(result);
                } else if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
                    result = response.readEntity(new GenericType<T>(callbackParamType));
                    responseFuture.complete(result);
                    callback.completed(result);
                } else {
                    failed(convertToException(new InboundJaxrsResponse(response, scope)));
                }
            }

            @Override
            public void failed(final ProcessingException error) {
                try {
                    if (error.getCause() instanceof WebApplicationException) {
                        responseFuture.completeExceptionally(error.getCause());
                    } else if (!responseFuture.isCancelled()) {
                        responseFuture.completeExceptionally(error);
                    }
                } finally {
                    callback.failed(error.getCause() instanceof CancellationException ? error.getCause() : error);
                }
            }
        };
        request().getClientRuntime().submit(requestForCall(requestContext), responseCallback);
    } catch (final Throwable error) {
        final ProcessingException ce;
        //noinspection ChainOfInstanceofChecks
        if (error instanceof ProcessingException) {
            ce = (ProcessingException) error;
            responseFuture.completeExceptionally(ce);
        } else if (error instanceof WebApplicationException) {
            ce = new ProcessingException(error);
            responseFuture.completeExceptionally(error);
        } else {
            ce = new ProcessingException(error);
            responseFuture.completeExceptionally(ce);
        }
        callback.failed(ce);
    }
    return responseFuture;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) RequestScope(org.glassfish.jersey.process.internal.RequestScope) CompletableFuture(java.util.concurrent.CompletableFuture) MediaType(javax.ws.rs.core.MediaType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) CancellationException(java.util.concurrent.CancellationException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 9 with RequestScope

use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.

the class JerseyInvocation method invoke.

@Override
public <T> T invoke(final GenericType<T> responseType) throws ProcessingException, WebApplicationException {
    if (responseType == null) {
        throw new IllegalArgumentException(LocalizationMessages.RESPONSE_TYPE_IS_NULL());
    }
    final ClientRuntime runtime = request().getClientRuntime();
    final RequestScope requestScope = runtime.getRequestScope();
    //noinspection Duplicates
    return requestScope.runInScope(() -> {
        try {
            return translate(runtime.invoke(requestForCall(requestContext)), requestScope, responseType);
        } catch (final ProcessingException ex) {
            if (ex.getCause() instanceof WebApplicationException) {
                throw (WebApplicationException) ex.getCause();
            }
            throw ex;
        }
    });
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RequestScope(org.glassfish.jersey.process.internal.RequestScope) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 10 with RequestScope

use of org.glassfish.jersey.process.internal.RequestScope in project jersey by jersey.

the class JaxrsProvidersTest method testProviders.

@Test
public void testProviders() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER), new Binder());
    injectionManager.register(new TestBinder(injectionManager));
    TestBinder.initProviders(injectionManager);
    RequestScope scope = injectionManager.getInstance(RequestScope.class);
    scope.runInScope(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Providers instance = injectionManager.getInstance(Providers.class);
            assertNotNull(instance);
            assertSame(JaxrsProviders.class, instance.getClass());
            assertNotNull(instance.getExceptionMapper(Throwable.class));
            assertNotNull(instance.getMessageBodyReader(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getContextResolver(String.class, MediaType.TEXT_PLAIN_TYPE));
            return null;
        }
    });
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) MessagingBinders(org.glassfish.jersey.message.internal.MessagingBinders) Providers(javax.ws.rs.ext.Providers) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Aggregations

RequestScope (org.glassfish.jersey.process.internal.RequestScope)11 Test (org.junit.Test)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Supplier (java.util.function.Supplier)4 DisposableSupplier (org.glassfish.jersey.internal.inject.DisposableSupplier)4 ProcessingException (javax.ws.rs.ProcessingException)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)3 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 Singleton (javax.inject.Singleton)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 Type (java.lang.reflect.Type)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 GenericType (javax.ws.rs.core.GenericType)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 Providers (javax.ws.rs.ext.Providers)1 Binder (org.glassfish.jersey.internal.inject.Binder)1