Search in sources :

Example 71 with ProcessingException

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

the class SimpleContainerFactory method _create.

private static SimpleServer _create(final URI address, final SSLContext context, final SimpleContainer container, final UnsafeValue<SocketProcessor, IOException> serverProvider) throws ProcessingException {
    if (address == null) {
        throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
    }
    final String scheme = address.getScheme();
    int defaultPort = org.glassfish.jersey.server.spi.Container.DEFAULT_HTTP_PORT;
    if (context == null) {
        if (!scheme.equalsIgnoreCase("http")) {
            throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
        }
    } else {
        if (!scheme.equalsIgnoreCase("https")) {
            throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
        }
        defaultPort = org.glassfish.jersey.server.spi.Container.DEFAULT_HTTPS_PORT;
    }
    int port = address.getPort();
    if (port == -1) {
        port = defaultPort;
    }
    final InetSocketAddress listen = new InetSocketAddress(port);
    final Connection connection;
    try {
        final SimpleTraceAnalyzer analyzer = new SimpleTraceAnalyzer();
        final SocketProcessor server = serverProvider.get();
        connection = new SocketConnection(server, analyzer);
        final SocketAddress socketAddr = connection.connect(listen, context);
        container.onServerStart();
        return new SimpleServer() {

            @Override
            public void close() throws IOException {
                container.onServerStop();
                analyzer.stop();
                connection.close();
            }

            @Override
            public int getPort() {
                return ((InetSocketAddress) socketAddr).getPort();
            }

            @Override
            public boolean isDebug() {
                return analyzer.isActive();
            }

            @Override
            public void setDebug(boolean enable) {
                if (enable) {
                    analyzer.start();
                } else {
                    analyzer.stop();
                }
            }
        };
    } catch (final IOException ex) {
        throw new ProcessingException(LocalizationMessages.ERROR_WHEN_CREATING_SERVER(), ex);
    }
}
Also used : SocketProcessor(org.simpleframework.transport.SocketProcessor) ContainerSocketProcessor(org.simpleframework.http.core.ContainerSocketProcessor) SocketConnection(org.simpleframework.transport.connect.SocketConnection) InetSocketAddress(java.net.InetSocketAddress) Connection(org.simpleframework.transport.connect.Connection) SocketConnection(org.simpleframework.transport.connect.SocketConnection) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ProcessingException(javax.ws.rs.ProcessingException)

Example 72 with ProcessingException

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

the class SubResourceLocatorRouter method getResource.

