use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class ResourceMethodInvoker method apply.
@Override
@SuppressWarnings("unchecked")
public ContainerResponse apply(final RequestProcessingContext processingContext) {
final ContainerRequest request = processingContext.request();
final Object resource = processingContext.routingContext().peekMatchedResource();
if (method.isSuspendDeclared() || method.isManagedAsyncDeclared()) {
if (!processingContext.asyncContext().suspend()) {
throw new ProcessingException(LocalizationMessages.ERROR_SUSPENDING_ASYNC_REQUEST());
}
}
if (method.isManagedAsyncDeclared()) {
processingContext.asyncContext().invokeManaged(new Producer<Response>() {
@Override
public Response call() {
final Response response = invoke(processingContext, resource);
if (method.isSuspendDeclared()) {
// we ignore any response returned from a method that injects AsyncResponse
return null;
}
return response;
}
});
// return null on current thread
return null;
} else {
// TODO replace with processing context factory method.
return new ContainerResponse(request, invoke(processingContext, resource));
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class WadlApplicationContextImpl method attachExternalGrammar.
/**
* Update the application object to include the generated grammar objects.
*/
private void attachExternalGrammar(final Application application, final ApplicationDescription applicationDescription, URI requestURI) {
try {
final String requestURIPath = requestURI.getPath();
if (requestURIPath.endsWith("application.wadl")) {
requestURI = UriBuilder.fromUri(requestURI).replacePath(requestURIPath.substring(0, requestURIPath.lastIndexOf('/') + 1)).build();
}
final String root = application.getResources().get(0).getBase();
final UriBuilder extendedPath = root != null ? UriBuilder.fromPath(root).path("/application.wadl/") : UriBuilder.fromPath("./application.wadl/");
final URI rootURI = root != null ? UriBuilder.fromPath(root).build() : null;
// Add a reference to this grammar
//
final Grammars grammars;
if (application.getGrammars() != null) {
LOGGER.info(LocalizationMessages.ERROR_WADL_GRAMMAR_ALREADY_CONTAINS());
grammars = application.getGrammars();
} else {
grammars = new Grammars();
application.setGrammars(grammars);
}
for (final String path : applicationDescription.getExternalMetadataKeys()) {
final URI schemaURI = extendedPath.clone().path(path).build();
final String schemaPath = rootURI != null ? requestURI.relativize(schemaURI).toString() : schemaURI.toString();
final Include include = new Include();
include.setHref(schemaPath);
final Doc doc = new Doc();
doc.setLang("en");
doc.setTitle("Generated");
include.getDoc().add(doc);
// Finally add to list
grammars.getInclude().add(include);
}
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_WADL_EXTERNAL_GRAMMAR(), e);
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class NettyConnector method apply.
@Override
public ClientResponse apply(ClientRequest jerseyRequest) {
final AtomicReference<ClientResponse> syncResponse = new AtomicReference<>(null);
final AtomicReference<Throwable> syncException = new AtomicReference<>(null);
try {
Future<?> resultFuture = apply(jerseyRequest, new AsyncConnectorCallback() {
@Override
public void response(ClientResponse response) {
syncResponse.set(response);
}
@Override
public void failure(Throwable failure) {
syncException.set(failure);
}
});
Integer timeout = ClientProperties.getValue(jerseyRequest.getConfiguration().getProperties(), ClientProperties.READ_TIMEOUT, 0);
if (timeout != null && timeout > 0) {
resultFuture.get(timeout, TimeUnit.MILLISECONDS);
} else {
resultFuture.get();
}
} catch (ExecutionException ex) {
Throwable e = ex.getCause() == null ? ex : ex.getCause();
throw new ProcessingException(e.getMessage(), e);
} catch (Exception ex) {
throw new ProcessingException(ex.getMessage(), ex);
}
Throwable throwable = syncException.get();
if (throwable == null) {
return syncResponse.get();
} else {
throw new RuntimeException(throwable);
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class JettyConnector method apply.
@Override
public Future<?> apply(final ClientRequest jerseyRequest, final AsyncConnectorCallback callback) {
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getStreamProvider(jerseyRequest);
if (entity != null) {
jettyRequest.content(entity);
}
final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
final Throwable failure;
try {
final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<ClientResponse>().whenComplete((clientResponse, throwable) -> {
if (throwable != null && throwable instanceof CancellationException) {
// take care of future cancellation
jettyRequest.abort(throwable);
}
});
final AtomicReference<ClientResponse> jerseyResponse = new AtomicReference<>();
final ByteBufferInputStream entityStream = new ByteBufferInputStream();
jettyRequest.send(new Response.Listener.Adapter() {
@Override
public void onHeaders(final Response jettyResponse) {
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName());
if (responseFuture.isDone()) {
if (!callbackInvoked.compareAndSet(false, true)) {
return;
}
}
final ClientResponse response = translateResponse(jerseyRequest, jettyResponse, entityStream);
jerseyResponse.set(response);
callback.response(response);
}
@Override
public void onContent(final Response jettyResponse, final ByteBuffer content) {
try {
entityStream.put(content);
} catch (final InterruptedException ex) {
final ProcessingException pe = new ProcessingException(ex);
entityStream.closeQueue(pe);
// try to complete the future with an exception
responseFuture.completeExceptionally(pe);
Thread.currentThread().interrupt();
}
}
@Override
public void onComplete(final Result result) {
entityStream.closeQueue();
// try to complete the future with the response only once truly done
responseFuture.complete(jerseyResponse.get());
}
@Override
public void onFailure(final Response response, final Throwable t) {
entityStream.closeQueue(t);
// try to complete the future with an exception
responseFuture.completeExceptionally(t);
if (callbackInvoked.compareAndSet(false, true)) {
callback.failure(t);
}
}
});
processContent(jerseyRequest, entity);
return responseFuture;
} catch (final Throwable t) {
failure = t;
}
if (callbackInvoked.compareAndSet(false, true)) {
callback.failure(failure);
}
CompletableFuture<Object> future = new CompletableFuture<>();
future.completeExceptionally(failure);
return future;
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class JettyConnector method apply.
@Override
public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException {
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getBytesProvider(jerseyRequest);
if (entity != null) {
jettyRequest.content(entity);
}
try {
final ContentResponse jettyResponse = jettyRequest.send();
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(), JettyConnector.this.getClass().getName());
final javax.ws.rs.core.Response.StatusType status = jettyResponse.getReason() == null ? Statuses.from(jettyResponse.getStatus()) : Statuses.from(jettyResponse.getStatus(), jettyResponse.getReason());
final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
processResponseHeaders(jettyResponse.getHeaders(), jerseyResponse);
try {
jerseyResponse.setEntityStream(new HttpClientResponseInputStream(jettyResponse));
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return jerseyResponse;
} catch (final Exception e) {
throw new ProcessingException(e);
}
}
Aggregations