Search in sources :

Example 6 with WriterInterceptor

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());
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 7 with WriterInterceptor

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());
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) IdentityHashMap(java.util.IdentityHashMap) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 8 with WriterInterceptor

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));
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) GenericType(javax.ws.rs.core.GenericType) WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntity(javax.ws.rs.core.GenericEntity)

Aggregations

WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)8 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 Test (org.junit.Test)4 OutputStream (java.io.OutputStream)2 Annotation (java.lang.annotation.Annotation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 IdentityHashMap (java.util.IdentityHashMap)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GenericEntity (javax.ws.rs.core.GenericEntity)1 GenericType (javax.ws.rs.core.GenericType)1 MediaType (javax.ws.rs.core.MediaType)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)1 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)1