private Object getResource(final RequestProcessingContext context) {
    final Object resource = context.routingContext().peekMatchedResource();
    final Method handlingMethod = locatorModel.getInvocable().getHandlingMethod();
    final Object[] parameterValues = ParameterValueHelper.getParameterValues(valueProviders);
    context.triggerEvent(RequestEvent.Type.LOCATOR_MATCHED);
    final PrivilegedAction invokeMethodAction = new PrivilegedAction() {

        @Override
        public Object run() {
            try {
                return handlingMethod.invoke(resource, parameterValues);
            } catch (IllegalAccessException | IllegalArgumentException | UndeclaredThrowableException ex) {
                throw new ProcessingException(LocalizationMessages.ERROR_RESOURCE_JAVA_METHOD_INVOCATION(), ex);
            } catch (final InvocationTargetException ex) {
                final Throwable cause = ex.getCause();
                if (cause instanceof WebApplicationException) {
                    throw (WebApplicationException) cause;
                }
                // handle all exceptions as potentially mappable (incl. ProcessingException)
                throw new MappableException(cause);
            } catch (final Throwable t) {
                throw new ProcessingException(t);
            }
        }
    };
    final SecurityContext securityContext = context.request().getSecurityContext();
    return (securityContext instanceof SubjectSecurityContext) ? ((SubjectSecurityContext) securityContext).doAsSubject(invokeMethodAction) : invokeMethodAction.run();
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecurityContext(javax.ws.rs.core.SecurityContext) SubjectSecurityContext(org.glassfish.jersey.server.SubjectSecurityContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 73 with ProcessingException

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

the class ApacheConnector method getHttpEntity.

private HttpEntity getHttpEntity(final ClientRequest clientRequest, final boolean bufferingEnabled) {
    final Object entity = clientRequest.getEntity();
    if (entity == null) {
        return null;
    }
    final AbstractHttpEntity httpEntity = new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            if (bufferingEnabled) {
                final ByteArrayOutputStream buffer = new ByteArrayOutputStream(512);
                writeTo(buffer);
                return new ByteArrayInputStream(buffer.toByteArray());
            } else {
                return null;
            }
        }

        @Override
        public void writeTo(final OutputStream outputStream) throws IOException {
            clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

                @Override
                public OutputStream getOutputStream(final int contentLength) throws IOException {
                    return outputStream;
                }
            });
            clientRequest.writeEntity();
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
    if (bufferingEnabled) {
        try {
            return new BufferedHttpEntity(httpEntity);
        } catch (final IOException e) {
            throw new ProcessingException(LocalizationMessages.ERROR_BUFFERING_ENTITY(), e);
        }
    } else {
        return httpEntity;
    }
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ChunkedOutputStream(org.apache.http.impl.io.ChunkedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 74 with ProcessingException

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

the class CachingConnectorProviderTest method testCachingConnector.

@Test
public void testCachingConnector() {
    final ReferenceCountingNullConnector connectorProvider = new ReferenceCountingNullConnector();
    final CachingConnectorProvider cachingConnectorProvider = new CachingConnectorProvider(connectorProvider);
    final ClientConfig configuration = new ClientConfig().connectorProvider(cachingConnectorProvider).getConfiguration();
    Client client1 = ClientBuilder.newClient(configuration);
    try {
        client1.target(UriBuilder.fromUri("/").build()).request().get();
    } catch (ProcessingException ce) {
        assertEquals("test", ce.getMessage());
        assertEquals(1, connectorProvider.getCount());
    }
    try {
        client1.target(UriBuilder.fromUri("/").build()).request().async().get();
    } catch (ProcessingException ce) {
        assertEquals("test-async", ce.getMessage());
        assertEquals(1, connectorProvider.getCount());
    }
    Client client2 = ClientBuilder.newClient(configuration);
    try {
        client2.target(UriBuilder.fromUri("/").build()).request().get();
    } catch (ProcessingException ce) {
        assertEquals("test", ce.getMessage());
        assertEquals(1, connectorProvider.getCount());
    }
    try {
        client2.target(UriBuilder.fromUri("/").build()).request().async().get();
    } catch (ProcessingException ce) {
        assertEquals("test-async", ce.getMessage());
        assertEquals(1, connectorProvider.getCount());
    }
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 75 with ProcessingException

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

the class JerseyInvocationTest method failedCallbackTest.

@Test
public void failedCallbackTest() throws InterruptedException {
    final Invocation.Builder builder = ClientBuilder.newClient().target("http://localhost:888/").request();
    for (int i = 0; i < 1; i++) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicInteger ai = new AtomicInteger(0);
        final InvocationCallback<String> callback = new InvocationCallback<String>() {

            @Override
            public void completed(final String arg0) {
                try {
                    ai.set(ai.get() + 1);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void failed(final Throwable throwable) {
                try {
                    int result = 10;
                    if (throwable instanceof ProcessingException) {
                        result += 100;
                    }
                    final Throwable ioe = throwable.getCause();
                    if (ioe instanceof IOException) {
                        result += 1000;
                    }
                    ai.set(ai.get() + result);
                } finally {
                    latch.countDown();
                }
            }
        };
        final Invocation invocation = builder.buildGet();
        final Future<String> future = invocation.submit(callback);
        try {
            future.get();
            fail("future.get() should have failed.");
        } catch (final ExecutionException e) {
            final Throwable pe = e.getCause();
            assertTrue("Execution exception cause is not a ProcessingException: " + pe.toString(), pe instanceof ProcessingException);
            final Throwable ioe = pe.getCause();
            assertTrue("Execution exception cause is not an IOException: " + ioe.toString(), ioe instanceof IOException);
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
        latch.await(1, TimeUnit.SECONDS);
        assertEquals(1110, ai.get());
    }
}
Also used : Invocation(javax.ws.rs.client.Invocation) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationCallback(javax.ws.rs.client.InvocationCallback) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

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