Search in sources :

Example 41 with ProcessingException

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

the class GrizzlyConnector method bufferEntity.

@SuppressWarnings("MagicNumber")
private byte[] bufferEntity(ClientRequest requestContext) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
    requestContext.setStreamProvider(new OutboundMessageContext.StreamProvider() {

        @Override
        public OutputStream getOutputStream(int contentLength) throws IOException {
            return baos;
        }
    });
    try {
        requestContext.writeEntity();
    } catch (IOException e) {
        throw new ProcessingException(LocalizationMessages.ERROR_BUFFERING_ENTITY(), e);
    }
    return baos.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 42 with ProcessingException

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

the class MessageBodyExceptionWrappingTest method testWrapping.

/**
     * Tests whether fail of message body writer causes throwing exception. Previously the
     * exception was not thrown and 500 status code was returned in the response.
     */
@Test
public void testWrapping() {
    WebTarget resource = target().path("test");
    StreamSource source = new StreamSource() {

        @Override
        public InputStream getInputStream() {
            throw new WebApplicationException(555);
        }
    };
    try {
        Response response = resource.request().post(Entity.entity(source, MediaType.TEXT_XML_TYPE));
        fail("Exception expected, instead response with " + response.getStatus() + " status has been returned.");
    } catch (ProcessingException e) {
        assertEquals(WebApplicationException.class, e.getCause().getClass());
        assertEquals(555, ((WebApplicationException) e.getCause()).getResponse().getStatus());
    }
}
Also used : Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) StreamSource(javax.xml.transform.stream.StreamSource) WebTarget(javax.ws.rs.client.WebTarget) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 43 with ProcessingException

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

the class SslConnectorHostnameVerifierTest method testHostnameVerifierApplied.

/**
     * Test to apply {@link HostnameVerifier} along with SSL in the predefined connectors
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testHostnameVerifierApplied() throws Exception {
    // Grizzly and Jetty connectors don't support Hostname Verification
    if (isExcluded(Arrays.asList(GrizzlyConnectorProvider.class, JettyConnectorProvider.class))) {
        return;
    }
    final Client client = ClientBuilder.newBuilder().withConfig(new ClientConfig().connectorProvider(connectorProvider)).register(HttpAuthenticationFeature.basic("user", "password")).hostnameVerifier(new CustomHostnameVerifier()).sslContext(getSslContext()).build();
    try {
        client.target(Server.BASE_URI).request().get(Response.class);
        fail("HostnameVerifier was not applied.");
    } catch (ProcessingException pex) {
        CustomHostnameVerifier.HostnameVerifierException hve = getHVE(pex);
        if (hve != null) {
            assertEquals(CustomHostnameVerifier.EX_VERIFIER_MESSAGE, hve.getMessage());
        } else {
            fail("Invalid wrapped exception.");
        }
    }
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) JettyConnectorProvider(org.glassfish.jersey.jetty.connector.JettyConnectorProvider) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 44 with ProcessingException

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

the class ResponseReadAndBufferEntityTest method testCloseUnreadResponseWithEntityStreamThatFailsToClose.

/**
     * This method tests behavior of input stream operations in case the underlying input stream throws an exception when closed.
     * Reproducer for JRFCAF-1344.
     * <p>
     * UC-3 : Try to close the response - underlying exception should be reported.
     */
@Test
public void testCloseUnreadResponseWithEntityStreamThatFailsToClose() throws Exception {
    final CorruptableInputStream entityStream = new CorruptableInputStream();
    final WebTarget target = target("response/corrupted");
    target.register(new ClientResponseFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
            responseContext.setEntityStream(entityStream);
        }
    });
    final Response response = target.request().buildGet().invoke();
    entityStream.setCorruptClose(true);
    try {
        response.close();
        fail("ProcessingException expected when closing the context and underlying stream throws an IOException.");
    } catch (ProcessingException pe) {
        assertThat(pe.getCause(), instanceOf(IOException.class));
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 45 with ProcessingException

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

the class ApacheConnector method apply.

@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
    final HttpUriRequest request = getUriHttpRequest(clientRequest);
    final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
    try {
        final CloseableHttpResponse response;
        final HttpClientContext context = HttpClientContext.create();
        if (preemptiveBasicAuth) {
            final AuthCache authCache = new BasicAuthCache();
            final BasicScheme basicScheme = new BasicScheme();
            authCache.put(getHost(request), basicScheme);
            context.setAuthCache(authCache);
        }
        response = client.execute(getHost(request), request, context);
        HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
        final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
        final ClientResponse responseContext = new ClientResponse(status, clientRequest);
        final List<URI> redirectLocations = context.getRedirectLocations();
        if (redirectLocations != null && !redirectLocations.isEmpty()) {
            responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
        }
        final Header[] respHeaders = response.getAllHeaders();
        final MultivaluedMap<String, String> headers = responseContext.getHeaders();
        for (final Header header : respHeaders) {
            final String headerName = header.getName();
            List<String> list = headers.get(headerName);
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(header.getValue());
            headers.put(headerName, list);
        }
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
                headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
            }
            final Header contentEncoding = entity.getContentEncoding();
            if (headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
                headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
            }
        }
        try {
            responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response)));
        } catch (final IOException e) {
            LOGGER.log(Level.SEVERE, null, e);
        }
        return responseContext;
    } catch (final Exception e) {
        throw new ProcessingException(e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ClientResponse(org.glassfish.jersey.client.ClientResponse) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) IOException(java.io.IOException) URI(java.net.URI) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) 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