Search in sources :

Example 51 with ProcessingException

use of javax.ws.rs.ProcessingException in project metrics by dropwizard.

the class SingletonMetricsExceptionMeteredPerClassJerseyTest method exceptionMeteredMethodsAreExceptionMetered.

@Test
public void exceptionMeteredMethodsAreExceptionMetered() {
    final Meter meter = registry.meter(name(InstrumentedResourceExceptionMeteredPerClass.class, "exceptionMetered", "exceptions"));
    assertThat(target("exception-metered").request().get(String.class)).isEqualTo("fuh");
    assertThat(meter.getCount()).isZero();
    try {
        target("exception-metered").queryParam("splode", true).request().get(String.class);
        failBecauseExceptionWasNotThrown(ProcessingException.class);
    } catch (ProcessingException e) {
        assertThat(e.getCause()).isInstanceOf(IOException.class);
    }
    assertThat(meter.getCount()).isEqualTo(1);
}
Also used : Meter(com.codahale.metrics.Meter) InstrumentedResourceExceptionMeteredPerClass(com.codahale.metrics.jersey2.resources.InstrumentedResourceExceptionMeteredPerClass) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 52 with ProcessingException

use of javax.ws.rs.ProcessingException in project dropwizard by dropwizard.

the class DropwizardApacheConnectorTest method connect_timeout_override_changes_how_long_it_takes_for_a_connection_to_timeout.

/**
     * <p>In first assertion we prove, that a request takes no longer than:
     * <em>request_time < connect_timeout + error_margin</em> (1)</p>
     * <p/>
     * </p>In the second we show that if we set <b>connect_timeout</b> to
     * <b>set_connect_timeout + increase + error_margin</b> then
     * <em>request_time > connect_timeout + increase + error_margin</em> (2)</p>
     * <p/>
     * <p>Now, (1) and (2) can hold at the same time if then connect_timeout update was successful.</p>
     */
@Test
public void connect_timeout_override_changes_how_long_it_takes_for_a_connection_to_timeout() {
    // before override
    WebTarget target = client.target(NON_ROUTABLE_ADDRESS);
    //This can't be tested without a real connection
    try {
        target.request().get(Response.class);
    } catch (ProcessingException e) {
        if (e.getCause() instanceof HttpHostConnectException) {
            return;
        }
    }
    assertThatConnectionTimeoutFor(target).isLessThan(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + ERROR_MARGIN_IN_MILLIS);
    // after override
    final int newTimeout = DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + INCREASE_IN_MILLIS + ERROR_MARGIN_IN_MILLIS;
    final WebTarget newTarget = target.property(ClientProperties.CONNECT_TIMEOUT, newTimeout);
    assertThatConnectionTimeoutFor(newTarget).isGreaterThan(newTimeout);
}
Also used : HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) WebTarget(javax.ws.rs.client.WebTarget) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 53 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class GrizzlyConnector method apply.

@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
    final Request connectorRequest = translate(request);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
    final ByteBufferInputStream entityStream = new ByteBufferInputStream();
    final AtomicBoolean callbackInvoked = new AtomicBoolean(false);
    Throwable failure;
    try {
        return grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {

            private volatile HttpResponseStatus status = null;

            @Override
            public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
                status = responseStatus;
                return STATE.CONTINUE;
            }

            @Override
            public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                if (!callbackInvoked.compareAndSet(false, true)) {
                    return STATE.ABORT;
                }
                HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
                // hand-off to grizzly's application thread pool for response processing
                processResponse(new Runnable() {

                    @Override
                    public void run() {
                        callback.response(translate(request, status, headers, entityStream));
                    }
                });
                return STATE.CONTINUE;
            }

            @Override
            public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                entityStream.put(bodyPart.getBodyByteBuffer());
                return STATE.CONTINUE;
            }

            @Override
            public Void onCompleted() throws Exception {
                entityStream.closeQueue();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                entityStream.closeQueue(t);
                if (callbackInvoked.compareAndSet(false, true)) {
                    t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
                    callback.failure(t);
                }
            }
        });
    } catch (Throwable t) {
        failure = t;
    }
    if (callbackInvoked.compareAndSet(false, true)) {
        callback.failure(failure);
    }
    CompletableFuture<Object> future = new CompletableFuture<>();
    future.completeExceptionally(failure);
    return future;
}
Also used : HttpResponseHeaders(com.ning.http.client.HttpResponseHeaders) HttpResponseStatus(com.ning.http.client.HttpResponseStatus) Request(com.ning.http.client.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) ByteBufferInputStream(org.glassfish.jersey.internal.util.collection.ByteBufferInputStream) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) ProcessingException(javax.ws.rs.ProcessingException)

Example 54 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class GrizzlyConnector method apply.

/**
     * Sends the {@link javax.ws.rs.core.Request} via Grizzly transport and returns the {@link javax.ws.rs.core.Response}.
     *
     * @param request Jersey client request to be sent.
     * @return received response.
     */
@Override
public ClientResponse apply(final ClientRequest request) {
    final Request connectorRequest = translate(request);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(request.getHeaders(), connectorRequest);
    final CompletableFuture<ClientResponse> responseFuture = new CompletableFuture<>();
    final ByteBufferInputStream entityStream = new ByteBufferInputStream();
    final AtomicBoolean futureSet = new AtomicBoolean(false);
    try {
        grizzlyClient.executeRequest(connectorRequest, new AsyncHandler<Void>() {

            private volatile HttpResponseStatus status = null;

            @Override
            public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
                status = responseStatus;
                return STATE.CONTINUE;
            }

            @Override
            public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                if (!futureSet.compareAndSet(false, true)) {
                    return STATE.ABORT;
                }
                HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, request.getHeaders(), GrizzlyConnector.this.getClass().getName());
                responseFuture.complete(translate(request, this.status, headers, entityStream));
                return STATE.CONTINUE;
            }

            @Override
            public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                entityStream.put(bodyPart.getBodyByteBuffer());
                return STATE.CONTINUE;
            }

            @Override
            public Void onCompleted() throws Exception {
                entityStream.closeQueue();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                entityStream.closeQueue(t);
                if (futureSet.compareAndSet(false, true)) {
                    t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t;
                    responseFuture.completeExceptionally(t);
                }
            }
        });
        return responseFuture.get();
    } catch (ExecutionException ex) {
        Throwable e = ex.getCause() == null ? ex : ex.getCause();
        throw new ProcessingException(e.getMessage(), e);
    } catch (InterruptedException ex) {
        throw new ProcessingException(ex.getMessage(), ex);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) HttpResponseHeaders(com.ning.http.client.HttpResponseHeaders) HttpResponseStatus(com.ning.http.client.HttpResponseStatus) Request(com.ning.http.client.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) ByteBufferInputStream(org.glassfish.jersey.internal.util.collection.ByteBufferInputStream) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionException(java.util.concurrent.ExecutionException) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) ProcessingException(javax.ws.rs.ProcessingException)

Example 55 with ProcessingException

use of javax.ws.rs.ProcessingException in project jersey by jersey.

the class JettyConnector method getBytesProvider.

private ContentProvider getBytesProvider(final ClientRequest clientRequest) {
    final Object entity = clientRequest.getEntity();
    if (entity == null) {
        return null;
    }
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

        @Override
        public OutputStream getOutputStream(final int contentLength) throws IOException {
            return outputStream;
        }
    });
    try {
        clientRequest.writeEntity();
    } catch (final IOException e) {
        throw new ProcessingException("Failed to write request entity.", e);
    }
    return new BytesContentProvider(outputStream.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) 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