Search in sources :

Example 1 with WriterInterceptor

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

the class CommonConfigTest method testProviderOrderManual.

@Test
public void testProviderOrderManual() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.register(MidPriorityProvider.class, 500);
    config.register(LowPriorityProvider.class, 20);
    config.register(HighPriorityProvider.class, 150);
    ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
    final Iterable<WriterInterceptor> allProviders = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<>());
    final Iterator<WriterInterceptor> iterator = allProviders.iterator();
    assertEquals(LowPriorityProvider.class, iterator.next().getClass());
    assertEquals(HighPriorityProvider.class, iterator.next().getClass());
    assertEquals(MidPriorityProvider.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 2 with WriterInterceptor

use of javax.ws.rs.ext.WriterInterceptor in project ddf by codice.

the class BodyWriter method writeBody.

<T> void writeBody(T o, Message outMessage, Class<?> cls, Type type, Annotation[] anns, OutputStream os) {
    if (o == null) {
        return;
    }
    @SuppressWarnings("unchecked") MultivaluedMap<String, Object> headers = (MultivaluedMap<String, Object>) outMessage.get(Message.PROTOCOL_HEADERS);
    @SuppressWarnings("unchecked") Class<T> theClass = (Class<T>) cls;
    MediaType contentType = JAXRSUtils.toMediaType(headers.getFirst("Content-Type").toString());
    List<WriterInterceptor> writers = ClientProviderFactory.getInstance(outMessage).createMessageBodyWriterInterceptor(theClass, type, anns, contentType, outMessage, null);
    if (writers != null) {
        try {
            JAXRSUtils.writeMessageBody(writers, o, theClass, type, anns, contentType, headers, outMessage);
            OutputStream realOs = outMessage.get(OutputStream.class);
            if (realOs != null) {
                realOs.flush();
            }
        } catch (Exception ex) {
            LOGGER.debug("Unable to write message body for final ECP response.");
        }
    } else {
        LOGGER.debug("No writers available for final ECP response");
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) OutputStream(java.io.OutputStream) MediaType(javax.ws.rs.core.MediaType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 3 with WriterInterceptor

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

the class WriterInterceptorExecutor method proceed.

/**
     * Starts the interceptor chain execution.
     */
@Override
@SuppressWarnings("unchecked")
public void proceed() throws IOException {
    final WriterInterceptor nextInterceptor = getNextInterceptor();
    if (nextInterceptor == null) {
        throw new ProcessingException(LocalizationMessages.ERROR_INTERCEPTOR_WRITER_PROCEED());
    }
    traceBefore(nextInterceptor, MsgTraceEvent.WI_BEFORE);
    try {
        nextInterceptor.aroundWriteTo(this);
    } finally {
        processedCount++;
        traceAfter(nextInterceptor, MsgTraceEvent.WI_AFTER);
    }
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with WriterInterceptor

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

the class ClientRequestTest method mockThrowing.

private ClientRequest mockThrowing(Exception exception) throws IOException {
    JerseyClient client = new JerseyClientBuilder().build();
    final ClientRequest request = new ClientRequest(URI.create("http://example.org"), client.getConfiguration(), new MapPropertiesDelegate());
    Mockito.doThrow(exception).when(workers).writeTo(any(), same(entityType.getRawType()), same(entityType.getType()), Mockito.<Annotation[]>any(), Mockito.<MediaType>any(), Mockito.<MultivaluedMap<String, Object>>any(), Mockito.<PropertiesDelegate>any(), Mockito.<OutputStream>any(), Mockito.<Iterable<WriterInterceptor>>any());
    return request;
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Annotation(java.lang.annotation.Annotation)

Example 5 with WriterInterceptor

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

the class CommonConfigTest method testProviderOrderSemiAutomatic.

@Test
public void testProviderOrderSemiAutomatic() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.register(MidPriorityProvider.class, 50);
    config.register(LowPriorityProvider.class, 2000);
    config.register(HighPriorityProvider.class);
    ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
    final Iterable<WriterInterceptor> allProviders = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<>());
    final Iterator<WriterInterceptor> iterator = allProviders.iterator();
    assertEquals(MidPriorityProvider.class, iterator.next().getClass());
    assertEquals(HighPriorityProvider.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)

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