Search in sources :

Example 11 with ProcessingException

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)));
    }
}
Also used : ProtocolException(java.net.ProtocolException) Client(javax.ws.rs.client.Client) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 12 with ProcessingException

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) 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 13 with ProcessingException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException)

Example 14 with ProcessingException

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);
    }
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ProcessingException(javax.ws.rs.ProcessingException)

Example 15 with ProcessingException

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;
}
Also used : ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Method(java.lang.reflect.Method) SortedSet(java.util.SortedSet) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) 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