Search in sources :

Example 1 with MediaType

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));
}
Also used : Objects(java.util.Objects) Flow(java.util.concurrent.Flow) DataChunk(io.helidon.common.http.DataChunk) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType)

Example 2 with MediaType

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)));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 3 with MediaType

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;
}
Also used : MediaType(io.helidon.common.http.MediaType) Publisher(java.util.concurrent.Flow.Publisher)

Example 4 with MediaType

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;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset)

Example 5 with MediaType

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");
}
Also used : MediaType(io.helidon.common.http.MediaType)

Aggregations

MediaType (io.helidon.common.http.MediaType)37 Charset (java.nio.charset.Charset)6 Map (java.util.Map)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 GenericType (io.helidon.common.GenericType)5 DataChunk (io.helidon.common.http.DataChunk)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Multi (io.helidon.common.reactive.Multi)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)4 Objects (java.util.Objects)4 Flow (java.util.concurrent.Flow)4 Http (io.helidon.common.http.Http)3 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)3 List (java.util.List)3 Single (io.helidon.common.reactive.Single)2 RoutingChecker (io.helidon.webserver.RoutingTest.RoutingChecker)2 RoutingTest.mockResponse (io.helidon.webserver.RoutingTest.mockResponse)2 JsonString (jakarta.json.JsonString)2 Jsonb (jakarta.json.bind.Jsonb)2