Search in sources :

Example 36 with ProcessingException

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));
    }
}
Also used : Response(javax.ws.rs.core.Response) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) ProcessingException(javax.ws.rs.ProcessingException)

Example 37 with ProcessingException

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);
    }
}
Also used : Include(com.sun.research.ws.wadl.Include) Doc(com.sun.research.ws.wadl.Doc) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Grammars(com.sun.research.ws.wadl.Grammars) JAXBException(javax.xml.bind.JAXBException) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 38 with ProcessingException

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) AsyncConnectorCallback(org.glassfish.jersey.client.spi.AsyncConnectorCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException)

Example 39 with ProcessingException

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;
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) ByteBufferInputStream(org.glassfish.jersey.internal.util.collection.ByteBufferInputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(org.eclipse.jetty.client.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) CancellationException(java.util.concurrent.CancellationException) ProcessingException(javax.ws.rs.ProcessingException)

Example 40 with ProcessingException

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Request(org.eclipse.jetty.client.api.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) IOException(java.io.IOException) CancellationException(java.util.concurrent.CancellationException) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(org.eclipse.jetty.client.api.Response) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

ProcessingException (javax.ws.rs.ProcessingException)91 Test (org.junit.Test)32 IOException (java.io.IOException)26 Response (javax.ws.rs.core.Response)20 WebTarget (javax.ws.rs.client.WebTarget)19 JerseyTest (org.glassfish.jersey.test.JerseyTest)16 WebApplicationException (javax.ws.rs.WebApplicationException)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Client (javax.ws.rs.client.Client)9 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ClientRequest (org.glassfish.jersey.client.ClientRequest)8 ClientResponse (org.glassfish.jersey.client.ClientResponse)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 URI (java.net.URI)6 NoContentException (javax.ws.rs.core.NoContentException)6 OutputStream (java.io.OutputStream)5 Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventSource (org.glassfish.jersey.media.sse.EventSource)5