Java Code Examples for akka.japi.Pair#second()

The following examples show how to use akka.japi.Pair#second() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventSnapshotCleanupCoordinator.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static JsonObject renderAction(final Pair<Instant, CleanupPersistenceResponse> element) {
    final CleanupPersistenceResponse response = element.second();
    final DittoHeaders headers = response.getDittoHeaders();
    final int status = response.getStatusCodeValue();
    final String start = headers.getOrDefault(START, "unknown");
    final String message = getResponseMessage(response);
    final String tagLine = String.format("%d start=%s <%s>", status, start, message);
    return JsonObject.newBuilder()
            .set(element.first().toString(), tagLine)
            .build();
}
 
Example 2
Source File: MergeSortedAsPairTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private void materializeTestProbes() {
    final Pair<Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>,
            TestSubscriber.Probe<Pair<Integer, Integer>>>
            probes =
            mergeSortedAsPairWithMat(TestSource.probe(system), TestSource.probe(system))
                    .toMat(TestSink.probe(system), Keep.both())
                    .run(mat);
    source1Probe = probes.first().first();
    source2Probe = probes.first().second();
    sinkProbe = probes.second();
}
 
Example 3
Source File: ResumeSourceTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void init() {
    system = ActorSystem.create();
    mat = ActorMaterializer.create(system);

    rematerializeSource();

    // materialize sink once - it never fails.
    final Sink<Integer, TestSubscriber.Probe<Integer>> sink = TestSink.probe(system);
    final Pair<TestSubscriber.Probe<Integer>, Sink<Integer, NotUsed>> sinkPair = sink.preMaterialize(mat);
    sinkProbe = sinkPair.first();
    testSink = sinkPair.second();
}
 
Example 4
Source File: TransistorTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Connect a transistor to 2 test collectors and a test sink.
 */
@Before
public void init() {
    system = ActorSystem.create();
    final Source<Integer, TestPublisher.Probe<Integer>> collectorSource = TestSource.probe(system);
    final Source<Integer, TestPublisher.Probe<Integer>> baseSource = TestSource.probe(system);
    final Sink<Integer, TestSubscriber.Probe<Integer>> emitterSink = TestSink.probe(system);
    final Transistor<Integer> underTest = Transistor.of();

    final Graph<SourceShape<Integer>, Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>>
            collectorGateTransistor =
            GraphDSL$.MODULE$.create3(
                    collectorSource, baseSource, underTest,
                    (collector, base, notUsed) -> Pair.create(collector, base),
                    (builder, collectorShape, baseShape, transistorShape) -> {
                        builder.from(collectorShape.out()).toInlet(transistorShape.in0());
                        builder.from(baseShape.out()).toInlet(transistorShape.in1());
                        return SourceShape.of(transistorShape.out());
                    });

    final Pair<Pair<TestPublisher.Probe<Integer>, TestPublisher.Probe<Integer>>, TestSubscriber.Probe<Integer>> m =
            Source.fromGraph(collectorGateTransistor)
                    .toMat(emitterSink, Keep.both())
                    .run(ActorMaterializer.create(system));

    collector = m.first().first();
    base = m.first().second();
    emitter = m.second();
}
 
Example 5
Source File: BackgroundSyncStream.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private Source<Metadata, NotUsed> filterForInconsistency(final Pair<Metadata, Metadata> pair) {
    final Metadata persisted = pair.first();
    final Metadata indexed = pair.second();
    final int comparison = compareMetadata(persisted, indexed);
    final Instant toleranceCutOff = Instant.now().minus(toleranceWindow);
    if (comparison < 0) {
        // persisted thing is not in search index; trigger update if the snapshot is not too recent
        return isInsideToleranceWindow(persisted, toleranceCutOff)
                ? Source.empty()
                : Source.single(persisted).log("PersistedAndNotIndexed");
    } else if (comparison > 0) {
        // indexed thing is not persisted; trigger update if the index entry is not too recent
        return isInsideToleranceWindow(indexed, toleranceCutOff)
                ? Source.empty()
                : Source.single(indexed).log("NotPersistedAndIndexed");
    } else {
        // IDs match
        if (indexed.getThingId().isDummy()) {
            // sanity check: entry should not be dummy
            return Source.failed(new IllegalStateException("Unexpected double-dummy entry: " + pair));
        } else if (isInsideToleranceWindow(indexed, toleranceCutOff)) {
            // ignore entries within tolerance window
            return Source.empty();
        } else {
            return emitUnlessConsistent(persisted, indexed);
        }
    }
}
 
