String msg = String.format("Parser tried to parse request %s as text body with charset %s, but it contains invalid characters!", request.id(), encodingToTry); logger.warn(msg); return F.Either.Left(e); } catch (Exception e) { String msg = "Unexpected exception!"; logger.error(msg, e); return F.Either.Left(e);
String msg = String.format("Parser tried to parse request %s as text body with charset %s, but it contains invalid characters!", request.id(), encodingToTry); logger.warn(msg); return F.Either.Left(e); } catch (Exception e) { String msg = "Unexpected exception!"; logger.error(msg, e); return F.Either.Left(e);
String msg = String.format("Parser tried to parse request %s as text body with charset %s, but it contains invalid characters!", request.id(), encodingToTry); logger.warn(msg); return F.Either.Left(e); } catch (Exception e) { String msg = "Unexpected exception!"; logger.error(msg, e); return F.Either.Left(e);
/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }
/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }
/** * Acceptor for JSON WebSockets. * * @param in The class of the incoming messages, used to decode them from the JSON. * @param <In> The websocket's input type (what it receives from clients) * @param <Out> The websocket's output type (what it writes to clients) * @return The WebSocket acceptor. */ public static <In, Out> MappedWebSocketAcceptor<In, Out> json(Class<In> in) { return new MappedWebSocketAcceptor<>(Scala.partialFunction(message -> { try { if (message instanceof Message.Binary) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Binary) message).data().iterator().asInputStream(), in)); } else if (message instanceof Message.Text) { return F.Either.Left(play.libs.Json.mapper().readValue(((Message.Text) message).data(), in)); } } catch (Exception e) { return F.Either.Right(new Message.Close(CloseCodes.Unacceptable(), e.getMessage())); } throw Scala.noMatch(); }), outMessage -> { try { return new Message.Text(play.libs.Json.mapper().writeValueAsString(outMessage)); } catch (Exception e) { throw new RuntimeException(e); } }); }
/** * Delegates underlying functionality to another body parser and converts the * result to Java API. */ @Override public play.libs.streams.Accumulator<ByteString, F.Either<Result, Http.MultipartFormData<A>>> apply(Http.RequestHeader request) { return delegate.apply(request.asScala()) .asJava() .map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { final play.api.mvc.MultipartFormData<A> scalaData = result.right().get(); return F.Either.Right(new DelegatingMultipartFormData(scalaData)); } }, JavaParsers.trampoline() ); }
/** * Delegates underlying functionality to another body parser and converts the * result to Java API. */ @Override public play.libs.streams.Accumulator<ByteString, F.Either<Result, Http.MultipartFormData<A>>> apply(Http.RequestHeader request) { return delegate.apply(request.asScala()) .asJava() .map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { final play.api.mvc.MultipartFormData<A> scalaData = result.right().get(); return F.Either.Right(new DelegatingMultipartFormData(scalaData)); } }, JavaParsers.trampoline() ); }
/** * Delegates underlying functionality to another body parser and converts the * result to Java API. */ @Override public play.libs.streams.Accumulator<ByteString, F.Either<Result, Http.MultipartFormData<A>>> apply(Http.RequestHeader request) { return delegate.apply(request.asScala()) .asJava() .map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { final play.api.mvc.MultipartFormData<A> scalaData = result.right().get(); return F.Either.Right(new DelegatingMultipartFormData(scalaData)); } }, JavaParsers.trampoline() ); }
@Override public CompletionStage<F.Either<Result, Flow<Message, Message, ?>>> apply(Http.RequestHeader request) { return f.apply(request).thenApply(resultOrFlow -> { if (resultOrFlow.left.isPresent()) { return F.Either.Left(resultOrFlow.left.get()); } else { Flow<Message, Message, ?> flow = AkkaStreams.bypassWith( Flow.<Message>create().collect(inMapper), play.api.libs.streams.AkkaStreams.onlyFirstCanFinishMerge(2), resultOrFlow.right.get().map(outMapper::apply) ); return F.Either.Right(flow); } }); } };
@Override public CompletionStage<F.Either<Result, Flow<Message, Message, ?>>> apply(Http.RequestHeader request) { return f.apply(request).thenApply(resultOrFlow -> { if (resultOrFlow.left.isPresent()) { return F.Either.Left(resultOrFlow.left.get()); } else { Flow<Message, Message, ?> flow = AkkaStreams.bypassWith( Flow.<Message>create().collect(inMapper), play.api.libs.streams.AkkaStreams.onlyFirstCanFinishMerge(2), resultOrFlow.right.get().map(outMapper::apply) ); return F.Either.Right(flow); } }); } };
@Override public CompletionStage<F.Either<Result, Flow<Message, Message, ?>>> apply(Http.RequestHeader request) { return f.apply(request).thenApply(resultOrFlow -> { if (resultOrFlow.left.isPresent()) { return F.Either.Left(resultOrFlow.left.get()); } else { Flow<Message, Message, ?> flow = AkkaStreams.bypassWith( Flow.<Message>create().collect(inMapper), play.api.libs.streams.AkkaStreams.onlyFirstCanFinishMerge(2), resultOrFlow.right.get().map(outMapper::apply) ); return F.Either.Right(flow); } }); } };
static <A, B> Accumulator<ByteString, F.Either<Result, A>> delegate(play.api.mvc.BodyParser<B> delegate, Function<B, A> transform, Http.RequestHeader request) { Accumulator<ByteString, scala.util.Either<play.api.mvc.Result, B>> javaAccumulator = delegate.apply(request.asScala()).asJava(); return javaAccumulator.map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { return F.Either.Right(transform.apply(result.right().get())); } }, JavaParsers.trampoline()); } }
static <A, B> Accumulator<ByteString, F.Either<Result, A>> delegate(play.api.mvc.BodyParser<B> delegate, Function<B, A> transform, Http.RequestHeader request) { Accumulator<ByteString, scala.util.Either<play.api.mvc.Result, B>> javaAccumulator = delegate.apply(request.asScala()).asJava(); return javaAccumulator.map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { return F.Either.Right(transform.apply(result.right().get())); } }, JavaParsers.trampoline()); } }
static <A, B> Accumulator<ByteString, F.Either<Result, A>> delegate(play.api.mvc.BodyParser<B> delegate, Function<B, A> transform, Http.RequestHeader request) { Accumulator<ByteString, scala.util.Either<play.api.mvc.Result, B>> javaAccumulator = delegate.apply(request.asScala()).asJava(); return javaAccumulator.map(result -> { if (result.isLeft()) { return F.Either.Left(result.left().get().asJava()); } else { return F.Either.Right(transform.apply(result.right().get())); } }, JavaParsers.trampoline()); } }