Search in sources :

Example 1 with MessageBodyReader

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

the class ProviderBinderTest method testServicesMbr.

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

Example 2 with MessageBodyReader

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

the class ProviderBinderTest method testServicesNotEmpty.

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

Example 3 with MessageBodyReader

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

the class ApplicationHandler method logApplicationInitConfiguration.

private static void logApplicationInitConfiguration(final InjectionManager injectionManager, final ResourceBag resourceBag, final ProcessingProviders processingProviders) {
    if (!LOGGER.isLoggable(Level.CONFIG)) {
        return;
    }
    final StringBuilder sb = new StringBuilder(LocalizationMessages.LOGGING_APPLICATION_INITIALIZED()).append('\n');
    final List<Resource> rootResourceClasses = resourceBag.getRootResources();
    if (!rootResourceClasses.isEmpty()) {
        sb.append(LocalizationMessages.LOGGING_ROOT_RESOURCE_CLASSES()).append(":");
        for (final Resource r : rootResourceClasses) {
            for (final Class clazz : r.getHandlerClasses()) {
                sb.append('\n').append("  ").append(clazz.getName());
            }
        }
    }
    sb.append('\n');
    final Set<MessageBodyReader> messageBodyReaders;
    final Set<MessageBodyWriter> messageBodyWriters;
    if (LOGGER.isLoggable(Level.FINE)) {
        Spliterator<MessageBodyReader> mbrSpliterator = Providers.getAllProviders(injectionManager, MessageBodyReader.class).spliterator();
        messageBodyReaders = StreamSupport.stream(mbrSpliterator, false).collect(Collectors.toSet());
        Spliterator<MessageBodyWriter> mbwSpliterator = Providers.getAllProviders(injectionManager, MessageBodyWriter.class).spliterator();
        messageBodyWriters = StreamSupport.stream(mbwSpliterator, false).collect(Collectors.toSet());
    } else {
        messageBodyReaders = Providers.getCustomProviders(injectionManager, MessageBodyReader.class);
        messageBodyWriters = Providers.getCustomProviders(injectionManager, MessageBodyWriter.class);
    }
    printProviders(LocalizationMessages.LOGGING_PRE_MATCH_FILTERS(), processingProviders.getPreMatchFilters(), sb);
    printProviders(LocalizationMessages.LOGGING_GLOBAL_REQUEST_FILTERS(), processingProviders.getGlobalRequestFilters(), sb);
    printProviders(LocalizationMessages.LOGGING_GLOBAL_RESPONSE_FILTERS(), processingProviders.getGlobalResponseFilters(), sb);
    printProviders(LocalizationMessages.LOGGING_GLOBAL_READER_INTERCEPTORS(), processingProviders.getGlobalReaderInterceptors(), sb);
    printProviders(LocalizationMessages.LOGGING_GLOBAL_WRITER_INTERCEPTORS(), processingProviders.getGlobalWriterInterceptors(), sb);
    printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_REQUEST_FILTERS(), processingProviders.getNameBoundRequestFilters(), sb);
    printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_RESPONSE_FILTERS(), processingProviders.getNameBoundResponseFilters(), sb);
    printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_READER_INTERCEPTORS(), processingProviders.getNameBoundReaderInterceptors(), sb);
    printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_WRITER_INTERCEPTORS(), processingProviders.getNameBoundWriterInterceptors(), sb);
    printProviders(LocalizationMessages.LOGGING_DYNAMIC_FEATURES(), processingProviders.getDynamicFeatures(), sb);
    printProviders(LocalizationMessages.LOGGING_MESSAGE_BODY_READERS(), messageBodyReaders.stream().map(new WorkersToStringTransform<>()).collect(Collectors.toList()), sb);
    printProviders(LocalizationMessages.LOGGING_MESSAGE_BODY_WRITERS(), messageBodyWriters.stream().map(new WorkersToStringTransform<>()).collect(Collectors.toList()), sb);
    LOGGER.log(Level.CONFIG, sb.toString());
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) Resource(org.glassfish.jersey.server.model.Resource) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 4 with MessageBodyReader

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

the class RequestUtil method getEntityParameters.

/**
     * Returns the form parameters from a request entity as a multi-valued map.
     * If the request does not have a POST method, or the media type is not
     * x-www-form-urlencoded, then null is returned.
     *
     * @param request the client request containing the entity to extract parameters from.
     * @return a {@link javax.ws.rs.core.MultivaluedMap} containing the entity form parameters.
     */
@SuppressWarnings("unchecked")
public static MultivaluedMap<String, String> getEntityParameters(ClientRequestContext request, MessageBodyWorkers messageBodyWorkers) {
    Object entity = request.getEntity();
    String method = request.getMethod();
    MediaType mediaType = request.getMediaType();
    // no entity, not a post or not x-www-form-urlencoded: return empty map
    if (entity == null || method == null || !HttpMethod.POST.equalsIgnoreCase(method) || mediaType == null || !mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
        return new MultivaluedHashMap<String, String>();
    }
    // it's ready to go if already expressed as a multi-valued map
    if (entity instanceof MultivaluedMap) {
        return (MultivaluedMap<String, String>) entity;
    }
    Type entityType = entity.getClass();
    // if the entity is generic, get specific type and class
    if (entity instanceof GenericEntity) {
        final GenericEntity generic = (GenericEntity) entity;
        // overwrite
        entityType = generic.getType();
        entity = generic.getEntity();
    }
    final Class entityClass = entity.getClass();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MessageBodyWriter writer = messageBodyWorkers.getMessageBodyWriter(entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    try {
        writer.writeTo(entity, entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, out);
    } catch (WebApplicationException wae) {
        throw new IllegalStateException(wae);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    try {
        return (MultivaluedMap<String, String>) reader.readFrom(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, in);
    } catch (IOException ioe) {
        throw new IllegalStateException(ioe);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntity(javax.ws.rs.core.GenericEntity) MediaType(javax.ws.rs.core.MediaType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 5 with MessageBodyReader

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

the class InboundEvent method readData.

/**
     * Read event data as a given generic type.
     *
     * @param type      generic type to be used for event data de-serialization.
     * @param mediaType {@link MediaType media type} to be used for event data de-serialization.
     * @return event data de-serialized as an instance of a given type.
     * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
     * @since 2.3
     */
public <T> T readData(GenericType<T> type, MediaType mediaType) {
    final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType;
    final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType);
    if (reader == null) {
        throw new MessageBodyProviderNotFoundException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND());
    }
    return readAndCast(type, effectiveMediaType, reader);
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) MediaType(javax.ws.rs.core.MediaType)

Aggregations

MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)10 Test (org.junit.Test)5 MediaType (javax.ws.rs.core.MediaType)4 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)3 Type (java.lang.reflect.Type)2 ProviderBinder (org.glassfish.jersey.internal.inject.ProviderBinder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Provider (javax.inject.Provider)1 Consumes (javax.ws.rs.Consumes)1 RuntimeType (javax.ws.rs.RuntimeType)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GenericEntity (javax.ws.rs.core.GenericEntity)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ContextResolver (javax.ws.rs.ext.ContextResolver)1 ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)1