Search in sources :

Example 6 with MessageBodyWriter

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

the class MessageBodyFactory method addWriters.

private static void addWriters(final List<WriterModel> models, final Set<MessageBodyWriter> writers, final boolean custom) {
    for (final MessageBodyWriter provider : writers) {
        final List<MediaType> values = MediaTypes.createFrom(provider.getClass().getAnnotation(Produces.class));
        models.add(new WriterModel(provider, values, custom));
    }
}
Also used : WriterModel(org.glassfish.jersey.message.WriterModel) Produces(javax.ws.rs.Produces) MediaType(javax.ws.rs.core.MediaType) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 7 with MessageBodyWriter

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

the class MessageBodyFactory method _getMessageBodyWriter.

@SuppressWarnings("unchecked")
private <T> MessageBodyWriter<T> _getMessageBodyWriter(final Class<T> c, final Type t, final Annotation[] as, final MediaType mediaType, final MediaType lookup, final PropertiesDelegate propertiesDelegate) {
    final List<MessageBodyWriter> writers = writersCache.get(lookup);
    if (writers == null) {
        return null;
    }
    final TracingLogger tracingLogger = TracingLogger.getInstance(propertiesDelegate);
    MessageBodyWriter<T> selected = null;
    final Iterator<MessageBodyWriter> iterator = writers.iterator();
    while (iterator.hasNext()) {
        final MessageBodyWriter p = iterator.next();
        if (isWriteable(p, c, t, as, mediaType)) {
            selected = (MessageBodyWriter<T>) p;
            tracingLogger.log(MsgTraceEvent.MBW_SELECTED, selected);
            break;
        }
        tracingLogger.log(MsgTraceEvent.MBW_NOT_WRITEABLE, p);
    }
    if (tracingLogger.isLogEnabled(MsgTraceEvent.MBW_SKIPPED)) {
        while (iterator.hasNext()) {
            final MessageBodyWriter p = iterator.next();
            tracingLogger.log(MsgTraceEvent.MBW_SKIPPED, p);
        }
    }
    return selected;
}
Also used : MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 8 with MessageBodyWriter

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

the class ProviderBinderTest method testProvidersMbwInstance.

@Test
public void testProvidersMbwInstance() {
    InjectionManager injectionManager = Injections.createInjectionManager(initBinders());
    ProviderBinder providerBinder = new ProviderBinder(injectionManager);
    providerBinder.bindInstances(Collections.singleton(new MyProvider()));
    Set<MessageBodyWriter> providers = Providers.getCustomProviders(injectionManager, MessageBodyWriter.class);
    assertEquals(instancesOfType(MyProvider.class, providers).size(), 1);
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 9 with MessageBodyWriter

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

the class ProviderBinderTest method testProvidersMbw.

@Test
public void testProvidersMbw() {
    InjectionManager injectionManager = Injections.createInjectionManager(initBinders());
    ProviderBinder providerBinder = new ProviderBinder(injectionManager);
    providerBinder.bindClasses(Collections.singleton(MyProvider.class));
    Set<MessageBodyWriter> providers = Providers.getCustomProviders(injectionManager, MessageBodyWriter.class);
    final Collection<MyProvider> myProviders = instancesOfType(MyProvider.class, providers);
    assertEquals(1, myProviders.size());
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 10 with MessageBodyWriter

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

the class ProviderBinderTest method testServicesMbw.

@Test
public void testServicesMbw() {
    InjectionManager injectionManager = Injections.createInjectionManager(initBinders());
    Set<MessageBodyWriter> providers = Providers.getProviders(injectionManager, MessageBodyWriter.class);
    assertTrue(providers.size() > 0);
}
Also used : MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Aggregations

MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)25 MediaType (javax.ws.rs.core.MediaType)10 Test (org.junit.Test)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Writer (java.io.Writer)6 Type (java.lang.reflect.Type)6 IOException (java.io.IOException)5 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)5 List (java.util.List)4 Map (java.util.Map)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 OutputStream (java.io.OutputStream)3 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 Produces (javax.ws.rs.Produces)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 File (java.io.File)2