Example 6
Source File: HttpPublisherActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private HttpRequest createRequest(final HttpPublishTarget publishTarget, final ExternalMessage message) {
    final Pair<Iterable<HttpHeader>, ContentType> headersPair = getHttpHeadersPair(message);
    final HttpRequest requestWithoutEntity = factory.newRequest(publishTarget).addHeaders(headersPair.first());
    final ContentType contentTypeHeader = headersPair.second();
    if (contentTypeHeader != null) {
        final HttpEntity.Strict httpEntity =
                HttpEntities.create(contentTypeHeader.contentType(), getPayloadAsBytes(message));
        return requestWithoutEntity.withEntity(httpEntity);
    } else if (message.isTextMessage()) {
        return requestWithoutEntity.withEntity(getTextPayload(message));
    } else {
        return requestWithoutEntity.withEntity(getBytePayload(message));
    }
}
 
Example 7
Source File: MessageMappingProcessorActor.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private CompletionStage<Collection<OutboundSignalWithId>> enrichAndFilterSignal(
        final Pair<OutboundSignalWithId, FilteredTopic> outboundSignalWithExtraFields) {

    final OutboundSignalWithId outboundSignal = outboundSignalWithExtraFields.first();
    final FilteredTopic filteredTopic = outboundSignalWithExtraFields.second();
    final Optional<JsonFieldSelector> extraFieldsOptional =
            Optional.ofNullable(filteredTopic).flatMap(FilteredTopic::getExtraFields);
    if (extraFieldsOptional.isEmpty()) {
        return CompletableFuture.completedFuture(Collections.singletonList(outboundSignal));
    }
    final JsonFieldSelector extraFields = extraFieldsOptional.get();
    final Target target = outboundSignal.getTargets().get(0);

    final ThingId thingId = ThingId.of(outboundSignal.getEntityId());
    final DittoHeaders headers = DittoHeaders.newBuilder()
            .authorizationContext(target.getAuthorizationContext())
            // schema version is always the latest for connectivity signal enrichment.
            .schemaVersion(JsonSchemaVersion.LATEST)
            .build();
    final CompletionStage<JsonObject> extraFuture =
            signalEnrichmentFacade.retrievePartialThing(thingId, extraFields, headers, outboundSignal.getSource());

    return extraFuture.thenApply(outboundSignal::setExtra)
            .thenApply(outboundSignalWithExtra -> applyFilter(outboundSignalWithExtra, filteredTopic))
            .exceptionally(error -> {
                logger.withCorrelationId(outboundSignal.getSource())
                        .warning("Could not retrieve extra data due to: {} {}",
                                error.getClass().getSimpleName(), error.getMessage());
                // recover from all errors to keep message-mapping-stream running despite enrichment failures
                return Collections.singletonList(recoverFromEnrichmentError(outboundSignal, target, error));
            });
}
 
Example 8
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void basicAuth() throws Exception {
    // GIVEN: the connection has plain credentials in the URI
    connection = connection.toBuilder()
            .uri("http://username:[email protected]:" + binding.localAddress().getPort() + "/path/prefix/")
            .build();
    final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig());
    final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
            newSourceSinkQueues(underTest);
    final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
    final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
    final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));
    final HttpResponse response = HttpResponse.create().withStatus(StatusCodes.OK);

    // WHEN: request-response cycle is carried out
    responseQueue.offer(CompletableFuture.completedFuture(response));
    sourceQueue.offer(request);
    final HttpRequest actualRequest = requestQueue.take();

    // THEN: actual received request has a basic auth header
    assertThat(actualRequest.getHeader(Authorization.class))
            .contains(Authorization.basic("username", "password"));
}
 
