@Override protected final Accumulator<ByteString, F.Either<Result, A>> apply1(Http.RequestHeader request) { Accumulator<ByteString, ByteString> byteStringByteStringAccumulator = Accumulator.strict( maybeStrictBytes -> CompletableFuture.completedFuture(maybeStrictBytes.orElse(ByteString.empty())), Sink.fold(ByteString.empty(), ByteString::concat) ); Accumulator<ByteString, F.Either<Result, A>> byteStringEitherAccumulator = byteStringByteStringAccumulator.mapFuture(bytes -> { try { return CompletableFuture.completedFuture(F.Either.Right(parse(request, bytes))); } catch (Exception e) { return errorHandler.onClientError(request, Status$.MODULE$.BAD_REQUEST(), errorMessage + ": " + e.getMessage()) .thenApply(F.Either::<Result, A>Left); } }, JavaParsers.trampoline()); return byteStringEitherAccumulator; }
/** * Validate the content type of the passed in request using the given validator. * * If the validator returns true, the passed in accumulator will be returned to parse the body, otherwise an * accumulator with a result created by the error handler will be returned. * * @param errorHandler The error handler used to create a bad request result if the content type is not valid. * @param request The request to validate. * @param errorMessage The error message to pass to the error handler if the content type is not valid. * @param validate The validation function. * @param parser The parser to use if the content type is valid. * @param <A> The type to be parsed by the parser * @return An accumulator to parse the body. */ public static <A> Accumulator<ByteString, F.Either<Result, A>> validateContentType(HttpErrorHandler errorHandler, Http.RequestHeader request, String errorMessage, Function<String, Boolean> validate, Function<Http.RequestHeader, Accumulator<ByteString, F.Either<Result, A>>> parser) { if (request.contentType().map(validate).orElse(false)) { return parser.apply(request); } else { CompletionStage<Result> result = errorHandler.onClientError(request, Status$.MODULE$.UNSUPPORTED_MEDIA_TYPE(), errorMessage); return Accumulator.done(result.thenApply(F.Either::Left)); } }
@Override public CompletionStage<Result> onClientError(RequestHeader request, int statusCode, String message) { if (!play.api.http.Status$.MODULE$.isClientError(statusCode)) { throw new IllegalArgumentException( "onClientError invoked with non client error status code " + statusCode + ": " + message); } ObjectNode result = Json.newObject(); result.put("requestId", request.asScala().id()); result.put("message", message); return CompletableFuture.completedFuture(Results.status(statusCode, error(result))); }
/** * Validate the content type of the passed in request using the given validator. * * If the validator returns true, the passed in accumulator will be returned to parse the body, otherwise an * accumulator with a result created by the error handler will be returned. * * @param errorHandler The error handler used to create a bad request result if the content type is not valid. * @param request The request to validate. * @param errorMessage The error message to pass to the error handler if the content type is not valid. * @param validate The validation function. * @param parser The parser to use if the content type is valid. * @param <A> The type to be parsed by the parser * @return An accumulator to parse the body. */ public static <A> Accumulator<ByteString, F.Either<Result, A>> validateContentType(HttpErrorHandler errorHandler, Http.RequestHeader request, String errorMessage, Function<String, Boolean> validate, Function<Http.RequestHeader, Accumulator<ByteString, F.Either<Result, A>>> parser) { if (request.contentType().map(validate).orElse(false)) { return parser.apply(request); } else { CompletionStage<Result> result = errorHandler.onClientError(request, Status$.MODULE$.UNSUPPORTED_MEDIA_TYPE(), errorMessage); return Accumulator.done(result.thenApply(F.Either::Left)); } }
/** * Validate the content type of the passed in request using the given validator. * * If the validator returns true, the passed in accumulator will be returned to parse the body, otherwise an * accumulator with a result created by the error handler will be returned. * * @param errorHandler The error handler used to create a bad request result if the content type is not valid. * @param request The request to validate. * @param errorMessage The error message to pass to the error handler if the content type is not valid. * @param validate The validation function. * @param parser The parser to use if the content type is valid. * @param <A> The type to be parsed by the parser * @return An accumulator to parse the body. */ public static <A> Accumulator<ByteString, F.Either<Result, A>> validateContentType(HttpErrorHandler errorHandler, Http.RequestHeader request, String errorMessage, Function<String, Boolean> validate, Function<Http.RequestHeader, Accumulator<ByteString, F.Either<Result, A>>> parser) { if (request.contentType().map(validate).orElse(false)) { return parser.apply(request); } else { CompletionStage<Result> result = errorHandler.onClientError(request, Status$.MODULE$.UNSUPPORTED_MEDIA_TYPE(), errorMessage); return Accumulator.done(result.thenApply(F.Either::Left)); } }
@Override protected final Accumulator<ByteString, F.Either<Result, A>> apply1(Http.RequestHeader request) { Accumulator<ByteString, ByteString> byteStringByteStringAccumulator = Accumulator.strict( maybeStrictBytes -> CompletableFuture.completedFuture(maybeStrictBytes.orElse(ByteString.empty())), Sink.fold(ByteString.empty(), ByteString::concat) ); Accumulator<ByteString, F.Either<Result, A>> byteStringEitherAccumulator = byteStringByteStringAccumulator.mapFuture(bytes -> { try { return CompletableFuture.completedFuture(F.Either.Right(parse(request, bytes))); } catch (Exception e) { return errorHandler.onClientError(request, Status$.MODULE$.BAD_REQUEST(), errorMessage + ": " + e.getMessage()) .thenApply(F.Either::<Result, A>Left); } }, JavaParsers.trampoline()); return byteStringEitherAccumulator; }
@Override protected final Accumulator<ByteString, F.Either<Result, A>> apply1(Http.RequestHeader request) { Accumulator<ByteString, ByteString> byteStringByteStringAccumulator = Accumulator.strict( maybeStrictBytes -> CompletableFuture.completedFuture(maybeStrictBytes.orElse(ByteString.empty())), Sink.fold(ByteString.empty(), ByteString::concat) ); Accumulator<ByteString, F.Either<Result, A>> byteStringEitherAccumulator = byteStringByteStringAccumulator.mapFuture(bytes -> { try { return CompletableFuture.completedFuture(F.Either.Right(parse(request, bytes))); } catch (Exception e) { return errorHandler.onClientError(request, Status$.MODULE$.BAD_REQUEST(), errorMessage + ": " + e.getMessage()) .thenApply(F.Either::<Result, A>Left); } }, JavaParsers.trampoline()); return byteStringEitherAccumulator; }
@Override public Accumulator<ByteString, F.Either<Result, A>> apply(Http.RequestHeader request) { Flow<ByteString, ByteString, Future<MaxSizeStatus>> takeUpToFlow = Flow.fromGraph(play.api.mvc.BodyParsers$.MODULE$.takeUpTo(maxLength)); Sink<ByteString, CompletionStage<F.Either<Result, A>>> result = apply1(request).toSink(); return Accumulator.fromSink(takeUpToFlow.toMat(result, (statusFuture, resultFuture) -> FutureConverters.toJava(statusFuture).thenCompose(status -> { if (status instanceof MaxSizeNotExceeded$) { return resultFuture; } else { return errorHandler.onClientError(request, Status$.MODULE$.REQUEST_ENTITY_TOO_LARGE(), "Request entity too large") .thenApply(F.Either::<Result, A>Left); } }) )); }
@Override public Accumulator<ByteString, F.Either<Result, A>> apply(Http.RequestHeader request) { Flow<ByteString, ByteString, Future<MaxSizeStatus>> takeUpToFlow = Flow.fromGraph(play.api.mvc.BodyParsers$.MODULE$.takeUpTo(maxLength)); Sink<ByteString, CompletionStage<F.Either<Result, A>>> result = apply1(request).toSink(); return Accumulator.fromSink(takeUpToFlow.toMat(result, (statusFuture, resultFuture) -> FutureConverters.toJava(statusFuture).thenCompose(status -> { if (status instanceof MaxSizeNotExceeded$) { return resultFuture; } else { return errorHandler.onClientError(request, Status$.MODULE$.REQUEST_ENTITY_TOO_LARGE(), "Request entity too large") .thenApply(F.Either::<Result, A>Left); } }) )); }
@Override public Accumulator<ByteString, F.Either<Result, A>> apply(Http.RequestHeader request) { Flow<ByteString, ByteString, Future<MaxSizeStatus>> takeUpToFlow = Flow.fromGraph(play.api.mvc.BodyParsers$.MODULE$.takeUpTo(maxLength)); Sink<ByteString, CompletionStage<F.Either<Result, A>>> result = apply1(request).toSink(); return Accumulator.fromSink(takeUpToFlow.toMat(result, (statusFuture, resultFuture) -> FutureConverters.toJava(statusFuture).thenCompose(status -> { if (status instanceof MaxSizeNotExceeded$) { return resultFuture; } else { return errorHandler.onClientError(request, Status$.MODULE$.REQUEST_ENTITY_TOO_LARGE(), "Request entity too large") .thenApply(F.Either::<Result, A>Left); } }) )); }
@Override public CompletionStage<Result> onClientError(RequestHeader request, int statusCode, String message) { if (!play.api.http.Status$.MODULE$.isClientError(statusCode)) { throw new IllegalArgumentException( "onClientError invoked with non client error status code " + statusCode + ": " + message); } ObjectNode result = Json.newObject(); result.put("requestId", request.asScala().id()); result.put("message", message); return CompletableFuture.completedFuture(Results.status(statusCode, error(result))); }
@Override public CompletionStage<Result> onClientError(RequestHeader request, int statusCode, String message) { if (!play.api.http.Status$.MODULE$.isClientError(statusCode)) { throw new IllegalArgumentException( "onClientError invoked with non client error status code " + statusCode + ": " + message); } ObjectNode result = Json.newObject(); result.put("requestId", request.asScala().id()); result.put("message", message); return CompletableFuture.completedFuture(Results.status(statusCode, error(result))); }