Search in sources :

Example 31 with WebApplicationException

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);
    }
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) WebApplicationException(javax.ws.rs.WebApplicationException) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) NotSupportedException(javax.ws.rs.NotSupportedException) ProcessingException(javax.ws.rs.ProcessingException)

Example 32 with WebApplicationException

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));
    }
}
Also used : Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 33 with WebApplicationException

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());
    }
}
Also used : Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) StreamSource(javax.xml.transform.stream.StreamSource) WebTarget(javax.ws.rs.client.WebTarget) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 34 with WebApplicationException

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) WebApplicationException(javax.ws.rs.WebApplicationException) SystemException(javax.transaction.SystemException) WebApplicationException(javax.ws.rs.WebApplicationException) NamingException(javax.naming.NamingException)

Example 35 with WebApplicationException

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());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST)

Aggregations

WebApplicationException (javax.ws.rs.WebApplicationException)276 Produces (javax.ws.rs.Produces)77 GET (javax.ws.rs.GET)71 Path (javax.ws.rs.Path)69 IOException (java.io.IOException)47 POST (javax.ws.rs.POST)47 Consumes (javax.ws.rs.Consumes)44 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)43 Response (javax.ws.rs.core.Response)30 MediaType (javax.ws.rs.core.MediaType)26 URI (java.net.URI)25 HashMap (java.util.HashMap)20 JSONObject (org.codehaus.jettison.json.JSONObject)20 Test (org.junit.Test)19 JSONException (org.codehaus.jettison.json.JSONException)18 ApiOperation (io.swagger.annotations.ApiOperation)17 ArrayList (java.util.ArrayList)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)15 List (java.util.List)14