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