Example 9
Source File: AkkaStreamsUtil.java    From netty-reactive-streams with Apache License 2.0 5 votes vote down vote up
public static <In, Out> Processor<In, Out> flowToProcessor(Flow<In, Out, ?> flow, Materializer materializer) {
    Pair<Subscriber<In>, Publisher<Out>> pair =
            Source.<In>asSubscriber()
                    .via(flow)
                    .toMat(Sink.<Out>asPublisher(AsPublisher.WITH_FANOUT), Keep.<Subscriber<In>, Publisher<Out>>both())
                    .run(materializer);

    return new DelegateProcessor<>(pair.first(), pair.second());
}
 
Example 10
Source File: ResumeSourceTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private void rematerializeSource() {
    final Source<Integer, TestPublisher.Probe<Integer>> source = TestSource.probe(system);
    final Pair<TestPublisher.Probe<Integer>, Source<Integer, NotUsed>> sourcePair = source.preMaterialize(mat);
    sourceProbe = sourcePair.first();
    testSource = sourcePair.second();
}
 
Example 11
Source File: HttpPublisherActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private void processResponse(final Pair<Try<HttpResponse>, HttpPushContext> responseWithMessage) {
    final Try<HttpResponse> tryResponse = responseWithMessage.first();
    final HttpPushContext context = responseWithMessage.second();
    final ExternalMessage message = context.getExternalMessage();
    final Uri requestUri = context.getRequestUri();
    if (tryResponse.isFailure()) {
        final Throwable error = tryResponse.toEither().left().get();
        final String errorDescription = MessageFormat.format("Failed to send HTTP request to <{0}>.",
                stripUserInfo(requestUri));
        log.debug("Failed to send message <{}> due to <{}>", message, error);
        responsePublishedMonitor.failure(message, errorDescription);
        escalate(error, errorDescription);
    } else {
        final HttpResponse response = tryResponse.toEither().right().get();
        log.debug("Sent message <{}>. Got response <{} {}>", message, response.status(), response.getHeaders());
        if (response.status().isSuccess()) {
            responsePublishedMonitor.success(message,
                    "HTTP call to <{0}> successfully responded with status <{1}>.",
                    stripUserInfo(requestUri), response.status());
            response.discardEntityBytes(materializer);
        } else {
            getResponseBody(response, materializer)
                    .thenAccept(body -> responsePublishedMonitor.failure(message,
                            "HTTP call to <{0}> responded with status <{1}> and body: {2}.",
                            stripUserInfo(requestUri),
                            response.status(), body)
                    )
                    .exceptionally(bodyReadError -> {
                        responsePublishedMonitor.failure(message,
                                "HTTP call to <{0}> responded with status <{1}>. Failed to read body within {2} ms",
                                stripUserInfo(requestUri), response.status(), READ_BODY_TIMEOUT_MS);
                        LogUtil.enhanceLogWithCorrelationId(log, message.getInternalHeaders());
                        log.info("Got <{}> when reading body of publish response to <{}>", bodyReadError,
                                message);
                        return null;
                    });
        }
    }
}
 
