Search in sources :

Example 1 with MessageBodyReadableContent

use of io.helidon.media.common.MessageBodyReadableContent in project helidon by oracle.

the class MultiPartDecoder method createPart.

private ReadableBodyPart createPart() {
    ReadableBodyPartHeaders headers = bodyPartHeaderBuilder.build();
    // create a reader context for the part
    MessageBodyReaderContext partContext = MessageBodyReaderContext.create(context, /* eventListener */
    null, headers, Optional.of(headers.contentType()));
    // create a readable content for the part
    MessageBodyReadableContent partContent = MessageBodyReadableContent.create(bodyPartPublisher, partContext);
    return bodyPartBuilder.headers(headers).content(partContent).build();
}
Also used : MessageBodyReaderContext(io.helidon.media.common.MessageBodyReaderContext) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 2 with MessageBodyReadableContent

use of io.helidon.media.common.MessageBodyReadableContent in project helidon by oracle.

the class TestClient method callServiceAndGetString.

/**
 * Call remote service method and return its raw data as JSON object.
 * No response content check is done. No query parameters are passed.
 *
 * @param service remote service name
 * @param method remote test method name
 * @return data returned by remote service
 */
public String callServiceAndGetString(final String service, final String method) {
    WebClientRequestBuilder rb = clientGetBuilderWithPath(service, method);
    final MessageBodyReadableContent content = rb.submit().await(1, TimeUnit.MINUTES).content();
    return content.as(String.class).await(1, TimeUnit.MINUTES);
}
Also used : WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 3 with MessageBodyReadableContent

use of io.helidon.media.common.MessageBodyReadableContent in project helidon by oracle.

the class HealthServerTest method checkResponse.

private static void checkResponse(Supplier<WebClientRequestBuilder> requestFactory, String requestPath, boolean expectContent) {
    WebClientResponse response = null;
    try {
        response = requestFactory.get().path(requestPath).accept(MediaType.APPLICATION_JSON).request().await();
        int expectedStatus = expectContent ? Http.Status.OK_200.code() : Http.Status.NO_CONTENT_204.code();
        assertThat("health response status", response.status().code(), is(expectedStatus));
        MessageBodyReadableContent content = response.content();
        String textContent = null;
        try {
            textContent = content.as(String.class).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        assertThat("content length", textContent.length(), expectContent ? greaterThan(0) : equalTo(0));
        if (expectContent) {
            JsonObject health = Json.createReader(new StringReader(textContent)).readObject();
            assertThat("health status", health.getString("status"), is("UP"));
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject) ExecutionException(java.util.concurrent.ExecutionException) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 4 with MessageBodyReadableContent

use of io.helidon.media.common.MessageBodyReadableContent in project helidon by oracle.

the class TestClient method callService.

JsonObject callService(WebClientRequestBuilder rb, Map<String, String> params) {
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            rb = rb.queryParam(entry.getKey(), encode(entry.getValue()));
        }
    }
    final MessageBodyReadableContent content = rb.submit().await(1, TimeUnit.MINUTES).content();
    final String response = content.as(String.class).await(1, TimeUnit.MINUTES);
    try {
        final JsonReader jsonReader = Json.createReader(new StringReader(response));
        final JsonValue jsonContent = jsonReader.read();
        switch(jsonContent.getValueType()) {
            case OBJECT:
                return jsonContent.asJsonObject();
            default:
                throw new HelidonTestException(String.format("Expected JSON object, but got JSON %s", jsonContent.getValueType().name().toLowerCase()));
        }
    } catch (JsonException t) {
        LOGGER.log(Level.WARNING, t, () -> String.format("Caught %s when parsing response: %s", t.getClass().getSimpleName(), response));
        throw new HelidonTestException(String.format("Caught %s when parsing response: %s", t.getClass().getSimpleName(), response), t);
    }
}
Also used : JsonException(jakarta.json.JsonException) StringReader(java.io.StringReader) JsonValue(jakarta.json.JsonValue) JsonReader(jakarta.json.JsonReader) Map(java.util.Map) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 5 with MessageBodyReadableContent

use of io.helidon.media.common.MessageBodyReadableContent in project helidon by oracle.

the class TestServiceClient method callServiceAndGetString.

/**
 * Call remote service method and return its raw data as JSON object.
 * No response content check is done. No query parameters are passed.
 *
 * @param method remote test method name
 * @return data returned by remote service
 */
public String callServiceAndGetString(final String method) {
    WebClientRequestBuilder rb = clientGetBuilderWithPath(service, method);
    final MessageBodyReadableContent content = rb.submit().await(1, TimeUnit.MINUTES).content();
    return content.as(String.class).await(1, TimeUnit.MINUTES);
}
Also used : WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Aggregations

MessageBodyReadableContent (io.helidon.media.common.MessageBodyReadableContent)5 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)2 StringReader (java.io.StringReader)2 MessageBodyReaderContext (io.helidon.media.common.MessageBodyReaderContext)1 WebClientResponse (io.helidon.webclient.WebClientResponse)1 JsonException (jakarta.json.JsonException)1 JsonObject (jakarta.json.JsonObject)1 JsonReader (jakarta.json.JsonReader)1 JsonValue (jakarta.json.JsonValue)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1