/** * 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(); }); }
public static HttpResponse handleRequest(HttpRequest request) { System.out.println("Handling request to " + request.getUri()); if (request.getUri().path().equals("/greeter")) { final Flow<Message, Message, NotUsed> greeterFlow = greeter(); return WebSocket.handleWebSocketRequestWith(request, greeterFlow); } else { return HttpResponse.create().withStatus(404); } } //#websocket-handling
/** * Depending on the request headers, one of the supported authentication mechanisms is applied. * * @param correlationId the correlationId which will be added to the log * @param inner the inner route which will be wrapped with the {@link AuthorizationContext} * @return the inner route wrapped with the {@link AuthorizationContext} */ public Route authenticate(final String correlationId, final Function<AuthorizationContext, Route> inner) { return extractRequestContext( requestContext -> DirectivesLoggingUtils.enhanceLogWithCorrelationId(correlationId, () -> { final Optional<AuthenticationProvider> applicableDirective = authenticationChain.stream() .filter(authenticationDirective -> authenticationDirective.isApplicable(requestContext)) .findFirst(); final Uri requestUri = requestContext.getRequest().getUri(); if (applicableDirective.isPresent()) { final AuthenticationProvider authenticationProvider = applicableDirective.get(); LOGGER.debug("Applying Authentication Directive '{}' to URI '{}'", authenticationProvider.getClass().getSimpleName(), requestUri); return authenticationProvider.authenticate(correlationId, inner); } else { LOGGER.debug("Missing Authentication for URI '{}'. Applying unauthorizedDirective '{}'", requestUri, unauthorizedDirective); return unauthorizedDirective.apply(correlationId); } })); }
@Override public boolean isApplicable(final RequestContext context) { return getRequestHeader(context, HttpHeader.X_DITTO_DUMMY_AUTH.getName()).isPresent() || context.getRequest().getUri().query().get(HttpHeader.X_DITTO_DUMMY_AUTH.getName()).isPresent(); }
final Uri requestUri = requestContext.getRequest().getUri(); final Optional<String> forwardedProtoHeader = extractXForwardedProtoHeader(requestUri, requestContext);
public static Route ensureEncoding(final String correlationId, final Supplier<Route> inner) { return extractRequestContext(requestContext -> enhanceLogWithCorrelationId(correlationId, () -> { final Uri uri = requestContext.getRequest().getUri(); try { // per default, Akka evaluates the query params "lazily" in the routes and throws an IllegalUriException // in case of error; we evaluate the query params explicitly here to be able to handle this error at // a central location uri.query(); } catch (final IllegalUriException e) { LOGGER.debug("URI parsing failed", e); final String rawRequestUri = HttpUtils.getRawRequestUri(requestContext.getRequest()); final String message = MessageFormat.format(URI_INVALID_TEMPLATE, rawRequestUri); return complete(StatusCodes.BAD_REQUEST, message); } return inner.get(); })); } }
/** * Prepares an {@link ExpiringTimerBuilder} with default {@link #FILTER_AUTH_METRIC_NAME} and tags. * @param authenticationType The name of the authentication type (i.e. jwt,...) * @param request The HttpRequest used to extract required tags. * @return The prepared {@link ExpiringTimerBuilder} */ public static ExpiringTimerBuilder newAuthFilterTimer(final String authenticationType, final HttpRequest request) { final String requestPath = request.getUri().toRelative().path(); final TraceInformation traceInformation = determineTraceInformation(requestPath); return newAuthFilterTimer(authenticationType, traceInformation.getTags()); }
/** * Prepares an {@link ExpiringTimerBuilder} with default {@link #FILTER_AUTH_METRIC_NAME} and tags. * @param authenticationType The name of the authentication type (i.e. jwt,...) * @param request The HttpRequest used to extract required tags. * @return The prepared {@link ExpiringTimerBuilder} */ public static ExpiringTimerBuilder newAuthFilterTimer(final String authenticationType, final HttpRequest request) { final String requestPath = request.getUri().toRelative().path(); final TraceInformation traceInformation = determineTraceInformation(requestPath); return newAuthFilterTimer(authenticationType, traceInformation.getTags()); }
private static Function<HttpResponse, HttpResponse> createModifiedLocationHeaderAddingResponseMapper( final HttpRequest request, final CommandResponse commandResponse) { return response -> { if (HttpStatusCode.CREATED == commandResponse.getStatusCode()) { Uri newUri = request.getUri(); if (!request.method().isIdempotent()) { // only for not idempotent requests (e.g.: POST), add the "createdId" to the path: final String uriStr = newUri.toString(); String createdLocation; final int uriIdIndex = uriStr.indexOf(commandResponse.getId()); // if the uri contains the id, but *not* at the beginning if (uriIdIndex > 0) { createdLocation = uriStr.substring(0, uriIdIndex) + commandResponse.getId() + commandResponse.getResourcePath().toString(); } else { createdLocation = uriStr + "/" + commandResponse.getId() + commandResponse.getResourcePath() .toString(); } if (createdLocation.endsWith("/")) { createdLocation = createdLocation.substring(0, createdLocation.length() - 1); } newUri = Uri.create(createdLocation); } return response.addHeader(Location.create(newUri)); } else { return response; } }; }
final String dummyAuth = getRequestHeader(requestContext, HttpHeader.X_DITTO_DUMMY_AUTH.getName()) .orElseGet(() -> requestContext.getRequest().getUri().query() .get(HttpHeader.X_DITTO_DUMMY_AUTH.getName()) .orElseThrow(
final HttpRequest request = requestContext.getRequest(); final String requestMethod = request.method().name(); final String requestUri = request.getUri().toRelative().toString(); return mapRouteResult( routeResult -> DirectivesLoggingUtils.enhanceLogWithCorrelationId(correlationId, () -> {
/** * Prepares an {@link ExpiringTimerBuilder} with default {@link #HTTP_ROUNDTRIP_METRIC_NAME} and tags. * @param request The request to extract tags and request method. * @return The prepared {@link ExpiringTimerBuilder} */ public static ExpiringTimerBuilder newHttpRoundTripTimer(final HttpRequest request) { final String requestMethod = request.method().name(); final String requestPath = request.getUri().toRelative().path(); final TraceInformation traceInformation = determineTraceInformation(requestPath); return newExpiringTimer(HTTP_ROUNDTRIP_METRIC_NAME) .tags(traceInformation.getTags()) .tag(TracingTags.REQUEST_METHOD, requestMethod); }
/** * Prepares an {@link ExpiringTimerBuilder} with default {@link #HTTP_ROUNDTRIP_METRIC_NAME} and tags. * @param request The request to extract tags and request method. * @return The prepared {@link ExpiringTimerBuilder} */ public static ExpiringTimerBuilder newHttpRoundTripTimer(final HttpRequest request) { final String requestMethod = request.method().name(); final String requestPath = request.getUri().toRelative().path(); final TraceInformation traceInformation = determineTraceInformation(requestPath); return newExpiringTimer(HTTP_ROUNDTRIP_METRIC_NAME) .tags(traceInformation.getTags()) .tag(TracingTags.REQUEST_METHOD, requestMethod); }
final String requestUri = request.getUri().toRelative().toString(); logger.warn("Request {} '{}' timed out after {}", requestMethod, requestUri, duration); logger.info("StatusCode of request {} '{}' was: {}", requestMethod, requestUri, statusCode);