use of javax.ws.rs.ext.WriterInterceptor in project jersey by jersey.
the class CommonConfigTest method testProviderOrderAutomatic.
@Test
public void testProviderOrderAutomatic() throws Exception {
final InjectionManager injectionManager = Injections.createInjectionManager();
config.register(MidPriorityProvider.class);
config.register(LowPriorityProvider.class);
config.register(HighPriorityProvider.class);
ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
final Iterable<WriterInterceptor> allProviders = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<WriterInterceptor>());
final Iterator<WriterInterceptor> iterator = allProviders.iterator();
assertEquals(HighPriorityProvider.class, iterator.next().getClass());
assertEquals(MidPriorityProvider.class, iterator.next().getClass());
assertEquals(LowPriorityProvider.class, iterator.next().getClass());
assertFalse(iterator.hasNext());
}
use of javax.ws.rs.ext.WriterInterceptor in project jersey by jersey.
the class CommonConfigTest method testProviderOrderDifForContracts.
@Test
@SuppressWarnings("unchecked")
public void testProviderOrderDifForContracts() throws Exception {
final Map<Class<?>, Integer> contracts = new IdentityHashMap<Class<?>, Integer>();
contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
contracts.put(ReaderInterceptor.class, 2000);
config.register(MidPriorityProvider.class, contracts);
contracts.clear();
contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
contracts.put(ReaderInterceptor.class, 1000);
config.register(LowPriorityProvider.class, contracts);
contracts.clear();
contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
contracts.put(ReaderInterceptor.class, 3000);
config.register(HighPriorityProvider.class, contracts);
contracts.clear();
final InjectionManager injectionManager = Injections.createInjectionManager();
ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
final Iterable<WriterInterceptor> writerInterceptors = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<WriterInterceptor>());
final Iterator<WriterInterceptor> writerIterator = writerInterceptors.iterator();
assertEquals(HighPriorityProvider.class, writerIterator.next().getClass());
assertEquals(MidPriorityProvider.class, writerIterator.next().getClass());
assertEquals(LowPriorityProvider.class, writerIterator.next().getClass());
assertFalse(writerIterator.hasNext());
final Iterable<ReaderInterceptor> readerInterceptors = Providers.getAllProviders(injectionManager, ReaderInterceptor.class, new RankedComparator<ReaderInterceptor>());
final Iterator<ReaderInterceptor> readerIterator = readerInterceptors.iterator();
assertEquals(LowPriorityProvider.class, readerIterator.next().getClass());
assertEquals(MidPriorityProvider.class, readerIterator.next().getClass());
assertEquals(HighPriorityProvider.class, readerIterator.next().getClass());
assertFalse(readerIterator.hasNext());
}
use of javax.ws.rs.ext.WriterInterceptor in project jersey by jersey.
the class TestContainerRequest method setEntity.
void setEntity(final Object requestEntity, final MessageBodyWorkers workers) {
final Object entity;
final GenericType entityType;
if (requestEntity instanceof GenericEntity) {
entity = ((GenericEntity) requestEntity).getEntity();
entityType = new GenericType(((GenericEntity) requestEntity).getType());
} else {
entity = requestEntity;
entityType = new GenericType(requestEntity.getClass());
}
final byte[] entityBytes;
if (entity != null) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(), new Annotation[0], getMediaType(), new MultivaluedHashMap<String, Object>(getHeaders()), getPropertiesDelegate(), output, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
LOGGER.log(Level.SEVERE, "Transforming entity to input stream failed.", ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
}
}
}
entityBytes = output.toByteArray();
} else {
entityBytes = new byte[0];
}
setEntity(new ByteArrayInputStream(entityBytes));
}
Aggregations