use of io.helidon.common.http.MediaType in project helidon by oracle.
the class JacksonBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
AtomicBoolean first = new AtomicBoolean(true);
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset());
return Multi.create(publisher).flatMap(objectToChunks).flatMap(it -> {
if (first.getAndSet(false)) {
// first record, do not prepend a comma
return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
} else {
// any subsequent record starts with a comma
return Multi.just(DataChunk.create(COMMA_BYTES), it);
}
}).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
use of io.helidon.common.http.MediaType in project helidon by oracle.
the class JacksonEsBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(TEXT_EVENT_STREAM_JSON);
context.contentType(contentType);
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset());
return Multi.create(publisher).flatMap(objectToChunks).flatMap(chunk -> Multi.just(DataChunk.create(DATA), chunk, DataChunk.create(NL)));
}
use of io.helidon.common.http.MediaType in project helidon by oracle.
the class BodyPartBodyStreamReader method read.
@Override
@SuppressWarnings("unchecked")
public <U extends ReadableBodyPart> Publisher<U> read(Publisher<DataChunk> publisher, GenericType<U> type, MessageBodyReaderContext context) {
String boundary = null;
MediaType contentType = context.contentType().orElse(null);
if (contentType != null) {
boundary = contentType.parameters().get("boundary");
}
if (boundary == null) {
throw new IllegalStateException("boudary header is missing");
}
MultiPartDecoder decoder = MultiPartDecoder.create(boundary, context);
publisher.subscribe(decoder);
return (Publisher<U>) decoder;
}
use of io.helidon.common.http.MediaType in project helidon by oracle.
the class MessageBodyWriterContext method charset.
@Override
public Charset charset() throws IllegalStateException {
if (charsetCached) {
return charsetCache;
}
MediaType contentType = contentType().orElse(null);
if (contentType != null) {
try {
charsetCache = contentType.charset().map(Charset::forName).orElse(DEFAULT_CHARSET);
charsetCached = true;
return charsetCache;
} catch (IllegalCharsetNameException | UnsupportedCharsetException ex) {
throw new IllegalStateException(ex);
}
}
charsetCache = DEFAULT_CHARSET;
charsetCached = true;
return charsetCache;
}
use of io.helidon.common.http.MediaType in project helidon by oracle.
the class MessageBodyWriterContext method findAccepted.
/**
* Find an media type in the inbound {@code Accept} header with the given
* predicate and default value.
* <ul>
* <li>The default value is returned if the predicate matches a media type
* with a wildcard subtype.<li>
* <li>The default value if the current {@code Content-Type} header is not
* set and the inbound {@code Accept} header is empty or missing.</li>
* <li>When the {@code Content-Type} header is set, if the predicate matches
* the {@code Content-Type} header value is returned.</li>
* </ul>
*
* @param predicate a predicate to match against the inbound {@code Accept}
* header
* @param defaultType a default media type
* @return MediaType, never {@code null}
* @throws IllegalStateException if no media type can be returned
*/
public MediaType findAccepted(Predicate<MediaType> predicate, MediaType defaultType) throws IllegalStateException {
Objects.requireNonNull(predicate, "predicate cannot be null");
Objects.requireNonNull(defaultType, "defaultType cannot be null");
MediaType contentType = contentType().orElse(null);
if (contentType == null) {
if (acceptedTypes.isEmpty()) {
return defaultType;
} else {
for (final MediaType acceptedType : acceptedTypes) {
if (predicate.test(acceptedType)) {
if (acceptedType.isWildcardType() || acceptedType.isWildcardSubtype()) {
return defaultType;
}
return MediaType.create(acceptedType.type(), acceptedType.subtype());
}
}
}
} else {
if (predicate.test(contentType)) {
return contentType;
}
}
throw new IllegalStateException("No accepted Content-Type");
}
Aggregations