Example 12
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void withHttpProxyConfig() throws Exception {
    // GIVEN: the connection's URI points to an unreachable host
    connection = connection.toBuilder()
            .uri("http://no.such.host.example:42/path/prefix/")
            .build();

    // GIVEN: the HTTP-push factory has the proxy configured to the test server binding
    final HttpPushFactory underTest = HttpPushFactory.of(connection, new HttpPushConfig() {
        @Override
        public int getMaxQueueSize() {
            return 0;
        }

        @Override
        public HttpProxyConfig getHttpProxyConfig() {
            return getEnabledProxyConfig(binding);
        }
    });
    final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
            newSourceSinkQueues(underTest);
    final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
    final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
    final HttpRequest request = underTest.newRequest(HttpPublishTarget.of("PUT:/path/appendage/"));

    // WHEN: request is made
    sourceQueue.offer(request);

    // THEN: CONNECT request is made to the Akka HTTP test server.
    // THEN: Akka HTTP server rejects CONNECT request, creating a failed response
    final Optional<Try<HttpResponse>> optionalTryResponse = sinkQueue.pull().toCompletableFuture().join();
    assertThat(optionalTryResponse).isNotEmpty();
    final Try<HttpResponse> tryResponse = optionalTryResponse.get();
    assertThat(tryResponse).isInstanceOf(Failure.class);
    assertThat(tryResponse.failed().get()).isInstanceOf(ProxyConnectionFailedException.class);
    assertThat(tryResponse.failed().get().getMessage())
            .contains("proxy rejected to open a connection to no.such.host.example:42 with status code: 400");
}
 
Example 13
Source File: HttpPushFactoryTest.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void handleFailure() throws Exception {
    new TestKit(actorSystem) {{
        // GIVEN: An HTTP-push connection is established against localhost.
        final HttpPushFactory underTest = HttpPushFactory.of(connection, connectionConfig.getHttpPushConfig());
        final Pair<SourceQueueWithComplete<HttpRequest>, SinkQueueWithCancel<Try<HttpResponse>>> pair =
                newSourceSinkQueues(underTest);
        final SourceQueueWithComplete<HttpRequest> sourceQueue = pair.first();
        final SinkQueueWithCancel<Try<HttpResponse>> sinkQueue = pair.second();
        final HttpRequest request1 = underTest.newRequest(HttpPublishTarget.of("1"));
        final HttpRequest request2 = underTest.newRequest(HttpPublishTarget.of("2"));
        final HttpRequest request3 = underTest.newRequest(HttpPublishTarget.of("3"));
        final HttpResponse response1 = HttpResponse.create().withStatus(StatusCodes.IM_A_TEAPOT);
        final HttpResponse response3 = HttpResponse.create().withStatus(StatusCodes.BLOCKED_BY_PARENTAL_CONTROLS);

        // GIVEN: The connection is working.
        responseQueue.offer(CompletableFuture.completedFuture(response1));
        sourceQueue.offer(request1);
        assertThat(requestQueue.take().getUri()).isEqualTo(request1.getUri());
        final Try<HttpResponse> responseOrError1 = pullResponse(sinkQueue);
        assertThat(responseOrError1.isSuccess()).isTrue();
        assertThat(responseOrError1.get().status()).isEqualTo(response1.status());

        // WHEN: In-flight request is killed
        // THEN: Akka HTTP responds with status 500
        responseQueue.offer(new CompletableFuture<>());
        sourceQueue.offer(request2);
        assertThat(requestQueue.take().getUri()).isEqualTo(request2.getUri());
        shutdownAllServerStreams();
        final Try<HttpResponse> responseOrError2 = pullResponse(sinkQueue);
        assertThat(responseOrError2.isSuccess()).isTrue();
        assertThat(responseOrError2.get().status()).isEqualTo(StatusCodes.INTERNAL_SERVER_ERROR);

        // WHEN: HTTP server becomes available again.
        // THEN: A new request resumes and the previously failed request is discarded.
        responseQueue.offer(CompletableFuture.completedFuture(response3));
        sourceQueue.offer(request3);
        assertThat(requestQueue.take().getUri()).isEqualTo(request3.getUri());
        final Try<HttpResponse> responseOrError3 = pullResponse(sinkQueue);
        assertThat(responseOrError3.isSuccess()).isTrue();
        assertThat(responseOrError3.get().status()).isEqualTo(response3.status());
    }};
}