Search in sources :

Example 1 with ExceptionMapper

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

the class JaxbStringReaderProviderTest method stringReaderDoesNotReadExternalDtds.

@Test
public void stringReaderDoesNotReadExternalDtds() {
    Provider<SAXParserFactory> saxParserFactoryProvider = new Provider<SAXParserFactory>() {

        final SaxParserFactoryInjectionProvider spf = new SaxParserFactoryInjectionProvider(new CommonConfig(RuntimeType.SERVER, ComponentBag.INCLUDE_ALL));

        @Override
        public SAXParserFactory get() {
            return spf.get();
        }
    };
    JaxbStringReaderProvider.RootElementProvider provider = new JaxbStringReaderProvider.RootElementProvider(saxParserFactoryProvider, new Providers() {

        @Override
        public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type) {
            return null;
        }

        @Override
        public <T> ContextResolver<T> getContextResolver(Class<T> contextType, MediaType mediaType) {
            return null;
        }
    });
    String content = "<!DOCTYPE x SYSTEM 'file:///no-such-file'> <rootObject/>";
    provider.getConverter(RootObject.class, null, null).fromString(content);
}
Also used : CommonConfig(org.glassfish.jersey.model.internal.CommonConfig) Providers(javax.ws.rs.ext.Providers) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) RuntimeType(javax.ws.rs.RuntimeType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MediaType(javax.ws.rs.core.MediaType) ContextResolver(javax.ws.rs.ext.ContextResolver) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 2 with ExceptionMapper

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

the class ExceptionMapperFactoryTest method testFindMappingExtendedExceptions.

/**
     * Test spec:
     * <p/>
     * setup:<br/>
     * - have two extended exception mappers, order matters<br/>
     * - both using the same generic type (RuntimeException)<br/>
     * - first mapper return isMappable true only to IllegalArgumentException<br/>
     * - second mapper return isMappable true only to IllegalStateException<br/>
     * <br/>
     * when:<br/>
     * - {@link ExceptionMapperFactory#findMapping(Throwable)} with IllegalArgumentException instance<br/>
     * <br/>
     * then:<br/>
     * - exception mapper factory returns IllegalArgumentExceptionMapper<br/>
     * <p/>
     * why:<br/>
     * - IllegalArgumentException has the same distance (1) for both exception mappers generic type (RuntimeException),
     * but IllegalArgumentException's isMappable return true, so it is the winner
     *
     * @throws Exception unexpected - if anything goes wrong, the test fails
     */
@Test
public void testFindMappingExtendedExceptions() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new ExtendedExceptionMappers());
    final ExceptionMapperFactory mapperFactory = new ExceptionMapperFactory(injectionManager);
    final ExceptionMapper mapper = mapperFactory.findMapping(new IllegalArgumentException());
    Assert.assertTrue("IllegalArgumentExceptionMapper should be returned", mapper instanceof IllegalArgumentExceptionMapper);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ExtendedExceptionMapper(org.glassfish.jersey.spi.ExtendedExceptionMapper) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 3 with ExceptionMapper

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

the class ExceptionMapperFactoryTest method testFindExtendedExceptions.

/**
     * Test spec: <br/>
     * <p/>
     * setup:<br/>
     * - have 2 extended mappers, order matters<br/>
     * - first mapper return isMappable true only to IllegalArgumentException<br/>
     * - second mapper return isMappable true only to IllegalStateException<br/>
     * <br/>
     * when:<br/>
     * - {@link ExceptionMapperFactory#find(Class)} invoked with IllegalArgumentException.class<br/>
     * then:<br/>
     * - exception mapper factory returns IllegalArgumentExceptionMapper<br/>
     * <p/>
     * why:<br/>
     * - both exception mappers have distance 1 to IllegalArgumentException, we don't have instance of the
     * IllegalArgumentException, so the isMappable check is not used and both are accepted, the later accepted is
     * the winner
     *
     * @throws Exception unexpected - if anything goes wrong, the test fails
     */
@Test
public void testFindExtendedExceptions() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new ExtendedExceptionMappers());
    final ExceptionMapperFactory mapperFactory = new ExceptionMapperFactory(injectionManager);
    final ExceptionMapper mapper = mapperFactory.find(IllegalArgumentException.class);
    Assert.assertTrue("IllegalStateExceptionMapper should be returned", mapper instanceof IllegalStateExceptionMapper);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ExtendedExceptionMapper(org.glassfish.jersey.spi.ExtendedExceptionMapper) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 4 with ExceptionMapper

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

the class MinijaxProviders method getExceptionMapper.

/**
 * Get an exception mapping provider for a particular class of exception.
 *
 * This is non-standard (i.e., not in the official JAX-RS spec), but there is evidence that
 * it will be in a future version:  https://github.com/jax-rs/api/issues/328 ("JAX_RS_SPEC-323").
 *
 * @param type
 * @param mediaType
 * @return
 */
@SuppressWarnings("unchecked")
public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(final Class<T> type, final MediaType mediaType) {
    for (final Class<? extends ExceptionMapper<?>> exceptionMapperClass : exceptionMappers.get(mediaType)) {
        final ParameterizedType parameterizedType = (ParameterizedType) exceptionMapperClass.getGenericInterfaces()[0];
        final Class<? extends Exception> exClass = (Class<? extends Exception>) parameterizedType.getActualTypeArguments()[0];
        if (exClass.isAssignableFrom(type)) {
            return (ExceptionMapper<T>) application.getResource(exceptionMapperClass);
        }
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper)

Example 5 with ExceptionMapper

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

the class TransactionExceptionMapper method toResponse.

@Override
public Response toResponse(final TransactionalException ejbException) {
    final Throwable cause = ejbException.getCause();
    if (cause != null) {
        final Class causeClass = cause.getClass();
        final ExceptionMapper exceptionMapper = providers.getExceptionMapper(causeClass);
        if (exceptionMapper == null) {
            return defaultResponse(cause);
        }
        return exceptionMapper.toResponse(cause);
    }
    return defaultResponse(ejbException);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper)

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