Search in sources :

Example 16 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method yamlFromResponse.

/**
 * Returns the response payload from the specified connection as a snakeyaml
 * {@code Yaml} object.
 *
 * @param cnx the {@code HttpURLConnection} containing the response
 * @return the YAML {@code Map<String, Object>} (created by snakeyaml) from
 * the HTTP response payload
 * @throws IOException in case of errors reading the response
 */
@SuppressWarnings(value = "unchecked")
public static Map<String, Object> yamlFromResponse(HttpURLConnection cnx) throws IOException {
    MediaType returnedMediaType = mediaTypeFromResponse(cnx);
    Yaml yaml = new Yaml();
    Charset cs = Charset.defaultCharset();
    if (returnedMediaType.charset().isPresent()) {
        cs = Charset.forName(returnedMediaType.charset().get());
    }
    try (InputStream is = cnx.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, cs)) {
        return (Map<String, Object>) yaml.load(isr);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Example 17 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class OpenAPISupport method chooseResponseMediaType.

private MediaType chooseResponseMediaType(ServerRequest req) {
    /*
         * Response media type default is application/vnd.oai.openapi (YAML)
         * unless otherwise specified.
         */
    Optional<String> queryParameterFormat = req.queryParams().first(OPENAPI_ENDPOINT_FORMAT_QUERY_PARAMETER);
    if (queryParameterFormat.isPresent()) {
        String queryParameterFormatValue = queryParameterFormat.get();
        try {
            return QueryParameterRequestedFormat.chooseFormat(queryParameterFormatValue).mediaType();
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Query parameter 'format' had value '" + queryParameterFormatValue + "' but expected " + Arrays.toString(QueryParameterRequestedFormat.values()));
        }
    }
    final Optional<MediaType> requestedMediaType = req.headers().bestAccepted(OpenAPIMediaType.preferredOrdering());
    final MediaType resultMediaType = requestedMediaType.orElseGet(() -> {
        LOGGER.log(Level.FINER, () -> String.format("Did not recognize requested media type %s; responding with default %s", req.headers().acceptedTypes(), DEFAULT_RESPONSE_MEDIA_TYPE.toString()));
        return DEFAULT_RESPONSE_MEDIA_TYPE;
    });
    return resultMediaType;
}
Also used : MediaType(io.helidon.common.http.MediaType) JsonString(jakarta.json.JsonString)

Example 18 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class OpenAPISupport method prepareResponse.

private void prepareResponse(ServerRequest req, ServerResponse resp) {
    try {
        final MediaType resultMediaType = chooseResponseMediaType(req);
        final String openAPIDocument = prepareDocument(resultMediaType);
        resp.status(Http.Status.OK_200);
        resp.headers().add(Http.Header.CONTENT_TYPE, resultMediaType.toString());
        resp.send(openAPIDocument);
    } catch (Exception ex) {
        resp.status(Http.Status.INTERNAL_SERVER_ERROR_500);
        resp.send("Error serializing OpenAPI document; " + ex.getMessage());
        LOGGER.log(Level.SEVERE, "Error serializing OpenAPI document", ex);
    }
}
Also used : MediaType(io.helidon.common.http.MediaType) JsonString(jakarta.json.JsonString) IOException(java.io.IOException)

Example 19 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method connectAndConsumePayload.

static MediaType connectAndConsumePayload(int port, String path, String queryParameter, MediaType expectedMediaType) throws Exception {
    HttpURLConnection cnx = getURLConnection(port, "GET", path, queryParameter);
    MediaType actualMT = validateResponseMediaType(cnx, expectedMediaType);
    if (actualMT.test(MediaType.APPLICATION_OPENAPI_YAML) || actualMT.test(MediaType.APPLICATION_YAML)) {
        yamlFromResponse(cnx);
    } else if (actualMT.test(MediaType.APPLICATION_OPENAPI_JSON) || actualMT.test(MediaType.APPLICATION_JSON)) {
        jsonFromResponse(cnx);
    } else {
        throw new IllegalArgumentException("Expected either JSON or YAML response but received " + actualMT.toString());
    }
    return actualMT;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MediaType(io.helidon.common.http.MediaType)

Example 20 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class WebClientResponseImpl method content.

@Override
public MessageBodyReadableContent content() {
    Optional<MediaType> mediaType = headers.contentType();
    MessageBodyReaderContext readerContext = MessageBodyReaderContext.create(this.readerContext, null, headers, mediaType);
    return MessageBodyReadableContent.create(publisher, readerContext);
}
Also used : MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext) 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