Search in sources :

Example 56 with ProcessingException

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

the class JettyConnector method translateRequest.

private Request translateRequest(final ClientRequest clientRequest) {
    final HttpMethod method = HttpMethod.fromString(clientRequest.getMethod());
    if (method == null) {
        throw new ProcessingException(LocalizationMessages.METHOD_NOT_SUPPORTED(clientRequest.getMethod()));
    }
    final URI uri = clientRequest.getUri();
    final Request request = client.newRequest(uri);
    request.method(method);
    request.followRedirects(clientRequest.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
    final Object readTimeout = clientRequest.getConfiguration().getProperties().get(ClientProperties.READ_TIMEOUT);
    if (readTimeout != null && readTimeout instanceof Integer && (Integer) readTimeout > 0) {
        request.timeout((Integer) readTimeout, TimeUnit.MILLISECONDS);
    }
    return request;
}
Also used : Request(org.eclipse.jetty.client.api.Request) ClientRequest(org.glassfish.jersey.client.ClientRequest) URI(java.net.URI) HttpMethod(org.eclipse.jetty.http.HttpMethod) ProcessingException(javax.ws.rs.ProcessingException)

Example 57 with ProcessingException

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

the class TimeoutTest method testSlow.

@Test
public void testSlow() {
    final URI u = target().getUri();
    ClientConfig config = new ClientConfig().property(ClientProperties.READ_TIMEOUT, 1000);
    config.connectorProvider(new JettyConnectorProvider());
    Client c = ClientBuilder.newClient(config);
    WebTarget t = c.target(u);
    try {
        t.path("test/timeout").request().get();
        fail("Timeout expected.");
    } catch (ProcessingException e) {
        assertThat("Unexpected processing exception cause", e.getCause(), instanceOf(TimeoutException.class));
    } finally {
        c.close();
    }
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URI(java.net.URI) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 58 with ProcessingException

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

the class OutboundJaxrsResponse method bufferEntity.

@Override
public boolean bufferEntity() throws ProcessingException {
    if (closed) {
        throw new IllegalStateException(LocalizationMessages.RESPONSE_CLOSED());
    }
    if (!context.hasEntity() || !InputStream.class.isAssignableFrom(context.getEntityClass())) {
        return false;
    }
    if (buffered) {
        // already buffered
        return true;
    }
    final InputStream in = InputStream.class.cast(context.getEntity());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    } catch (IOException ex) {
        throw new ProcessingException(ex);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            throw new ProcessingException(ex);
        }
    }
    context.setEntity(new ByteArrayInputStream(out.toByteArray()));
    buffered = true;
    return true;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException)

Example 59 with ProcessingException

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

the class WriterInterceptorExecutor method proceed.

/**
     * Starts the interceptor chain execution.
     */
@Override
@SuppressWarnings("unchecked")
public void proceed() throws IOException {
    final WriterInterceptor nextInterceptor = getNextInterceptor();
    if (nextInterceptor == null) {
        throw new ProcessingException(LocalizationMessages.ERROR_INTERCEPTOR_WRITER_PROCEED());
    }
    traceBefore(nextInterceptor, MsgTraceEvent.WI_BEFORE);
    try {
        nextInterceptor.aroundWriteTo(this);
    } finally {
        processedCount++;
        traceAfter(nextInterceptor, MsgTraceEvent.WI_AFTER);
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ProcessingException(javax.ws.rs.ProcessingException)

Example 60 with ProcessingException

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

the class EntityInputStream method isEmpty.

/**
     * Check if the underlying entity stream is empty.
     * <p>
     * Note that the operation may need to block until a first byte (or EOF) is available in the stream.
     * </p>
     *
     * @return {@code true} if the entity stream is empty, {@code false} otherwise.
     */
public boolean isEmpty() {
    ensureNotClosed();
    final InputStream in = input;
    if (in == null) {
        return true;
    }
    try {
        // Try #markSupported first - #available on WLS waits until socked timeout is reached when chunked encoding is used.
        if (in.markSupported()) {
            in.mark(1);
            int i = in.read();
            in.reset();
            return i == -1;
        } else {
            try {
                if (in.available() > 0) {
                    return false;
                }
            } catch (IOException ioe) {
            // NOOP. Try other approaches as this can fail on WLS.
            }
            int b = in.read();
            if (b == -1) {
                return true;
            }
            PushbackInputStream pbis;
            if (in instanceof PushbackInputStream) {
                pbis = (PushbackInputStream) in;
            } else {
                pbis = new PushbackInputStream(in, 1);
                input = pbis;
            }
            pbis.unread(b);
            return false;
        }
    } catch (IOException ex) {
        throw new ProcessingException(ex);
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) 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