use of javax.ws.rs.ProcessingException 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 javax.ws.rs.ProcessingException 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 javax.ws.rs.ProcessingException in project jersey by jersey.
the class JdkHttpServerFactory method createHttpServer.
private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler, final SSLContext sslContext, final boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_NULL());
}
final String scheme = uri.getScheme();
final boolean isHttp = "http".equalsIgnoreCase(scheme);
final boolean isHttps = "https".equalsIgnoreCase(scheme);
final HttpsConfigurator httpsConfigurator = sslContext != null ? new HttpsConfigurator(sslContext) : null;
if (isHttp) {
if (httpsConfigurator != null) {
// attempt to use https with http scheme
LOG.warning(LocalizationMessages.WARNING_CONTAINER_URI_SCHEME_SECURED());
}
} else if (isHttps) {
if (httpsConfigurator == null) {
if (start) {
// Starting https server w/o SSL is invalid, it will lead to error anyway.
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_HTTPS_NO_SSL());
} else {
// Creating the https server w/o SSL context, but not starting it is valid.
// However, server.setHttpsConfigurator() must be called before the start.
LOG.info(LocalizationMessages.INFO_CONTAINER_HTTPS_NO_SSL());
}
}
} else {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_SCHEME_UNKNOWN(uri));
}
final String path = uri.getPath();
if (path == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_NULL(uri));
} else if (path.isEmpty()) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_EMPTY(uri));
} else if (path.charAt(0) != '/') {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_START(uri));
}
final int port = (uri.getPort() == -1) ? (isHttp ? Container.DEFAULT_HTTP_PORT : Container.DEFAULT_HTTPS_PORT) : uri.getPort();
final HttpServer server;
try {
server = isHttp ? HttpServer.create(new InetSocketAddress(port), 0) : HttpsServer.create(new InetSocketAddress(port), 0);
} catch (final IOException ioe) {
throw new ProcessingException(LocalizationMessages.ERROR_CONTAINER_EXCEPTION_IO(), ioe);
}
if (isHttps && httpsConfigurator != null) {
((HttpsServer) server).setHttpsConfigurator(httpsConfigurator);
}
server.setExecutor(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("jdk-http-server-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build()));
server.createContext(path, handler);
final HttpServer wrapper = isHttp ? createHttpServerWrapper(server, handler) : createHttpsServerWrapper((HttpsServer) server, handler);
if (start) {
wrapper.start();
}
return wrapper;
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class JdkHttpsServerTest method testCreateHttpsServerDefaultSslContext.
/**
* Test, that {@link javax.net.ssl.SSLHandshakeException} is thrown when attepmting to connect to server with client
* not configured correctly.
* @throws Exception
*/
@Test(expected = SSLHandshakeException.class)
public void testCreateHttpsServerDefaultSslContext() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, SSLContext.getDefault(), true);
assertThat(server, instanceOf(HttpsServer.class));
// access the https server with not configured client
final Client client = ClientBuilder.newBuilder().newClient();
try {
client.target(httpsUri).path("testHttps").request().get(String.class);
} catch (final ProcessingException e) {
throw e.getCause();
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class NettyHttpContainerProvider method createHttp2Server.
/**
* Create and start Netty HTTP/2 server.
* <p>
* The server is capable of connection upgrade to HTTP/2. HTTP/1.x request will be server as they were used to.
* <p>
* Note that this implementation cannot be more experimental. Any contributions / feedback is welcomed.
*
* @param baseUri base uri.
* @param configuration Jersey configuration.
* @param sslContext Netty {@link SslContext}.
* @return Netty channel instance.
* @throws ProcessingException when there is an issue with creating new container.
*/
public static Channel createHttp2Server(final URI baseUri, final ResourceConfig configuration, SslContext sslContext) throws ProcessingException {
final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
final EventLoopGroup workerGroup = new NioEventLoopGroup();
final NettyHttpContainer container = new NettyHttpContainer(configuration);
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new JerseyServerInitializer(baseUri, sslContext, container, true));
int port = getPort(baseUri);
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {
@Override
public void operationComplete(Future<? super Void> future) throws Exception {
container.getApplicationHandler().onShutdown(container);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
});
return ch;
} catch (InterruptedException e) {
throw new ProcessingException(e);
}
}
Aggregations