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