/** * Constructs a left side of the disjoint union, as opposed to the Right side. * * @param value The value of the left side * @param <L> the left type * @param <R> the right type * @return A left sided disjoint union */ public static <L, R> Either<L, R> Left(L value) { return new Either<L, R>(Optional.of(value), Optional.<R>empty()); }
try { CharsetDecoder decoder = encodingToTry.newDecoder().onMalformedInput(CodingErrorAction.REPORT); return F.Either.Right(decoder.decode(byteBuffer).toString()); } catch (CharacterCodingException 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);
public static <A, B> Either<A, B> _2(B value) { return new Either(None, Some(value)); }
public static <A, B> Either<A, B> _1(A value) { return new Either(Some(value), None); }
@Override public void invoke(Promise<F.Tuple<Integer, Promise<Object>>> completed) { F.Tuple<Integer, Promise<Object>> value = completed.getOrNull(); switch (value._1) { case 1: result.invoke(F.Either.<A, B>_1((A) value._2.getOrNull())); break; case 2: result.invoke(F.Either.<A, B>_2((B) value._2.getOrNull())); break; } } });
/** * Constructs a right side of the disjoint union, as opposed to the Left side. * * @param value The value of the right side * @return A right sided disjoint union */ public static <L, R> Either<L, R> Right(R value) { return new Either<L, R>(Optional.<L>empty(), Optional.of(value)); }
/** * Constructs a left side of the disjoint union, as opposed to the Right side. * * @param value The value of the left side * @return A left sided disjoint union */ public static <L, R> Either<L, R> Left(L value) { return new Either<L, R>(Optional.of(value), Optional.<R>empty()); }
try { CharsetDecoder decoder = encodingToTry.newDecoder().onMalformedInput(CodingErrorAction.REPORT); return F.Either.Right(decoder.decode(byteBuffer).toString()); } catch (CharacterCodingException 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);
try { CharsetDecoder decoder = encodingToTry.newDecoder().onMalformedInput(CodingErrorAction.REPORT); return F.Either.Right(decoder.decode(byteBuffer).toString()); } catch (CharacterCodingException 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 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 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 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 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); } }); } };