Search in sources :

Example 1 with MediaContext

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

the class Main method supports.

/**
 * Combination of filtering {@link Handler} pattern with {@link io.helidon.webserver.Service Service} registration capabilities
 * can be used by other frameworks for the integration. WebServer is shipped with several integrated libraries (supports)
 * including <i>static content</i>, JSON and Jersey. See {@code POM.xml} for requested dependencies.
 */
public void supports() {
    Routing routing = Routing.builder().register(StaticContentSupport.create("/static")).get("/hello/{what}", (req, res) -> res.send(JSON.createObjectBuilder().add("message", "Hello " + req.path().param("what")).build())).register("/api", JerseySupport.builder().register(HelloWorldResource.class).build()).build();
    MediaContext mediaContext = MediaContext.builder().addWriter(JsonpSupport.writer()).build();
    startServer(routing, mediaContext);
}
Also used : MediaContext(io.helidon.media.common.MediaContext) Routing(io.helidon.webserver.Routing)

Example 2 with MediaContext

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

the class Main method mediaReader.

/**
 * Use a custom {@link MessageBodyReader reader} to convert the request content into an object of a given type.
 */
public void mediaReader() {
    Routing routing = Routing.builder().post("/create-record", Handler.create(Name.class, (req, res, name) -> {
        System.out.println("Name: " + name);
        res.status(Http.Status.CREATED_201).send(name.toString());
    })).build();
    // Create a media support that contains the defaults and our custom Name reader
    MediaContext mediaContext = MediaContext.builder().addReader(NameReader.create()).build();
    startServer(routing, mediaContext);
}
Also used : MediaContext(io.helidon.media.common.MediaContext) Routing(io.helidon.webserver.Routing)

Example 3 with MediaContext

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

the class RequestContentTest method requestTestStub.

private static Request requestTestStub(Publisher<DataChunk> flux) {
    BareRequest bareRequestMock = mock(BareRequest.class);
    doReturn(URI.create("http://0.0.0.0:1234")).when(bareRequestMock).uri();
    doReturn(flux).when(bareRequestMock).bodyPublisher();
    WebServer webServer = mock(WebServer.class);
    MediaContext mediaContext = MediaContext.create();
    doReturn(mediaContext.readerContext()).when(webServer).readerContext();
    doReturn(mediaContext.writerContext()).when(webServer).writerContext();
    return new RequestTestStub(bareRequestMock, webServer);
}
Also used : MediaContext(io.helidon.media.common.MediaContext)

Example 4 with MediaContext

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

the class MediaContextTest method testMediaSupportWithoutDefaults.

@Test
public void testMediaSupportWithoutDefaults() throws Exception {
    WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").mediaContext(MediaContext.empty()).build();
    client.get().request(String.class).thenAccept(it -> fail("No reader for String should be registered!")).exceptionally(ex -> {
        assertThat(ex.getCause().getMessage(), is("No reader found for type: class java.lang.String"));
        return null;
    }).toCompletableFuture().get();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) WebClient(io.helidon.webclient.WebClient) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) IOException(java.io.IOException) MediaContext(io.helidon.media.common.MediaContext) Json(jakarta.json.Json) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonObject(jakarta.json.JsonObject) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) InputStream(java.io.InputStream) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

MediaContext (io.helidon.media.common.MediaContext)4 Routing (io.helidon.webserver.Routing)2 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)1 WebClient (io.helidon.webclient.WebClient)1 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)1 Json (jakarta.json.Json)1 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)1 JsonObject (jakarta.json.JsonObject)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 ExecutionException (java.util.concurrent.ExecutionException)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.is (org.hamcrest.Matchers.is)1 Assertions.fail (org.junit.jupiter.api.Assertions.fail)1 Test (org.junit.jupiter.api.Test)1