akka.util.ByteString Java Examples
The following examples show how to use
akka.util.ByteString.
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: StatsRoute.java From ditto with Eclipse Public License 2.0 | 6 votes |
private Route handleSudoCountThingsPerRequest(final RequestContext ctx, final SudoCountThings command) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); Source.single(command) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), AbstractHttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); final CompletionStage<HttpResponse> allThingsCountHttpResponse = Source.fromCompletionStage(httpResponseFuture) .flatMapConcat(httpResponse -> httpResponse.entity().getDataBytes()) .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(Integer::valueOf) .map(count -> JsonObject.newBuilder().set("allThingsCount", count).build()) .map(jsonObject -> HttpResponse.create() .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(jsonObject.toString())) .withStatus(HttpStatusCode.OK.toInt())) .runWith(Sink.head(), materializer); return completeWithFuture(allThingsCountHttpResponse); }
Example #2
Source File: AbstractRoute.java From ditto with Eclipse Public License 2.0 | 6 votes |
protected Route handlePerRequest(final RequestContext ctx, final DittoHeaders dittoHeaders, final Source<ByteString, ?> payloadSource, final Function<String, Command> requestJsonToCommandFunction, @Nullable final Function<JsonValue, JsonValue> responseTransformFunction) { // check if Akka HTTP timeout was overwritten by our code (e.g. for claim messages) final boolean increasedAkkaHttpTimeout = ctx.getRequest().getHeader(TimeoutAccess.class) .map(TimeoutAccess::timeoutAccess) .map(akka.http.javadsl.TimeoutAccess::getTimeout) .map(scalaDuration -> Duration.ofNanos(scalaDuration.toNanos())) .filter(akkaHttpTimeout -> akkaHttpTimeout.compareTo(commandConfig.getMaxTimeout()) > 0) .isPresent(); if (increasedAkkaHttpTimeout) { return doHandlePerRequest(ctx, dittoHeaders, payloadSource, requestJsonToCommandFunction, responseTransformFunction); } else { return withCustomRequestTimeout(dittoHeaders.getTimeout().orElse(null), this::validateCommandTimeout, null, // don't set default timeout in order to use the configured akka-http default timeout -> doHandlePerRequest(ctx, dittoHeaders.toBuilder().timeout(timeout).build(), payloadSource, requestJsonToCommandFunction, responseTransformFunction)); } }
Example #3
Source File: WebSocketDataCenterServer.java From ts-reaktive with MIT License | 6 votes |
private Flow<Message,Message,?> flow(ActorRef shardRegion) { return Flow.<Message>create() .map(msg -> { if (msg.isText()) { log.warn("Ignoring unexpected text-kind web socket message {}", msg); return Option.<Query.EventEnvelope>none(); } else { return Option.<Query.EventEnvelope>some(Query.EventEnvelope.parseFrom(msg.asBinaryMessage().getStrictData().toArray())); } }) .filter(o -> o.isDefined()) .map(o -> o.get()) .mapAsync(maxInFlight, e -> ask(shardRegion, e, timeout)) .map(resp -> (Long) resp) .map(l -> BinaryMessage.create(ByteString.fromArray(EventsPersisted.newBuilder().setOffset(l).build().toByteArray()))); }
Example #4
Source File: SolutionSettingsControllerTest.java From remote-monitoring-services-java with MIT License | 6 votes |
@Test(timeout = SolutionSettingsControllerTest.TIMEOUT) @Category({UnitTest.class}) public void setLogoShouldReturnGivenName() throws BaseException, ExecutionException, InterruptedException, UnsupportedEncodingException, URISyntaxException { // Arrange String name = rand.NextString(); Logo model = new Logo(null, null, name, false); setLogoMockSetup(model); controller = new SolutionSettingsController(mockStorage, mockActions); Http.Response mockResponse = TestUtils.setRequest(SolutionSettingsControllerTest.LOGO_BODY); // Act byte[] bytes = TestUtils.getBytes(controller.setLogoAsync().toCompletableFuture().get()); byte[] emptyBytes = new byte[0]; // Assert assertEquals(ByteString.fromArray(bytes), ByteString.fromArray(emptyBytes)); Mockito.verify(mockResponse).setHeader(Logo.NAME_HEADER, name); Mockito.verify(mockResponse).setHeader(Logo.IS_DEFAULT_HEADER, Boolean.toString(false)); }
Example #5
Source File: CompressedDDataHandler.java From ditto with Eclipse Public License 2.0 | 6 votes |
@Override public CompletionStage<Collection<ActorRef>> getSubscribers(final Collection<ByteString> topic) { return get(Replicator.readLocal()).thenApply(optional -> { if (optional.isPresent()) { final ORMultiMap<ActorRef, ByteString> mmap = optional.get(); ddataMetrics.set((long) mmap.size()); return JavaConverters.mapAsJavaMap(mmap.entries()) .entrySet() .stream() .filter(entry -> topic.stream().anyMatch(entry.getValue()::contains)) .map(Map.Entry::getKey) .collect(Collectors.toList()); } else { ddataMetrics.set(0L); return Collections.emptyList(); } }); }
Example #6
Source File: SolutionSettingsControllerTest.java From remote-monitoring-services-java with MIT License | 6 votes |
@Test(timeout = SolutionSettingsControllerTest.TIMEOUT) @Category({UnitTest.class}) public void setLogoShouldReturnGivenLogo() throws BaseException, ExecutionException, InterruptedException, UnsupportedEncodingException, URISyntaxException { // Arrange String image = rand.NextString(); String type = rand.NextString(); Logo model = new Logo(image, type, null, false); setLogoMockSetup(model); controller = new SolutionSettingsController(mockStorage, mockActions); Http.Response mockResponse = TestUtils.setRequest(SolutionSettingsControllerTest.LOGO_BODY); // Act byte[] bytes = TestUtils.getBytes(controller.setLogoAsync().toCompletableFuture().get()); byte[] bytesold = Base64.getDecoder().decode(model.getImage().getBytes()); // Assert assertEquals(ByteString.fromArray(bytes), ByteString.fromArray(bytesold)); Mockito.verify(mockResponse).setHeader(Logo.IS_DEFAULT_HEADER, Boolean.toString(false)); }
Example #7
Source File: SolutionSettingsControllerTest.java From remote-monitoring-services-java with MIT License | 6 votes |
@Test(timeout = SolutionSettingsControllerTest.TIMEOUT) @Category({UnitTest.class}) public void setLogoShouldReturnGivenLogoAndName() throws BaseException, ExecutionException, InterruptedException, UnsupportedEncodingException, URISyntaxException { // Arrange String image = rand.NextString(); String type = rand.NextString(); String name = rand.NextString(); Logo model = new Logo(image, type, name, false); setLogoMockSetup(model); controller = new SolutionSettingsController(mockStorage, mockActions); Http.Response mockResponse = TestUtils.setRequest(SolutionSettingsControllerTest.LOGO_BODY); // Act byte[] bytes = TestUtils.getBytes(controller.setLogoAsync().toCompletableFuture().get()); byte[] bytesold = Base64.getDecoder().decode(model.getImage().getBytes()); // Assert assertEquals(ByteString.fromArray(bytes), ByteString.fromArray(bytesold)); Mockito.verify(mockResponse).setHeader(Logo.NAME_HEADER, name); Mockito.verify(mockResponse).setHeader(Logo.IS_DEFAULT_HEADER, Boolean.toString(false)); }
Example #8
Source File: DittoRuntimeExceptionToHttpResponseTest.java From ditto with Eclipse Public License 2.0 | 6 votes |
@Test public void conversionReturnsExpected() { final byte invalidApiVersion = -1; final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .correlationId(testName.getMethodName()) .build(); final CommandNotSupportedException exception = CommandNotSupportedException.newBuilder(invalidApiVersion) .dittoHeaders(dittoHeaders) .build(); final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(dittoHeaders); final List<HttpHeader> httpHeaders = new ArrayList<>(externalHeaders.size()); externalHeaders.forEach((key, value) -> httpHeaders.add(HttpHeader.parse(key, value))); final HttpResponse expected = HttpResponse.create() .withStatus(exception.getStatusCode().toInt()) .withHeaders(httpHeaders) .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(exception.toJsonString())); final DittoRuntimeExceptionToHttpResponse underTest = DittoRuntimeExceptionToHttpResponse.getInstance(headerTranslator); final HttpResponse actual = underTest.apply(exception); assertThat(actual).isEqualTo(expected); }
Example #9
Source File: StreamController.java From glowroot with Apache License 2.0 | 6 votes |
public Result stream() { Source<ByteString, ?> source = Source.<ByteString>actorRef(256, OverflowStrategy.dropNew()) .mapMaterializedValue(sourceActor -> { sourceActor.tell(ByteString.fromString("kiki"), null); try { MILLISECONDS.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } sourceActor.tell(ByteString.fromString("foo"), null); sourceActor.tell(ByteString.fromString("bar"), null); sourceActor.tell(new Status.Success(NotUsed.getInstance()), null); new CreateTraceEntry().traceEntryMarker(); return null; }); return ok().chunked(source); }
Example #10
Source File: SolutionSettingsControllerTest.java From remote-monitoring-services-java with MIT License | 6 votes |
@Test(timeout = SolutionSettingsControllerTest.TIMEOUT) @Category({UnitTest.class}) public void getLogoShouldReturnDefaultLogo() throws BaseException, ExecutionException, InterruptedException { // Arrange Logo model = Logo.Default; getLogoMockSetup(model); controller = new SolutionSettingsController(mockStorage, mockActions); Http.Response mockResponse = TestUtils.setRequest(SolutionSettingsControllerTest.LOGO_BODY); // Act Result result = controller.getLogoAsync().toCompletableFuture().get(); byte[] bytes = TestUtils.getBytes(result); byte[] bytesold = Base64.getDecoder().decode(model.getImage().getBytes()); // Assert assertEquals(ByteString.fromArray(bytes), ByteString.fromArray(bytesold)); Mockito.verify(mockResponse).setHeader(Logo.NAME_HEADER, Logo.Default.getName()); Mockito.verify(mockResponse).setHeader(Logo.IS_DEFAULT_HEADER, Boolean.toString(true)); }
Example #11
Source File: SolutionSettingsControllerTest.java From remote-monitoring-services-java with MIT License | 6 votes |
@Test(timeout = SolutionSettingsControllerTest.TIMEOUT) @Category({UnitTest.class}) public void getLogoShouldReturnExpectedNameAndType() throws BaseException, ExecutionException, InterruptedException { // Arrange String image = rand.NextString(); String type = rand.NextString(); String name = rand.NextString(); Logo model = new Logo(image, type, name, false); getLogoMockSetup(model); controller = new SolutionSettingsController(mockStorage, mockActions); Http.Response mockResponse = TestUtils.setRequest(SolutionSettingsControllerTest.LOGO_BODY); // Act Result result = controller.getLogoAsync().toCompletableFuture().get(); byte[] bytes = TestUtils.getBytes(result); byte[] bytesold = Base64.getDecoder().decode(model.getImage().getBytes()); // Assert assertEquals(ByteString.fromArray(bytes), ByteString.fromArray(bytesold)); Mockito.verify(mockResponse).setHeader(Logo.NAME_HEADER, name); Mockito.verify(mockResponse).setHeader(Logo.IS_DEFAULT_HEADER, Boolean.toString(false)); }
Example #12
Source File: Base64Flows.java From ts-reaktive with MIT License | 5 votes |
@Override protected byte[] transform(ByteString input) { byte[] a = Base64.rfc2045().decode(input.toArray()); if (a == null) { throw new IllegalArgumentException("Base64 input is not a valid multiple of 4-char sequences."); } return a; }
Example #13
Source File: MessagesRoute.java From ditto with Eclipse Public License 2.0 | 5 votes |
private Route handleMessage(final RequestContext ctx, final Source<ByteString, Object> payloadSource, final Function<ByteBuffer, MessageCommand<?, ?>> requestPayloadToCommandFunction) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); payloadSource.fold(ByteString.empty(), ByteString::concat) .map(ByteString::toArray) .map(ByteBuffer::wrap) .map(requestPayloadToCommandFunction) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), AbstractHttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); return completeWithFuture(preprocessResponse(httpResponseFuture)); }
Example #14
Source File: JacksonWriter.java From ts-reaktive with MIT License | 5 votes |
/** * Returns a flow that buffers up to [maximumBatchSize] JSON events and writing them together. * * Buffering is only done if upstream is faster than downstream. If there is demand from downstream, * also slower batches will be written. */ public static Flow<JSONEvent,ByteString,NotUsed> flow(int maximumBatchSize) { return Flow.of(JSONEvent.class) .batch(maximumBatchSize, event -> { List<JSONEvent> l = new ArrayList<>(); l.add(event); return l; }, (list, event) -> { list.add(event); return list; }).via(new JacksonWriter()); }
Example #15
Source File: ByteStringOutputStream.java From ts-reaktive with MIT License | 5 votes |
/** * Returns a ByteString containing the bytes that have been written so far, and then * empties the internal buffer. */ public ByteString getBytesAndReset() { try { delegate.close(); } catch (IOException e) { // can't occur, we're not doing I/O throw new RuntimeException(e); } ByteString result = buffer.result(); buffer = new ByteStringBuilder(); delegate = buffer.asOutputStream(); return result; }
Example #16
Source File: HttpFlow.java From ts-reaktive with MIT License | 5 votes |
private Flow<ByteString, ByteString, CompletionStage<HttpResponse>> createFlow(HttpMethod method, Uri uri, Option<ContentType> contentType, Predicate<HttpResponse> isSuccess, HttpHeader... headers) { Sink<ByteString, Publisher<ByteString>> in = Sink.asPublisher(AsPublisher.WITH_FANOUT); // akka internally recreates this twice, on some errors... Source<ByteString, Subscriber<ByteString>> out = Source.asSubscriber(); return Flow.fromSinkAndSourceMat(in, out, Keep.both()).mapMaterializedValue(pair -> { RequestEntity entity; if (contentType.isDefined()) { Source<ByteString, NotUsed> inReader = Source.fromPublisher(pair.first()); entity = HttpEntities.createChunked(contentType.get(), inReader); } else { entity = HttpEntities.EMPTY; } HttpRequest rq = HttpRequest.create().withMethod(method).withUri(uri).addHeaders(Arrays.asList(headers)).withEntity(entity); return http.singleRequest(rq).thenApply(resp -> { if (isSuccess.test(resp)) { resp.entity().getDataBytes() .runWith(Sink.fromSubscriber(pair.second()), materializer); } else { log.info("Http responded error: {} for request {}", resp, rq); resp.discardEntityBytes(materializer); pair.second().onError(new IllegalStateException("Unsuccessful HTTP response: " + resp + " for " + rq)); } return resp; }).exceptionally(x -> { Throwable cause = (x instanceof CompletionException) ? x.getCause() : x; if (!(cause instanceof IllegalStateException)) { log.info("Could not make http request " + rq, cause); } pair.second().onError(cause); throw (cause instanceof RuntimeException) ? (RuntimeException) x : new RuntimeException(cause); }); }); }
Example #17
Source File: KafkaPublisherActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static String mapExternalMessagePayload(final ExternalMessage externalMessage) { if (externalMessage.isTextMessage()) { return externalMessage.getTextPayload().orElse(""); } else if (externalMessage.isBytesMessage()) { return externalMessage.getBytePayload() .map(ByteString::fromByteBuffer) .map(ByteString::utf8String) .orElse(""); } return ""; }
Example #18
Source File: AbstractHttpRequestActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static HttpResponse addEntityAccordingToContentType(final HttpResponse response, final JsonValue entity, final DittoHeaders dittoHeaders) { final ContentType contentType = getContentType(dittoHeaders); final String entityString = CONTENT_TYPE_TEXT.equals(contentType) ? entity.asString() : entity.toString(); return response.withEntity(contentType, ByteString.fromString(entityString)); }
Example #19
Source File: AbstractRoute.java From ditto with Eclipse Public License 2.0 | 5 votes |
protected Route handlePerRequest(final RequestContext ctx, final DittoHeaders dittoHeaders, final Source<ByteString, ?> payloadSource, final Function<String, Command> requestJsonToCommandFunction) { return handlePerRequest(ctx, dittoHeaders, payloadSource, requestJsonToCommandFunction, null); }
Example #20
Source File: AbstractHttpRequestActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static HttpResponse buildResponseWithoutHeadersFromDittoRuntimeException( final DittoRuntimeException exception) { final HttpResponse responseWithoutHeaders = HttpResponse.create().withStatus(exception.getStatusCode().toInt()); if (HttpStatusCode.NOT_MODIFIED.equals(exception.getStatusCode())) { return responseWithoutHeaders; } return responseWithoutHeaders.withEntity(CONTENT_TYPE_JSON, ByteString.fromString(exception.toJsonString())); }
Example #21
Source File: StaxWriter.java From ts-reaktive with MIT License | 5 votes |
/** * Returns a flow that buffers up to [maximumBatchSize] XML events and writing them together. * * Buffering is only done if upstream is faster than downstream. If there is demand from downstream, * also slower batches will be written. */ public static Flow<XMLEvent,ByteString,NotUsed> flow(int maximumBatchSize) { return Flow.of(XMLEvent.class) .batch(maximumBatchSize, event -> { List<XMLEvent> l = new ArrayList<>(); l.add(event); return l; }, (list, event) -> { list.add(event); return list; }).via(new StaxWriter()); }
Example #22
Source File: HttpPublisherActor.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static CompletionStage<String> getResponseBody(final HttpResponse response, final ActorMaterializer materializer) { return response.entity() .toStrict(READ_BODY_TIMEOUT_MS, materializer) .thenApply(HttpEntity.Strict::getData) .thenApply(ByteString::utf8String); }
Example #23
Source File: StatsRoute.java From ditto with Eclipse Public License 2.0 | 5 votes |
private Route handleDevOpsPerRequest(final RequestContext ctx, final Source<ByteString, ?> payloadSource, final Function<String, DevOpsCommand<?>> requestJsonToCommandFunction) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); payloadSource .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(requestJsonToCommandFunction) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), AbstractHttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); return completeWithFuture(httpResponseFuture); }
Example #24
Source File: DittoRuntimeExceptionToHttpResponse.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Override public HttpResponse apply(final DittoRuntimeException exception) { checkNotNull(exception, "exception"); return HttpResponse.create() .withStatus(exception.getStatusCode().toInt()) .withHeaders(getExternalHeadersFor(exception.getDittoHeaders())) .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(exception.toJsonString())); }
Example #25
Source File: AkkaHttpClientService.java From mutual-tls-ssl with Apache License 2.0 | 5 votes |
private String extractBody(HttpResponse httpResponse) { return httpResponse.entity() .getDataBytes() .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .runWith(Sink.head(), actorSystem) .toCompletableFuture() .join(); }
Example #26
Source File: CompressedSubscriptionsTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void exportAllSubscriptions() { final CompressedSubscriptions underTest = getVennDiagram(); final CompressedUpdate update = underTest.export(true); assertThat(update.shouldReplaceAll()).isTrue(); assertThat(update.getDeletes()).isEmpty(); assertThat(update.getInserts()).containsExactlyInAnyOrder( IntStream.rangeClosed(1, 7) .mapToObj(i -> underTest.hashTopic(String.valueOf(i))) .toArray(ByteString[]::new) ); }
Example #27
Source File: CompressedSubscriptions.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Override protected void onRemovedTopic(final TopicData<ByteString> removedTopic) { hashCodeToTopicCount.computeIfPresent(removedTopic.getHashes(), (hashes, count) -> { if (count > 1) { return count - 1; } else { updates.delete(hashes); return null; } }); }
Example #28
Source File: Base64Flows.java From ts-reaktive with MIT License | 5 votes |
/** * Removes whitespace (or any other ignored characters) from an incoming ByteString buffer * and returns the result. */ private static ByteString removeWhitespace(ByteString in) { ByteStringBuilder b = new ByteStringBuilder(); b.sizeHint(in.size()); for (int i = 0; i < in.size(); i++) { byte ch = in.apply(i); if (NOT_WHITESPACE[ch]) { b.putByte(ch); } } return b.result(); }
Example #29
Source File: CompressedSubscriptions.java From ditto with Eclipse Public License 2.0 | 5 votes |
private CompressedSubscriptions( final Collection<Integer> seeds, final Map<ActorRef, Set<String>> subscriberToTopic, final Map<ActorRef, Predicate<Collection<String>>> subscriberToFilter, final Map<String, TopicData<ByteString>> topicToData, final Map<ByteString, Integer> hashCodeToTopicCount, final CompressedUpdate updates) { super(subscriberToTopic, subscriberToFilter, topicToData); this.seeds = seeds; this.hashCodeToTopicCount = hashCodeToTopicCount; this.updates = updates; }
Example #30
Source File: PrometheusReporterRoute.java From ditto with Eclipse Public License 2.0 | 5 votes |
/** * Builds a Route for a HTTP Prometheus endpoint where Prometheus can scrape metrics from. * * @param prometheusReporter the PrometheusReporter to retrieve the {@code scrapeData} from * @return the Prometheus Route */ public static Route buildPrometheusReporterRoute(final PrometheusReporter prometheusReporter) { return get(() -> complete(HttpResponse.create() .withStatus(StatusCodes.OK) .withEntity(CONTENT_TYPE, ByteString.fromString(prometheusReporter.scrapeData())) ) ); }