Search in sources :

Example 21 with MediaType

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

the class FileBasedContentHandler method detectType.

private MediaType detectType(String fileName, RequestHeaders requestHeaders) {
    Objects.requireNonNull(fileName);
    Objects.requireNonNull(requestHeaders);
    // then check the type is accepted by the request
    return findCustomMediaType(fileName).or(() -> MediaTypes.detectType(fileName).map(MediaType::parse)).map(it -> {
        if (requestHeaders.isAccepted(it)) {
            return it;
        }
        throw new HttpException("Media type " + it + " is not accepted by request", Http.Status.UNSUPPORTED_MEDIA_TYPE_415);
    }).orElseGet(() -> {
        List<MediaType> acceptedTypes = requestHeaders.acceptedTypes();
        if (acceptedTypes.isEmpty()) {
            return MediaType.APPLICATION_OCTET_STREAM;
        } else {
            return acceptedTypes.iterator().next();
        }
    });
}
Also used : MessageBodyWriter(io.helidon.media.common.MessageBodyWriter) DefaultMediaSupport(io.helidon.media.common.DefaultMediaSupport) Files(java.nio.file.Files) MediaTypes(io.helidon.common.media.type.MediaTypes) IOException(java.io.IOException) Instant(java.time.Instant) Logger(java.util.logging.Logger) RequestHeaders(io.helidon.webserver.RequestHeaders) MediaType(io.helidon.common.http.MediaType) ServerRequest(io.helidon.webserver.ServerRequest) Objects(java.util.Objects) List(java.util.List) ResponseHeaders(io.helidon.webserver.ResponseHeaders) Map(java.util.Map) HttpException(io.helidon.webserver.HttpException) ServerResponse(io.helidon.webserver.ServerResponse) Optional(java.util.Optional) Http(io.helidon.common.http.Http) Path(java.nio.file.Path) MediaType(io.helidon.common.http.MediaType) HttpException(io.helidon.webserver.HttpException)

Example 22 with MediaType

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

the class FormParamsSupport method accept.

@Override
public void accept(ServerRequest req, ServerResponse res) {
    MediaType reqMediaType = req.headers().contentType().orElse(MediaType.TEXT_PLAIN);
    Charset charset = reqMediaType.charset().map(Charset::forName).orElse(StandardCharsets.UTF_8);
    req.content().registerReader(FormParams.class, (chunks, type) -> ContentReaders.readString(chunks, charset).map(s -> FormParams.create(s, reqMediaType)).toStage());
    req.next();
}
Also used : MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset)

Example 23 with MediaType

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

the class HashRequestHeaders method bestAccepted.

@Override
public Optional<MediaType> bestAccepted(MediaType... mediaTypes) {
    if (mediaTypes == null || mediaTypes.length == 0) {
        return Optional.empty();
    }
    List<MediaType> accepts = acceptedTypes();
    if (accepts == null || accepts.isEmpty()) {
        return Optional.ofNullable(mediaTypes[0]);
    }
    double best = 0;
    MediaType result = null;
    for (MediaType mt : mediaTypes) {
        if (mt != null) {
            for (MediaType acc : accepts) {
                double q = acc.qualityFactor();
                if (q > best && acc.test(mt)) {
                    if (q == 1) {
                        return Optional.of(mt);
                    } else {
                        best = q;
                        result = mt;
                    }
                }
            }
        }
    }
    return Optional.ofNullable(result);
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 24 with MediaType

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

the class RequestPredicateTest method accepts2.

@Test
public void accepts2() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().get("/accepts2", RequestPredicate.create().accepts(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).thenApply((req, resp) -> {
        checker.handlerInvoked("acceptsMediaType");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("doesNotAcceptMediaType");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().accepts((MediaType[]) null);
    });
    routing.route(mockRequest("/accepts2", Map.of("Accept", List.of("application/json"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts2", Map.of("Accept", List.of("text/plain"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts2", Map.of("Accept", List.of("text/plain", "application/xml"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts2", Map.of("Accept", List.of())), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts2", Map.of()), mockResponse());
    assertThat(checker.handlersInvoked(), is("acceptsMediaType"));
    checker.reset();
    routing.route(mockRequest("/accepts2", Map.of("Accept", List.of("application/xml"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotAcceptMediaType"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

Example 25 with MediaType

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

the class RequestPredicateTest method hasContentType2.

@Test
public void hasContentType2() {
    final RoutingChecker checker = new RoutingChecker();
    Routing routing = Routing.builder().get("/contentType2", RequestPredicate.create().hasContentType(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).thenApply((req, resp) -> {
        checker.handlerInvoked("hasContentType");
    }).otherwise((req, res) -> {
        checker.handlerInvoked("doesNotHaveContentType");
    })).build();
    assertThrows(NullPointerException.class, () -> {
        RequestPredicate.create().hasContentType((MediaType[]) null);
    });
    routing.route(mockRequest("/contentType2", Map.of("Content-Type", List.of("text/plain"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("hasContentType"));
    checker.reset();
    routing.route(mockRequest("/contentType2", Map.of("Content-Type", List.of("text/plain"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("hasContentType"));
    checker.reset();
    routing.route(mockRequest("/contentType2", Map.of("Content-Type", List.of("application/json"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("hasContentType"));
    checker.reset();
    routing.route(mockRequest("/contentType2", Map.of("Content-Type", List.of("application/xml"))), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotHaveContentType"));
    checker.reset();
    routing.route(mockRequest("/contentType2", Map.of("Content-Type", List.of())), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotHaveContentType"));
    checker.reset();
    routing.route(mockRequest("/contentType2", Map.of()), mockResponse());
    assertThat(checker.handlersInvoked(), is("doesNotHaveContentType"));
}
Also used : Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Mockito(org.mockito.Mockito) RoutingTest.mockResponse(io.helidon.webserver.RoutingTest.mockResponse) List(java.util.List) Predicate(java.util.function.Predicate) Map(java.util.Map) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) MediaType(io.helidon.common.http.MediaType) MediaType(io.helidon.common.http.MediaType) RoutingChecker(io.helidon.webserver.RoutingTest.RoutingChecker) Test(org.junit.jupiter.api.Test)

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