use of io.helidon.webserver.RequestHeaders in project metro-jax-ws by eclipse-ee4j.
the class WSUtils method getBaseAddress.
static String getBaseAddress(ServerRequest req) {
/* Computes the Endpoint's address from the request.
* Uses "X-Forwarded-Proto", "X-Forwarded-Host" and "Host" headers
* so that it has correct address(IP address or someother hostname)
* through which the application reached the endpoint.
*/
StringBuilder buf = new StringBuilder();
RequestHeaders headers = req.headers();
String protocol = headers.first("X-Forwarded-Proto").orElse(req.isSecure() ? "https" : "http");
// An X-Forwarded-Host header would mean we are behind a reverse
// proxy. Use it as host address if found, or the Host header
// otherwise.
String host = headers.first("X-Forwarded-Host").orElse(headers.first("Host").orElse(req.localAddress() + ":" + req.localPort()));
buf.append(protocol);
buf.append("://");
buf.append(host);
buf.append(basePath(req.path()));
return buf.toString();
}
use of io.helidon.webserver.RequestHeaders in project helidon by oracle.
the class NonJaxRsResource method createNonJaxRsParticipantResource.
Service createNonJaxRsParticipantResource() {
return rules -> rules.any("/{type}/{fqdn}/{methodName}", (req, res) -> {
LOGGER.log(Level.FINE, () -> "Non JAX-RS LRA resource " + req.method().name() + " " + req.absoluteUri());
RequestHeaders headers = req.headers();
HttpRequest.Path path = req.path();
URI lraId = headers.first(LRA_HTTP_CONTEXT_HEADER).or(() -> headers.first(LRA_HTTP_ENDED_CONTEXT_HEADER)).map(URI::create).orElse(null);
URI parentId = headers.first(LRA_HTTP_PARENT_CONTEXT_HEADER).map(URI::create).orElse(null);
PropagatedHeaders propagatedHeaders = participantService.prepareCustomHeaderPropagation(headers.toMap());
String fqdn = path.param("fqdn");
String method = path.param("methodName");
String type = path.param("type");
switch(type) {
case "compensate":
case "complete":
Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, parentId, propagatedHeaders)).forSingle(result -> result.ifPresentOrElse(r -> sendResult(res, r), res::send)).exceptionallyAccept(t -> sendError(lraId, req, res, t));
break;
case "afterlra":
req.content().as(String.class).map(LRAStatus::valueOf).observeOn(exec).flatMapSingle(s -> Single.defer(() -> participantService.invoke(fqdn, method, lraId, s, propagatedHeaders))).onComplete(res::send).onError(t -> sendError(lraId, req, res, t)).ignoreElement();
break;
case "status":
Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, null, propagatedHeaders)).forSingle(result -> result.ifPresentOrElse(r -> sendResult(res, r), // or in the case of non-JAX-RS method returning ParticipantStatus null.
() -> res.status(Response.Status.GONE.getStatusCode()).send())).exceptionallyAccept(t -> sendError(lraId, req, res, t));
break;
case "forget":
Single.<Optional<?>>empty().observeOn(exec).onCompleteResumeWithSingle(o -> participantService.invoke(fqdn, method, lraId, parentId, propagatedHeaders)).onComplete(res::send).onError(t -> sendError(lraId, req, res, t)).ignoreElement();
break;
default:
LOGGER.severe(() -> "Unexpected non Jax-Rs LRA compensation type " + type + ": " + req.absoluteUri());
res.status(404).send();
break;
}
});
}
use of io.helidon.webserver.RequestHeaders 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.webserver.RequestHeaders in project helidon by oracle.
the class StaticContentHandlerTest method etag_InNonMatch_Accept.
@Test
void etag_InNonMatch_Accept() {
RequestHeaders req = mock(RequestHeaders.class);
when(req.values(Http.Header.IF_NONE_MATCH)).thenReturn(List.of("\"ccc\"", "W/\"aaa\""));
when(req.values(Http.Header.IF_MATCH)).thenReturn(Collections.emptyList());
ResponseHeaders res = mock(ResponseHeaders.class);
assertHttpException(() -> StaticContentHandler.processEtag("aaa", req, res), Http.Status.NOT_MODIFIED_304);
verify(res).put(Http.Header.ETAG, "\"aaa\"");
}
use of io.helidon.webserver.RequestHeaders in project helidon by oracle.
the class StaticContentHandlerTest method ifUnmodifySince_NotAccept.
@Test
void ifUnmodifySince_NotAccept() {
ZonedDateTime modified = ZonedDateTime.now();
RequestHeaders req = mock(RequestHeaders.class);
Mockito.doReturn(Optional.of(modified.minusSeconds(60))).when(req).ifUnmodifiedSince();
Mockito.doReturn(Optional.empty()).when(req).ifModifiedSince();
ResponseHeaders res = mock(ResponseHeaders.class);
assertHttpException(() -> StaticContentHandler.processModifyHeaders(modified.toInstant(), req, res), Http.Status.PRECONDITION_FAILED_412);
}
Aggregations