use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class JerseyInvocationTest method overrideHttpMethodBasedComplianceCheckTest.
/**
* Checks that presence of request entity fo HTTP DELETE method does not fail in Jersey.
* Instead, the request is propagated up to HttpURLConnection, where it fails with
* {@code ProtocolException}.
* <p/>
* See also JERSEY-1711.
*
* @see #overrideHttpMethodBasedComplianceCheckNegativeTest()
*/
@Test
public void overrideHttpMethodBasedComplianceCheckTest() {
final Client c1 = ClientBuilder.newClient().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
try {
c1.target("http://localhost:8080/myPath").request().method("DELETE", Entity.text("body"));
fail("ProcessingException expected.");
} catch (final ProcessingException ex) {
assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
}
final Client c2 = ClientBuilder.newClient();
try {
c2.target("http://localhost:8080/myPath").request().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true).method("DELETE", Entity.text("body"));
fail("ProcessingException expected.");
} catch (final ProcessingException ex) {
assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
}
final Client c3 = ClientBuilder.newClient().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, false);
try {
c3.target("http://localhost:8080/myPath").request().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true).method("DELETE", Entity.text("body"));
fail("ProcessingException expected.");
} catch (final ProcessingException ex) {
assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class EncodingFilterTest method testClosingClientResponseStreamRetrievedByResponseOnError.
/**
* Reproducer for JERSEY-2028.
*
* @see #testClosingClientResponseStreamRetrievedByValueOnError
*/
@Test
public void testClosingClientResponseStreamRetrievedByResponseOnError() {
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)));
final Response response = client.target(UriBuilder.fromUri("/").build()).request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("gzip", response.getHeaderString(CONTENT_ENCODING));
try {
response.readEntity(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 InboundMessageContext method bufferEntity.
/**
* Buffer the entity stream (if not empty).
*
* @return {@code true} if the entity input stream was successfully buffered.
* @throws javax.ws.rs.ProcessingException in case of an IO error.
*/
public boolean bufferEntity() throws ProcessingException {
entityContent.ensureNotClosed();
try {
if (entityContent.isBuffered() || !entityContent.hasContent()) {
return true;
}
final InputStream entityStream = entityContent.getWrappedStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ReaderWriter.writeTo(entityStream, baos);
} finally {
// Workaround for JRFCAF-1344: the underlying stream close() implementation may be thread-unsafe
// and as such the close() may result in an IOException at the socket input stream level,
// if the close() gets called at once from multiple threads somehow.
// We want to ignore these exceptions in the readEntity/bufferEntity operations though.
ReaderWriter.safelyClose(entityStream);
}
entityContent.setContent(new ByteArrayInputStream(baos.toByteArray()), true);
return true;
} catch (IOException ex) {
throw new ProcessingException(LocalizationMessages.MESSAGE_CONTENT_BUFFERING_FAILED(), ex);
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class ReaderInterceptorExecutor method proceed.
/**
* Starts the interceptor chain execution.
*
* @return an entity read from the stream.
*/
@Override
@SuppressWarnings("unchecked")
public Object proceed() throws IOException {
if (!interceptors.hasNext()) {
throw new ProcessingException(LocalizationMessages.ERROR_INTERCEPTOR_READER_PROCEED());
}
final ReaderInterceptor interceptor = interceptors.next();
traceBefore(interceptor, MsgTraceEvent.RI_BEFORE);
try {
return interceptor.aroundReadFrom(this);
} finally {
processedCount++;
traceAfter(interceptor, MsgTraceEvent.RI_AFTER);
}
}
use of javax.ws.rs.ProcessingException in project jersey by jersey.
the class MultivaluedParameterExtractorFactory method process.
@SuppressWarnings("unchecked")
private MultivaluedParameterExtractor<?> process(final ParamConverterFactory paramConverterFactory, final String defaultValue, final Class<?> rawType, final Type type, final Annotation[] annotations, final String parameterName) {
// Try to find a converter that support rawType and type at first.
// E.g. if someone writes a converter that support List<Integer> this approach should precede the next one.
ParamConverter<?> converter = paramConverterFactory.getConverter(rawType, type, annotations);
if (converter != null) {
try {
return new SingleValueExtractor(converter, parameterName, defaultValue);
} catch (final ExtractorException e) {
throw e;
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
}
}
// Check whether the rawType is the type of the collection supported.
if (rawType == List.class || rawType == Set.class || rawType == SortedSet.class) {
// Get the generic type of the list. If none is found default to String.
final List<ClassTypePair> typePairs = ReflectionHelper.getTypeArgumentAndClass(type);
final ClassTypePair typePair = (typePairs.size() == 1) ? typePairs.get(0) : null;
if (typePair == null || typePair.rawClass() == String.class) {
return StringCollectionExtractor.getInstance(rawType, parameterName, defaultValue);
} else {
converter = paramConverterFactory.getConverter(typePair.rawClass(), typePair.type(), annotations);
if (converter == null) {
return null;
}
try {
return CollectionExtractor.getInstance(rawType, converter, parameterName, defaultValue);
} catch (final ExtractorException e) {
throw e;
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
}
}
}
// Check primitive types.
if (rawType == String.class) {
return new SingleStringValueExtractor(parameterName, defaultValue);
} else if (rawType == Character.class) {
return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(rawType));
} else if (rawType.isPrimitive()) {
// Convert primitive to wrapper class
final Class<?> wrappedRaw = PrimitiveMapper.primitiveToClassMap.get(rawType);
if (wrappedRaw == null) {
// Primitive type not supported
return null;
}
if (wrappedRaw == Character.class) {
return new PrimitiveCharacterExtractor(parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
}
// Check for static valueOf(String)
final Method valueOf = AccessController.doPrivileged(ReflectionHelper.getValueOfStringMethodPA(wrappedRaw));
if (valueOf != null) {
try {
return new PrimitiveValueOfExtractor(valueOf, parameterName, defaultValue, PrimitiveMapper.primitiveToDefaultValueMap.get(wrappedRaw));
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.DEFAULT_COULD_NOT_PROCESS_METHOD(defaultValue, valueOf));
}
}
}
return null;
}
Aggregations