use of javax.ws.rs.ext.MessageBodyWriter 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.MessageBodyWriter 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.MessageBodyWriter in project cxf by apache.
the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbrOnly.
@Test
public void testRegisterMbrMbwProviderAsMbrOnly() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
JAXBElementProvider<Book> customProvider = new JAXBElementProvider<Book>();
pf.registerUserProvider((Feature) context -> {
context.register(customProvider, MessageBodyReader.class);
return true;
});
MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertSame(reader, customProvider);
MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertTrue(writer instanceof JAXBElementProvider);
assertNotSame(writer, customProvider);
}
use of javax.ws.rs.ext.MessageBodyWriter in project cxf by apache.
the class BinaryDataProviderTest method testWriteTo.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testWriteTo() throws Exception {
MessageBodyWriter p = new BinaryDataProvider();
ByteArrayOutputStream os = new ByteArrayOutputStream();
p.writeTo(new byte[] { 'h', 'i' }, null, null, null, null, null, os);
assertTrue(Arrays.equals(new String("hi").getBytes(), os.toByteArray()));
ByteArrayInputStream is = new ByteArrayInputStream("hi".getBytes());
os = new ByteArrayOutputStream();
p.writeTo(is, null, null, null, null, null, os);
assertTrue(Arrays.equals(os.toByteArray(), new String("hi").getBytes()));
Reader r = new StringReader("hi");
os = new ByteArrayOutputStream();
p.writeTo(r, null, null, null, MediaType.valueOf("text/xml"), null, os);
assertTrue(Arrays.equals(os.toByteArray(), new String("hi").getBytes()));
os = new ByteArrayOutputStream();
p.writeTo(new StreamingOutputImpl(), null, null, null, MediaType.valueOf("text/xml"), null, os);
assertTrue(Arrays.equals(os.toByteArray(), new String("hi").getBytes()));
}
use of javax.ws.rs.ext.MessageBodyWriter in project cxf by apache.
the class PrimitiveTextProviderTest method testWriteBoolean.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testWriteBoolean() throws Exception {
MessageBodyWriter p = new PrimitiveTextProvider();
ByteArrayOutputStream os = new ByteArrayOutputStream();
p.writeTo(Boolean.TRUE, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
os = new ByteArrayOutputStream();
final boolean value = true;
p.writeTo(value, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
}
Aggregations