private static Optional<String> extractCorrelationId(final HttpRequest request) { return request.getHeader(CORRELATION_ID_HEADER).map(HttpHeader::value); }
/** * Extracts the {@link RequestHeaders} of the specified request. * * @param requestContext the RequestContext from which to extract the headers. * @return the extracted headers of {@code requestContext}. * @throws NullPointerException if {@code requestContext} is {@code null}. */ public static RequestHeaders getHeaders(final RequestContext requestContext) { checkNotNull(requestContext, "requestContext"); final Map<String, String> headers = new HashMap<>(); requestContext.getRequest().getHeaders().forEach( header -> headers.put(header.name().toLowerCase(), header.value())); final Iterable<akka.http.javadsl.model.headers.HttpCookiePair> cookies = requestContext.getRequest() .getHeader(Cookie.class) .map(akka.http.javadsl.model.headers.Cookie::getCookies) .orElse(Collections.emptyList()); final Map<String, String> cookieMap = new HashMap<>(); cookies.forEach(cookie -> cookieMap.put(cookie.name(), cookie.value())); return new RequestHeaders(headers, cookieMap); }
/** * Gets the request header with the given name. * * @param requestContext the RequestContext to extract the header from * @param name the name of the header * @return the header value or an empty {@link Optional} */ public static Optional<String> getRequestHeader(final RequestContext requestContext, final String name) { return requestContext.getRequest() .getHeader(name) .map(akka.http.javadsl.model.HttpHeader::value); }
/** * Gets the (absolute) raw requestUri. * <p>NOTE: You must configure {@code raw-request-uri-header = on} for Akka-Http, otherwise the url already * parsed by Akka Http will be returned.</p> * * @param request the http request * @return the raw requestUri */ public static String getRawRequestUri(final HttpRequest request) { return request.getHeader(RawRequestURI.class) .map(RawRequestURI::toString) .orElseGet(() -> { LOGGER.warn("raw-request-uri-header is not enabled, returning already parsed URI"); return request.getUri().toString(); }); }
/** * Checks whether the given request contains an authorization header starting with the given {@code * authorizationHeaderPrefix}. * * @param requestContext the context of the request * @param authorizationHeaderPrefix the prefix of the authorization header * @return {@code true}, if the request contains a matching authorization header */ public static boolean containsAuthorizationForPrefix(final RequestContext requestContext, final String authorizationHeaderPrefix) { final Optional<String> authorizationHeader = requestContext.getRequest().getHeader(HttpHeader.AUTHORIZATION.toString().toLowerCase()) .map(akka.http.javadsl.model.HttpHeader::value) .filter(headerValue -> headerValue.startsWith(authorizationHeaderPrefix)); return authorizationHeader.isPresent(); }
private static Optional<String> extractXForwardedProtoHeader(final Uri requestUri, final RequestContext requestContext) { String forwardedProtoHeaderValue; final Optional<akka.http.javadsl.model.HttpHeader> standardForwardedProtoHeader = requestContext.getRequest() .getHeader(X_FORWARDED_PROTO_STANDARD) // .filter(header -> header.value().length() > 0); forwardedProtoHeaderValue = standardForwardedProtoHeader.map(akka.http.javadsl.model.HttpHeader::value) .orElse(null); if (forwardedProtoHeaderValue != null) { LOGGER.debug("Header {} was: '{}' for uri: {}", X_FORWARDED_PROTO_STANDARD, forwardedProtoHeaderValue, requestUri); } else { final Optional<akka.http.javadsl.model.HttpHeader> forwardedProtoHeaderLbaas = requestContext.getRequest() .getHeader(X_FORWARDED_PROTO_LBAAS) // .filter(header -> header.value().length() > 0); forwardedProtoHeaderValue = forwardedProtoHeaderLbaas.map(akka.http.javadsl.model.HttpHeader::value) .orElse(null); if (forwardedProtoHeaderValue != null) { LOGGER.debug("Header {} was: '{}' for uri: {}", X_FORWARDED_PROTO_LBAAS, forwardedProtoHeaderValue, requestUri); } else { LOGGER.debug("Missing header {} for uri: {}", X_FORWARDED_PROTO_STANDARD + " or " + X_FORWARDED_PROTO_LBAAS, requestUri); } } return Optional.ofNullable(forwardedProtoHeaderValue); }
private Function<ByteBuffer, MessageCommand<?, ?>> buildSendFeatureMessage(final MessageDirection direction, final RequestContext ctx, final DittoHeaders dittoHeaders, final String thingId, final String featureId, final String msgSubject, final Duration timeout) { final HttpRequest httpRequest = ctx.getRequest(); return payload -> { final ContentType contentType = httpRequest.entity() .getContentType(); final MessageHeaders headers = MessageHeaders.newBuilder(direction, thingId, normalizeSubject(msgSubject)) .featureId(featureId) .correlationId(dittoHeaders.getCorrelationId().orElse(null)) .contentType(contentType .toString()) .timeout(timeout) .timestamp(OffsetDateTime.now()) .validationUrl(httpRequest.getHeader(X_DITTO_VALIDATION_URL) .map(HttpHeader::value) .orElse(null)) .putHeaders(dittoHeaders) .build(); final MessageBuilder<Object> messageBuilder = initMessageBuilder(payload, contentType, headers); return SendFeatureMessage.of(thingId, featureId, messageBuilder.build(), enhanceHeaders(dittoHeaders)); }; }