use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class ParameterValueHelper method getParameterValues.
/**
* Get the array of parameter values.
*
* @param valueProviders a list of value providers.
* @return array of parameter values provided by the value providers.
*/
public static Object[] getParameterValues(List<ParamValueFactoryWithSource<?>> valueProviders) {
final Object[] params = new Object[valueProviders.size()];
try {
int entityProviderIndex = -1;
int index = 0;
for (ParamValueFactoryWithSource<?> paramValProvider : valueProviders) {
// entity provider has to be called last; see JERSEY-2642
if (paramValProvider.getSource().equals(Parameter.Source.ENTITY)) {
entityProviderIndex = index++;
continue;
}
params[index++] = paramValProvider.get();
}
if (entityProviderIndex != -1) {
params[entityProviderIndex] = valueProviders.get(entityProviderIndex).get();
}
return params;
} catch (WebApplicationException e) {
throw e;
} catch (MessageBodyProviderNotFoundException e) {
throw new NotSupportedException(e);
} catch (ProcessingException e) {
throw e;
} catch (RuntimeException e) {
if (e.getCause() instanceof WebApplicationException) {
throw (WebApplicationException) e.getCause();
}
throw new MappableException("Exception obtaining parameters", e);
}
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class BasicClientTest method testSyncClientInvocationErrorResponse.
@Test
public void testSyncClientInvocationErrorResponse() throws InterruptedException, ExecutionException {
final WebTarget errorResource = target().path("resource").path("error");
Response r1 = errorResource.request().get();
assertEquals(404, r1.getStatus());
assertEquals("error", r1.readEntity(String.class));
try {
errorResource.request().get(String.class);
fail("ExecutionException expected.");
} catch (WebApplicationException ex) {
final Response r = ex.getResponse();
assertEquals(404, r.getStatus());
assertEquals("error", r.readEntity(String.class));
}
try {
target().path("resource").path("errorlist").request().get(new GenericType<List<String>>() {
});
fail("ExecutionException expected.");
} catch (WebApplicationException ex) {
final Response r = ex.getResponse();
assertEquals(404, r.getStatus());
assertEquals("error", r.readEntity(String.class));
}
}
use of javax.ws.rs.WebApplicationException 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.WebApplicationException in project jersey by jersey.
the class TransactionManager method manage.
public static void manage(Transactional t) {
UserTransaction utx = getUtx();
try {
utx.begin();
if (t.joinTransaction) {
t.em.joinTransaction();
}
t.transact();
utx.commit();
} catch (Exception e) {
try {
utx.rollback();
} catch (SystemException se) {
throw new WebApplicationException(se);
}
throw new WebApplicationException(e);
} finally {
t.em.close();
}
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class CdiTransactionalResource method transferMoney.
@POST
public String transferMoney(@QueryParam("from") long from, @QueryParam("to") long to, String amount) {
final Account toAccount = entityManager.find(Account.class, to);
if (toAccount != null) {
try {
toAccount.setBalance(toAccount.getBalance() + Long.decode(amount));
entityManager.merge(toAccount);
final Account fromAccount = entityManager.find(Account.class, from);
fromAccount.setBalance(fromAccount.getBalance() - Long.decode(amount));
if (fromAccount.getBalance() < 0) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Transaction failed. Not enough money on the funding account.").build());
}
entityManager.merge(fromAccount);
return "Transaction sucessful.";
} catch (Exception e) {
if (e instanceof WebApplicationException) {
throw (WebApplicationException) e;
} else {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Something bad happened.").build());
}
}
} else {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Target account not found.").build());
}
}
Aggregations