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));
}
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;
}
});
}
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<>() {...}}.
* </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;
}
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;
}
});
}
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;
}
});
}
Aggregations