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);
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations