mapRouteResult

mapRouteResult

Description

Changes the message the inner route sends to the responder.

The mapRouteResult directive is used as a building block for Custom Directives to transform the RouteResult coming back from the inner route.

See Transforming the RouteResult for similar directives.

Example

// this directive is a joke, don't do that :-)
final Route route = mapRouteResult(r -> {
  if (r instanceof Complete) {
    final HttpResponse response = ((Complete) r).getResponse();
    return RouteResults.complete(response.withStatus(200));
  } else {
    return r;
  }
}, () -> complete(StatusCodes.ACCEPTED));

// tests:
testRoute(route).run(HttpRequest.GET("/"))
  .assertStatusCode(StatusCodes.OK);
final Route route = mapRouteResult(rr -> {
  final Iterable<Rejection> rejections = Collections.singletonList(Rejections.authorizationFailed());
  return RouteResults.rejected(rejections);
}, () -> complete("abc"));

// tests:
runRouteUnSealed(route, HttpRequest.GET("/"))
  .assertRejections(Rejections.authorizationFailed());

Contents