Search in sources :

Example 6 with ExceptionMapper

use of javax.ws.rs.ext.ExceptionMapper in project cxf by apache.

the class ProviderFactoryTest method testRegisterFeatureInFeature.

@Test
public void testRegisterFeatureInFeature() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    final Object provider = new WebApplicationExceptionMapper();
    pf.registerUserProvider((Feature) context -> {
        context.register((Feature) context2 -> {
            context2.register(provider);
            return true;
        });
        return true;
    });
    ExceptionMapper<WebApplicationException> em = pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
    assertSame(provider, em);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Assert.assertNotSame(org.junit.Assert.assertNotSame) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) RuntimeType(javax.ws.rs.RuntimeType) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) Assert.assertFalse(org.junit.Assert.assertFalse) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ConstrainedTo(javax.ws.rs.ConstrainedTo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) Assert.assertNotNull(org.junit.Assert.assertNotNull) JAXBElement(javax.xml.bind.JAXBElement) IOUtils(org.apache.cxf.helpers.IOUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Assert.assertNull(org.junit.Assert.assertNull) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) WebApplicationException(javax.ws.rs.WebApplicationException) Feature(javax.ws.rs.core.Feature) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 7 with ExceptionMapper

use of javax.ws.rs.ext.ExceptionMapper in project minijax by minijax.

the class MinijaxApplication method toResponse.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Response toResponse(final MinijaxRequestContext context, final Exception ex) {
    final MinijaxResourceMethod rm = context.getResourceMethod();
    final List<MediaType> mediaTypes;
    if (rm != null) {
        mediaTypes = rm.getProduces();
    } else {
        mediaTypes = context.getAcceptableMediaTypes();
    }
    for (final MediaType mediaType : mediaTypes) {
        final ExceptionMapper mapper = providers.getExceptionMapper(ex.getClass(), mediaType);
        if (mapper != null) {
            return mapper.toResponse(ex);
        }
    }
    return ExceptionUtils.toWebAppException(ex).getResponse();
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MediaType(javax.ws.rs.core.MediaType)

Example 8 with ExceptionMapper

use of javax.ws.rs.ext.ExceptionMapper in project tomee by apache.

the class EJBExceptionMapper method toResponse.

@Override
public Response toResponse(final EJBException ejbException) {
    final Exception cause = ejbException.getCausedByException();
    if (cause != null) {
        final Class causeClass = cause.getClass();
        final ExceptionMapper exceptionMapper = providers.getExceptionMapper(causeClass);
        if (exceptionMapper == null) {
            return defaultResponse(cause);
        }
        return exceptionMapper.toResponse(cause);
    } else if (EJBAccessException.class.isInstance(ejbException)) {
        return Response.status(Response.Status.FORBIDDEN).build();
    }
    return defaultResponse(ejbException);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) EJBException(javax.ejb.EJBException) EJBAccessException(javax.ejb.EJBAccessException) EJBAccessException(javax.ejb.EJBAccessException)

Example 9 with ExceptionMapper

use of javax.ws.rs.ext.ExceptionMapper in project cxf by apache.

the class ProviderFactoryTest method testRegisterInFeature.

@Test
public void testRegisterInFeature() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    final Object provider = new WebApplicationExceptionMapper();
    pf.registerUserProvider((Feature) context -> {
        context.register(provider);
        return true;
    });
    ExceptionMapper<WebApplicationException> em = pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
    assertSame(provider, em);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Assert.assertNotSame(org.junit.Assert.assertNotSame) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) RuntimeType(javax.ws.rs.RuntimeType) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) Assert.assertFalse(org.junit.Assert.assertFalse) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ConstrainedTo(javax.ws.rs.ConstrainedTo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) Assert.assertNotNull(org.junit.Assert.assertNotNull) JAXBElement(javax.xml.bind.JAXBElement) IOUtils(org.apache.cxf.helpers.IOUtils) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Assert.assertNull(org.junit.Assert.assertNull) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) WebApplicationException(javax.ws.rs.WebApplicationException) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)9 MediaType (javax.ws.rs.core.MediaType)4 Test (org.junit.Test)4 Annotation (java.lang.annotation.Annotation)3 Type (java.lang.reflect.Type)3 RuntimeType (javax.ws.rs.RuntimeType)3 ContextResolver (javax.ws.rs.ext.ContextResolver)3 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)3 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)3 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 Iterator (java.util.Iterator)2 List (java.util.List)2