Search in sources :

Example 76 with ProcessingException

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

the class EncodingFilterTest method testClosingClientResponseStreamRetrievedByValueOnError.

/**
     * Reproducer for JERSEY-2028.
     *
     * @see #testClosingClientResponseStreamRetrievedByResponseOnError
     */
@Test
public void testClosingClientResponseStreamRetrievedByValueOnError() {
    final TestInputStream responseStream = new TestInputStream();
    Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector() {

        @Override
        public ClientResponse apply(ClientRequest requestContext) throws ProcessingException {
            final ClientResponse responseContext = new ClientResponse(Response.Status.OK, requestContext);
            responseContext.header(CONTENT_ENCODING, "gzip");
            responseContext.setEntityStream(responseStream);
            return responseContext;
        }
    }).register(new EncodingFeature(GZipEncoder.class, DeflateEncoder.class)));
    try {
        client.target(UriBuilder.fromUri("/").build()).request().get(String.class);
        fail("Exception caused by invalid gzip stream expected.");
    } catch (ProcessingException ex) {
        assertTrue("Response input stream not closed when exception is thrown.", responseStream.isClosed);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientRequest(org.glassfish.jersey.client.ClientRequest) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 77 with ProcessingException

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

the class Main method startServer.

public static HttpServer startServer(String webRootPath) {
    final HttpServer server = new HttpServer();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            server.shutdownNow();
        }
    }));
    final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
    server.addListener(listener);
    final ServerConfiguration config = server.getServerConfiguration();
    // add handler for serving static content
    config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
    // add handler for serving JAX-RS resources
    config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH);
    try {
        // Start the server.
        server.start();
    } catch (Exception ex) {
        throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
    }
    return server;
}
Also used : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) GrizzlyHttpContainer(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer) ProcessingException(javax.ws.rs.ProcessingException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 78 with ProcessingException

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

the class Main method startServer.

public static HttpServer startServer(String webRootPath) {
    final HttpServer server = new HttpServer();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            server.shutdownNow();
        }
    }));
    final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
    server.addListener(listener);
    final ServerConfiguration config = server.getServerConfiguration();
    // add handler for serving static content
    config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
    // add handler for serving JAX-RS resources
    config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH);
    try {
        // Start the server.
        server.start();
    } catch (Exception ex) {
        throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
    }
    return server;
}
Also used : CLStaticHttpHandler(org.glassfish.grizzly.http.server.CLStaticHttpHandler) ServerConfiguration(org.glassfish.grizzly.http.server.ServerConfiguration) HttpServer(org.glassfish.grizzly.http.server.HttpServer) GrizzlyHttpContainer(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer) ProcessingException(javax.ws.rs.ProcessingException) IOException(java.io.IOException) NetworkListener(org.glassfish.grizzly.http.server.NetworkListener) ProcessingException(javax.ws.rs.ProcessingException)

Example 79 with ProcessingException

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

the class ViewableMessageBodyWriter method writeTo.

@Override
public void writeTo(final Viewable viewable, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
    try {
        final ResolvedViewable resolvedViewable = resolve(viewable);
        if (resolvedViewable == null) {
            final String message = LocalizationMessages.TEMPLATE_NAME_COULD_NOT_BE_RESOLVED(viewable.getTemplateName());
            throw new WebApplicationException(new ProcessingException(message), Response.Status.NOT_FOUND);
        }
        httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, resolvedViewable.getMediaType());
        resolvedViewable.writeTo(entityStream, httpHeaders);
    } catch (ViewableContextException vce) {
        throw new NotFoundException(vce);
    }
}
Also used : ResolvedViewable(org.glassfish.jersey.server.mvc.spi.ResolvedViewable) WebApplicationException(javax.ws.rs.WebApplicationException) NotFoundException(javax.ws.rs.NotFoundException) ViewableContextException(org.glassfish.jersey.server.mvc.spi.ViewableContextException) ProcessingException(javax.ws.rs.ProcessingException)

Example 80 with ProcessingException

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

the class BodyPart method getEntityAs.

/**
     * Returns the entity after appropriate conversion to the requested type. This is useful only when the containing
     * {@link MultiPart} instance has been received, which causes the {@code providers} property to have been set.
     *
     * @param clazz desired class into which the entity should be converted.
     * @return entity after appropriate conversion to the requested type.
     *
     * @throws ProcessingException if an IO error arises during reading an entity.
     * @throws IllegalArgumentException if no {@link MessageBodyReader} can be found to perform the requested conversion.
     * @throws IllegalStateException if this method is called when the {@code providers} property has not been set or when the
     * entity instance is not the unconverted content of the body part entity.
     */
public <T> T getEntityAs(final Class<T> clazz) {
    if (entity == null || !(entity instanceof BodyPartEntity)) {
        throw new IllegalStateException(LocalizationMessages.ENTITY_HAS_WRONG_TYPE());
    }
    if (clazz == BodyPartEntity.class) {
        return clazz.cast(entity);
    }
    final Annotation[] annotations = new Annotation[0];
    final MessageBodyReader<T> reader = messageBodyWorkers.getMessageBodyReader(clazz, clazz, annotations, mediaType);
    if (reader == null) {
        throw new IllegalArgumentException(LocalizationMessages.NO_AVAILABLE_MBR(clazz, mediaType));
    }
    try {
        return reader.readFrom(clazz, clazz, annotations, mediaType, headers, ((BodyPartEntity) entity).getInputStream());
    } catch (final IOException ioe) {
        throw new ProcessingException(LocalizationMessages.ERROR_READING_ENTITY(String.class), ioe);
    }
}
Also used : IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) 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