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);
}
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);
}
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());
}
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);
}
}
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);
}
Aggregations