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