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