/** * Set a Multipart Form url encoded body to this request saving it as a raw body. * * @param data the multipart-form parameters * @param temporaryFileCreator the temporary file creator. * @param mat a Akka Streams Materializer * @return the modified builder */ public RequestBuilder bodyRaw(List<MultipartFormData.Part<Source<ByteString, ?>>> data, Files.TemporaryFileCreator temporaryFileCreator, Materializer mat) { String boundary = MultipartFormatter.randomBoundary(); try { ByteString materializedData = MultipartFormatter .transform(Source.from(data), boundary) .runWith(Sink.reduce(ByteString::concat), mat) .toCompletableFuture() .get(); play.api.mvc.RawBuffer buffer = new play.api.mvc.RawBuffer(materializedData.size(), temporaryFileCreator.asScala(), materializedData); return body(new RequestBody(JavaParsers.toJavaRaw(buffer)), MultipartFormatter.boundaryToContentType(boundary)); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Failure while materializing Multipart/Form Data", e); } }
/** * Set a Multipart Form url encoded body to this request saving it as a raw body. * * @param data the multipart-form parameters * @param temporaryFileCreator the temporary file creator. * @param mat a Akka Streams Materializer * @return the modified builder */ public RequestBuilder bodyRaw(List<MultipartFormData.Part<Source<ByteString, ?>>> data, Files.TemporaryFileCreator temporaryFileCreator, Materializer mat) { String boundary = MultipartFormatter.randomBoundary(); try { ByteString materializedData = MultipartFormatter .transform(Source.from(data), boundary) .runWith(Sink.reduce(ByteString::concat), mat) .toCompletableFuture() .get(); play.api.mvc.RawBuffer buffer = new play.api.mvc.RawBuffer(materializedData.size(), temporaryFileCreator.asScala(), materializedData); return body(new RequestBody(JavaParsers.toJavaRaw(buffer)), MultipartFormatter.boundaryToContentType(boundary)); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Failure while materializing Multipart/Form Data", e); } }
/** * Set a Multipart Form url encoded body to this request saving it as a raw body. * * @param data the multipart-form parameters * @param temporaryFileCreator the temporary file creator. * @param mat a Akka Streams Materializer * @return the modified builder */ public RequestBuilder bodyRaw(List<MultipartFormData.Part<Source<ByteString, ?>>> data, Files.TemporaryFileCreator temporaryFileCreator, Materializer mat) { String boundary = MultipartFormatter.randomBoundary(); try { ByteString materializedData = MultipartFormatter .transform(Source.from(data), boundary) .runWith(Sink.reduce(ByteString::concat), mat) .toCompletableFuture() .get(); play.api.mvc.RawBuffer buffer = new play.api.mvc.RawBuffer(materializedData.size(), temporaryFileCreator.asScala(), materializedData); return body(new RequestBody(JavaParsers.toJavaRaw(buffer)), MultipartFormatter.boundaryToContentType(boundary)); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Failure while materializing Multipart/Form Data", e); } }
public CompletionStage<A> run(Source<E, ?> source, Materializer mat) { return source.runWith(sink, mat); }
public CompletionStage<A> run(Source<E, ?> source, Materializer mat) { return source.runWith(toSink, mat); }
public Publisher<Object> asPublisher() { return source.runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat); } }
public CompletionStage<A> run(Materializer mat) { return Source.<E>empty().runWith(sink, mat); }
@Override public Optional<Instant> retrieveLastSuccessfulStreamEnd() { final Source<Optional<Instant>, NotUsed> source = retrieveLastSuccessfulStreamEndAsync(); final CompletionStage<Optional<Instant>> done = source.runWith(Sink.head(), mat); try { return done.toCompletableFuture().get(BLOCKING_TIMEOUT_SECS, TimeUnit.SECONDS); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } catch (final ExecutionException | TimeoutException e) { throw new IllegalStateException(e); } }
@Override public Optional<Instant> retrieveLastSuccessfulStreamEnd() { final Source<Optional<Instant>, NotUsed> source = retrieveLastSuccessfulStreamEndAsync(); final CompletionStage<Optional<Instant>> done = source.runWith(Sink.head(), mat); try { return done.toCompletableFuture().get(BLOCKING_TIMEOUT_SECS, TimeUnit.SECONDS); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } catch (final ExecutionException | TimeoutException e) { throw new IllegalStateException(e); } }
@Override @SuppressWarnings("unchecked") protected <T> Publisher<T> filter(Processor processor, final Class<T> filterClass) { Source src = Source.fromPublisher(processor) .filter(o -> filterClass.isAssignableFrom(o.getClass())); return (Publisher<T>) src.runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat); }
@Override @SuppressWarnings("unchecked") protected Publisher hide(Processor processor) { return (Publisher) Source .fromPublisher(processor) .runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), mat); }
private CompletionStage<Done> dropUndefinedIndices(final String collectionName, final List<Index> definedIndices) { return getIndicesExceptDefaultIndex(collectionName) .flatMapConcat(existingIndices -> { LOGGER.info("Drop undefined indices - Existing indices are: {}", existingIndices); final List<String> indicesToDrop = getUndefinedIndexNames(existingIndices, definedIndices); LOGGER.info("Dropping undefined indices: {}", indicesToDrop); return dropIndices(collectionName, indicesToDrop); }) .runWith(Sink.ignore(), materializer); }
private CompletionStage<Done> dropUndefinedIndices(final String collectionName, final List<Index> definedIndices) { return getIndicesExceptDefaultIndex(collectionName) .flatMapConcat(existingIndices -> { LOGGER.info("Drop undefined indices - Existing indices are: {}", existingIndices); final List<String> indicesToDrop = getUndefinedIndexNames(existingIndices, definedIndices); LOGGER.info("Dropping undefined indices: {}", indicesToDrop); return dropIndices(collectionName, indicesToDrop); }) .runWith(Sink.ignore(), materializer); }
Publisher<HashTag> hashtags() { return Source .fromPublisher(this.tweets()) .map(Tweet::getHashTags) .reduce((a, b) -> { Set<HashTag> tags = new HashSet<>(); tags.addAll(a); tags.addAll(b); return tags; }) .mapConcat(param -> param) .runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), this.actorMaterializer); }
private BiFunction<Accumulator<E, A>, Throwable, Void> handler(final Subscription sub) { return (acc, error) -> { if (acc != null) { Source.fromPublisher(publisher(sub)).runWith(acc.toSink().mapMaterializedValue(this::completeResultWith), materializer); } else { // On error sub.cancel(); result.completeExceptionally(error); } return null; }; }
private BiFunction<Accumulator<E, A>, Throwable, Void> handler(final Subscription sub) { return (acc, error) -> { if (acc != null) { Source.fromPublisher(publisher(sub)).runWith(acc.toSink().mapMaterializedValue(this::completeResultWith), materializer); } else { // On error sub.cancel(); result.completeExceptionally(error); } return null; }; }
public static void main(String[] args) throws Exception { Config cfg = ConfigFactory.parseResources(AkkaRange.class, "/akka-streams.conf").resolve(); ActorSystem actorSystem = ActorSystem.create("sys", cfg); ActorMaterializer materializer = ActorMaterializer.create(actorSystem); Source<Integer, NotUsed> source = Source.repeat(1) .map(v -> v + 1); Publisher<Integer> p = source.runWith(Sink.asPublisher(AsPublisher.WITH_FANOUT), materializer); p.subscribe(println()); Thread.sleep(1000); actorSystem.terminate(); } }
private void deleteThingFromSearchIndex() { final StartedTimer timer = DittoMetrics.expiringTimer(TRACE_THING_DELETE).tag(UPDATE_TYPE_TAG, "delete").build(); circuitBreaker.callWithCircuitBreakerCS(() -> searchUpdaterPersistence .delete(thingId) .via(stopTimer(timer)) .runWith(Sink.last(), materializer) .whenComplete(this::handleDeletion)); }
private void deleteThingFromSearchIndex() { final StartedTimer timer = DittoMetrics.expiringTimer(TRACE_THING_DELETE).tag(UPDATE_TYPE_TAG, "delete").build(); circuitBreaker.callWithCircuitBreakerCS(() -> searchUpdaterPersistence .delete(thingId) .via(stopTimer(timer)) .runWith(Sink.last(), materializer) .whenComplete(this::handleDeletion)); }
private void namespaceReport(final SudoRetrieveNamespaceReport namespaceReport) { final DittoHeaders dittoHeaders = namespaceReport.getDittoHeaders(); LogUtil.enhanceLogWithCorrelationId(log, dittoHeaders.getCorrelationId()); log.info("Processing SudoRetrieveNamespaceReport command: {}", namespaceReport); PatternsCS.pipe(searchPersistence.generateNamespaceCountReport() .runWith(Sink.head(), materializer), dispatcher) .to(getSender()); }