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