io.vertx.core.buffer.Buffer Java Examples
The following examples show how to use
io.vertx.core.buffer.Buffer.
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: FileBasedTenantServiceTest.java From hono with Eclipse Public License 2.0 | 6 votes |
/** * Verifies that the tenant service successfully starts up even if * the file to read tenants from contains malformed JSON. * * @param ctx The vert.x context. */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testDoStartIgnoresMalformedJson(final VertxTestContext ctx) { // GIVEN a tenant service configured to read data from a file // that contains malformed JSON props.setFilename(FILE_NAME); when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE); doAnswer(invocation -> { final Buffer data = mock(Buffer.class); when(data.getBytes()).thenReturn("NO JSON".getBytes(StandardCharsets.UTF_8)); final Handler handler = invocation.getArgument(1); handler.handle(Future.succeededFuture(data)); return null; }).when(fileSystem).readFile(eq(props.getFilename()), any(Handler.class)); // WHEN starting the service final Promise<Void> startupTracker = Promise.promise(); startupTracker.future().onComplete(ctx.completing( // THEN startup succeeds )); svc.start().onComplete(startupTracker); }
Example #2
Source File: KeepAliveHttpServerResponse.java From sfs with Apache License 2.0 | 6 votes |
@Override public HttpServerResponse write(Buffer chunk) { ObservableFuture<Void> handler = RxHelper.observableFuture(); stopKeepAlive(handler); handler.subscribe(new Subscriber<Void>() { @Override public void onCompleted() { delegate.write(chunk); } @Override public void onError(Throwable e) { handleThrowable(e); } @Override public void onNext(Void aVoid) { } }); return this; }
Example #3
Source File: JournalFile.java From sfs with Apache License 2.0 | 6 votes |
private Observable<Void> setSuperBlock(SfsVertx vertx, BlobFile internalBlobFile, Super superBlock) { Buffer buffer = buffer(superBlock.toByteArray()); Block.Frame<Buffer> frame = encodeFrame(buffer); Buffer frameBuffer = frame.getData(); int frameSize = frameBuffer.length(); checkState(frameSize <= SUPER_BLOCK_SIZE, "Super block frame size was %s, which is greater block size of %s", frameSize, SUPER_BLOCK_SIZE); // write the super block twice so that we can recover from a failed // write return aVoid() .flatMap(aVoid -> internalBlobFile.consume(vertx, SUPER_BLOCK_POSITION_0, frameBuffer)) .flatMap(aVoid -> internalBlobFile.force(vertx, true)) .flatMap(aVoid -> internalBlobFile.consume(vertx, SUPER_BLOCK_POSITION_1, frameBuffer)) .flatMap(aVoid -> internalBlobFile.force(vertx, true)); }
Example #4
Source File: WebSocketServiceTest.java From besu with Apache License 2.0 | 6 votes |
@Test public void websocketServiceCloseConnectionOnUnrecoverableError(final TestContext context) { final Async async = context.async(); final byte[] bigMessage = new byte[HttpServerOptions.DEFAULT_MAX_WEBSOCKET_MESSAGE_SIZE + 1]; Arrays.fill(bigMessage, (byte) 1); httpClient.websocket( "/", webSocket -> { webSocket.write(Buffer.buffer(bigMessage)); webSocket.closeHandler(v -> async.complete()); }); async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS); }
Example #5
Source File: Utils.java From vertx-web with Apache License 2.0 | 6 votes |
public static Buffer readResourceToBuffer(String resource) { ClassLoader cl = getClassLoader(); try { Buffer buffer = Buffer.buffer(); try (InputStream in = cl.getResourceAsStream(resource)) { if (in == null) { return null; } int read; byte[] data = new byte[4096]; while ((read = in.read(data, 0, data.length)) != -1) { if (read == data.length) { buffer.appendBytes(data); } else { byte[] slice = new byte[read]; System.arraycopy(data, 0, slice, 0, slice.length); buffer.appendBytes(slice); } } } return buffer; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
Example #6
Source File: VertxWebSocketSessionTest.java From vertx-spring-boot with Apache License 2.0 | 6 votes |
@Test public void shouldSendBinaryMessage() { TestPublisher<WebSocketMessage> source = TestPublisher.create(); Mono<Void> result = session.send(source); StepVerifier.create(result) .expectSubscription() .then(() -> source.assertMinRequested(1)) .then(() -> source.next(getWebSocketMessage(WebSocketMessage.Type.BINARY, "test1"))) .then(() -> source.assertMinRequested(1)) .then(() -> source.next(getWebSocketMessage(WebSocketMessage.Type.BINARY, "test2"))) .then(() -> source.assertMinRequested(1)) .then(source::complete) .verifyComplete(); verify(mockServerWebSocket).writeBinaryMessage(Buffer.buffer("test1")); verify(mockServerWebSocket).writeBinaryMessage(Buffer.buffer("test2")); }
Example #7
Source File: MockUtil.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
public void mockHighwayCodec() { new MockUp<HighwayCodec>() { @Mock RequestHeader readRequestHeader(Buffer headerBuffer) throws Exception { return requestHeader; } @Mock public Invocation decodeRequest(RequestHeader header, OperationProtobuf operationProtobuf, Buffer bodyBuffer) throws Exception { if (decodeRequestSucc) { return Mockito.mock(Invocation.class); } throw new Error("decode failed"); } }; }
Example #8
Source File: HttpFileTransferIT.java From vertx-spring-boot with Apache License 2.0 | 6 votes |
private static Flux<DataBuffer> readFile(Vertx vertx, Path path) { AsyncFile file = vertx.fileSystem().openBlocking(path.toString(), new OpenOptions()); Flux<Buffer> buffers = Flux.create(sink -> { file.pause(); file.endHandler(v -> sink.complete()); file.exceptionHandler(sink::error); file.handler(sink::next); sink.onRequest(file::fetch); }); DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); return Flux.from(buffers) .map(Buffer::getBytes) .map(dataBufferFactory::wrap); }
Example #9
Source File: RedirectAuthHandlerTest.java From vertx-web with Apache License 2.0 | 6 votes |
private Consumer<HttpClientRequest> sendLoginRequestConsumer() { return req -> { String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO"; Buffer buffer = Buffer.buffer(); String str = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + usernameParam + "\"\r\n\r\ntim\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + passwordParam + "\"\r\n\r\ndelicious:sausages\r\n" + "--" + boundary + "--\r\n"; buffer.appendString(str); req.putHeader("content-length", String.valueOf(buffer.length())); req.putHeader("content-type", "multipart/form-data; boundary=" + boundary); if (sessionCookie.get() != null) { req.putHeader("cookie", sessionCookie.get()); } req.write(buffer); }; }
Example #10
Source File: VendorListService.java From prebid-server-java with Apache License 2.0 | 6 votes |
/** * Saves given vendor list on file system. */ private Future<VendorListResult<T>> saveToFile(VendorListResult<T> vendorListResult) { final Promise<VendorListResult<T>> promise = Promise.promise(); final int version = vendorListResult.getVersion(); final String filepath = new File(cacheDir, version + JSON_SUFFIX).getPath(); fileSystem.writeFile(filepath, Buffer.buffer(vendorListResult.getVendorListAsString()), result -> { if (result.succeeded()) { promise.complete(vendorListResult); } else { logger.error("Could not create new vendor list for version {0}, file: {1}", result.cause(), version, filepath); promise.fail(result.cause()); } }); return promise.future(); }
Example #11
Source File: SettingsCacheNotificationHandler.java From prebid-server-java with Apache License 2.0 | 6 votes |
/** * Propagates invalidating settings cache */ private void doInvalidate(RoutingContext context) { final Buffer body = context.getBody(); if (body == null) { HttpUtil.respondWith(context, HttpResponseStatus.BAD_REQUEST, "Missing invalidation data."); return; } final InvalidateSettingsCacheRequest request; try { request = mapper.decodeValue(body, InvalidateSettingsCacheRequest.class); } catch (DecodeException e) { HttpUtil.respondWith(context, HttpResponseStatus.BAD_REQUEST, "Invalid invalidation."); return; } cacheNotificationListener.invalidate(request.getRequests(), request.getImps()); HttpUtil.respondWith(context, HttpResponseStatus.OK, null); }
Example #12
Source File: VaultConfigStore.java From vertx-config with Apache License 2.0 | 6 votes |
private Future<Buffer> extract(JsonObject json) { Promise<Buffer> promise = Promise.promise(); if (json == null) { promise.complete(new JsonObject().toBuffer()); } else if (config.getString("key") != null) { Object value = json.getValue(config.getString("key")); if (value == null) { promise.complete(new JsonObject().toBuffer()); } else if (value instanceof String) { promise.complete(Buffer.buffer((String) value)); } else if (value instanceof JsonObject) { promise.complete(((JsonObject) value).toBuffer()); } } else { promise.complete(json.toBuffer()); } return promise.future(); }
Example #13
Source File: FaviconHandlerImpl.java From vertx-web with Apache License 2.0 | 5 votes |
private Buffer readFile(FileSystem fs, String path) { if (fs.existsBlocking(path)) { return fs.readFileBlocking(path); } else { throw new RuntimeException(path + " not found!"); } }
Example #14
Source File: SubscriptionsUsingTopicTest.java From vertx-stomp with Apache License 2.0 | 5 votes |
@Test public void testSendingWithoutDestination() { AtomicBoolean failureDetected = new AtomicBoolean(); clients.add(StompClient.create(vertx).connect(ar -> { final StompClientConnection connection = ar.result(); try { connection.send((String) null, Buffer.buffer("hello")); } catch (IllegalArgumentException e) { failureDetected.set(true); } })); Awaitility.await().atMost(10, TimeUnit.SECONDS).until(failureDetected::get); }
Example #15
Source File: RoutingContextImplTest.java From vertx-web with Apache License 2.0 | 5 votes |
@Test public void test_null_literal_object_as_json_yields_empty_json_object() throws Exception { router.route().handler(event -> { assertEquals(null, event.getBodyAsJson()); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("null")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
Example #16
Source File: AsyncFileEndableWriteStream.java From sfs with Apache License 2.0 | 5 votes |
@Override public final void end(Buffer buffer) { checkNotEnded(); ended = true; delegate.write(buffer); endInternal(); }
Example #17
Source File: ReadStreamAdapterBackPressureTest.java From vertx-rx with Apache License 2.0 | 5 votes |
@Test public void testDeliverDuringResume() { TestSubscriber<Buffer> subscriber = new TestSubscriber<Buffer>().prefetch(0); FakeStream<Buffer> stream = new FakeStream<>(); stream.drainHandler(v -> stream.emit(buffer("2"))); O observable = toObservable(stream, 2); subscribe(observable, subscriber); stream.emit(Buffer.buffer("0")); stream.emit(Buffer.buffer("1")); subscriber.request(2); subscriber.assertItems(buffer("0"), buffer("1")).assertEmpty(); }
Example #18
Source File: AbstractVertxBasedHttpProtocolAdapterTest.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Verifies that the adapter does not wait for a telemetry message being settled and accepted * by a downstream peer before responding with a 202 status to the device. */ @SuppressWarnings("unchecked") @Test public void testUploadTelemetryDoesNotWaitForAcceptedOutcome() { // GIVEN an adapter with a downstream telemetry consumer attached final Future<ProtonDelivery> outcome = Future.succeededFuture(mock(ProtonDelivery.class)); givenATelemetrySenderForOutcome(outcome); final HttpServer server = getHttpServer(false); final AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null); // WHEN a device publishes a telemetry message final Buffer payload = Buffer.buffer("some payload"); final HttpServerResponse response = mock(HttpServerResponse.class); final RoutingContext ctx = newRoutingContext(payload, "application/text", mock(HttpServerRequest.class), response); when(ctx.addBodyEndHandler(any(Handler.class))).thenAnswer(invocation -> { final Handler<Void> handler = invocation.getArgument(0); handler.handle(null); return 0; }); adapter.uploadTelemetryMessage(ctx, "tenant", "device"); // THEN the device receives a 202 response immediately verify(response).setStatusCode(202); verify(response).end(); // and the message has been reported as processed verify(metrics).reportTelemetry( eq(MetricsTags.EndpointType.TELEMETRY), eq("tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_MOST_ONCE), eq(payload.length()), eq(MetricsTags.TtdStatus.NONE), any()); }
Example #19
Source File: MultilineParserTest.java From vertx-mail-client with Apache License 2.0 | 5 votes |
/** * Tests multi responses, each response has multiple lines */ @Test public void testMultiResponseMultiLinesMore(TestContext testContext) { Async async = testContext.async(2); final String ehloBanner = "220 hello from smtp server"; final AtomicBoolean init = new AtomicBoolean(false); final String multilinesWithLF = "250-2.1.0 OK1\n250 2.1.0.1 OK1.1\r\n" + "250 2.1.1 OK2\r\n" + "250 2.1.2 OK3"; Handler<Buffer> dataHandler = b -> { if (!init.get()) { testContext.assertEquals(ehloBanner, b.toString()); async.countDown(); } else { String message = b.toString(); testContext.assertTrue(StatusCode.isStatusOk(message)); testContext.assertEquals(multilinesWithLF, message); String[] lines = message.split("\r\n"); testContext.assertEquals(3, lines.length); testContext.assertEquals("250-2.1.0 OK1\n250 2.1.0.1 OK1.1", lines[0]); testContext.assertEquals("250 2.1.1 OK2", lines[1]); testContext.assertEquals("250 2.1.2 OK3", lines[2]); async.countDown(); } }; MultilineParser multilineParser = new MultilineParser(dataHandler); multilineParser.setExpected(1); // simulate the ehlo banner on connection multilineParser.handle(Buffer.buffer(ehloBanner + "\r\n")); init.set(true); multilineParser.setExpected(3); multilineParser.handle(Buffer.buffer(multilinesWithLF + "\r\n")); }
Example #20
Source File: SockJSHandlerTest.java From vertx-web with Apache License 2.0 | 5 votes |
private Buffer combineReplies(List<Buffer> receivedMessages) { Buffer combinedReply = Buffer.buffer(); for (Buffer receivedMessage : receivedMessages) { combinedReply.appendBuffer(receivedMessage); } return combinedReply; }
Example #21
Source File: GossipManager.java From jgossip with Apache License 2.0 | 5 votes |
protected void shutdown() { getSettings().getMsgService().unListen(); doGossipExecutor.shutdown(); try { Thread.sleep(getSettings().getGossipInterval()); } catch (InterruptedException e) { throw new RuntimeException(e); } Buffer buffer = encodeShutdownMessage(); for (int i = 0; i < getLiveMembers().size(); i++) { sendGossip(buffer, getLiveMembers(), i); } isWorking = false; }
Example #22
Source File: ExecuteRequest.java From sfs with Apache License 2.0 | 5 votes |
public Observable<Holder2<HttpClientResponse, Buffer>> executeAndBufferResponse(String url) { return execute(url) .flatMap(httpClientResponse -> just(httpClientResponse) .flatMap(new HttpClientResponseBodyBuffer()) .onErrorResumeNext(throwable -> { httpClientResponse.resume(); return error(throwable); }) .map(buffer -> new Holder2<>(httpClientResponse, buffer))); }
Example #23
Source File: ProtocolGateway.java From hono with Eclipse Public License 2.0 | 5 votes |
private void handleData(final NetSocket socket, final Map<String, Object> dictionary, final Buffer buffer) { final String data = buffer.toString(); LOG.debug("received data from device: [{}]", data); // split up in command token [0] and args [1] final String[] command = data.split(" ", 2); executeCommand(command, socket, dictionary) .onSuccess(c -> { socket.write("OK\n"); }) .onFailure(t -> { LOG.debug("failed to process data provided by device"); socket.write("FAILED: " + t.getMessage() + "\n"); }); }
Example #24
Source File: PipePayloadParser.java From jetlinks-community with Apache License 2.0 | 5 votes |
@Override public synchronized void handle(Buffer buffer) { if (recordParser == null && directMapper == null) { log.error("record parser not init"); return; } if (recordParser != null) { recordParser.handle(buffer); return; } Buffer buf = directMapper.apply(buffer); if (null != buf) { sink.next(buf); } }
Example #25
Source File: WSConsumesTest.java From vert.x-microservice with Apache License 2.0 | 5 votes |
@Test public void testSimpleObjectByBinarySerialisation() throws InterruptedException { final PersonOne message = new PersonOne("Andy","M"); getClient().websocket(8080, "localhost", SERVICE_REST_GET + "/testSimpleObjectByBinarySerialisation", ws -> { long startTime = System.currentTimeMillis(); ws.handler((data) -> { System.out.println("testSimpleObjectBySerialisation :" + new String(data.getBytes())); assertNotNull(data.getString(0, data.length())); assertTrue(new String(data.getBytes()).equals(message.getName())); ws.close(); long endTime = System.currentTimeMillis(); System.out.println("Total execution time testSimpleObjectByBinarySerialisation: " + (endTime - startTime) + "ms"); testComplete(); }); try { byte[] tmp = Serializer.serialize(message); ws.writeBinaryMessage(Buffer.buffer(tmp)); } catch (IOException e) { e.printStackTrace(); } }); await(); }
Example #26
Source File: MessageSendTester.java From enmasse with Apache License 2.0 | 5 votes |
/** * Fill up the message with extra payload to get an exact payload size (in bytes). * <p> * If the payload size if greater than zero, it will always requires {@link #FIXED_JSON_EXTRA_SIZE} * of bytes remaining for an empty object and {@link #FIXED_JSON_EXTRA_SIZE} plus one for an object * which already contains a field. * * @param payload The payload to modify. * @param payloadSize The target payload size. If this is zero or less, this operation is a no-op. * @throws IllegalArgumentException if the target payload size is lower than the already provided * payload. */ static Buffer fillWithPayload(final JsonObject payload, final int payloadSize) { final Buffer buffer = payload.toBuffer(); if (payloadSize <= 0) { return buffer; } // if the object is empty, there will be no extra comma final int extraComma = payload.isEmpty() ? 0 : 1; final int actualSize = buffer.length(); final int fixed = FIXED_JSON_EXTRA_SIZE + extraComma; if (actualSize + fixed > payloadSize) { // we are already "too big" throw new IllegalArgumentException(String.format("Provided payload size already exceeds maximum (actual: %d + %d, target: %d)", actualSize, fixed, payloadSize)); } final int diff = payloadSize - actualSize - fixed; final char[] fill = new char[diff]; Arrays.fill(fill, 'a'); payload.put("extra", String.valueOf(fill)); return payload.toBuffer(); }
Example #27
Source File: RoutingContextImplTest.java From vertx-web with Apache License 2.0 | 5 votes |
@Test public void test_null_literal_array_as_json_array_yields_null_json_array() throws Exception { router.route().handler(event -> { assertEquals(null, event.getBodyAsJsonArray()); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("null")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
Example #28
Source File: PublishEncoder.java From vertx-mqtt-broker with Apache License 2.0 | 5 votes |
@Override protected void encode(PublishMessage message, ByteBuf out) { if (message.getQos() == AbstractMessage.QOSType.RESERVED) { throw new IllegalArgumentException("Found a message with RESERVED Qos"); } if (message.getTopicName() == null || message.getTopicName().isEmpty()) { throw new IllegalArgumentException("Found a message with empty or null topic name"); } ByteBuf variableHeaderBuff = Buffer.buffer(2).getByteBuf(); try { variableHeaderBuff.writeBytes(Utils.encodeString(message.getTopicName())); if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE ) { if (message.getMessageID() == null) { throw new IllegalArgumentException("Found a message with QOS 1 or 2 and not MessageID setted"); } variableHeaderBuff.writeShort(message.getMessageID()); } variableHeaderBuff.writeBytes(message.getPayload().array()); int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); ByteBuf buff = Buffer.buffer(2 + variableHeaderSize).getByteBuf(); buff.writeByte(AbstractMessage.PUBLISH << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { variableHeaderBuff.release(); } }
Example #29
Source File: SockJSHandlerTest.java From vertx-web with Apache License 2.0 | 5 votes |
@Test public void testSplitLargeReplySockJs() throws InterruptedException { String serverPath = "/large-reply-sockjs"; String largeMessage = TestUtils.randomAlphaString(65536 * 2); Buffer largeReplyBuffer = Buffer.buffer(largeMessage); setupSockJsServer(serverPath, (sock, requestBuffer) -> { sock.write(largeReplyBuffer); sock.close(); }); List<Buffer> receivedMessages = new ArrayList<>(); WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages); String messageToSend = "[\"hello\"]"; openedWebSocket.writeFrame(WebSocketFrame.textFrame(messageToSend, true)); await(5, TimeUnit.SECONDS); int receivedReplyCount = receivedMessages.size(); assertTrue("Should have received > 2 reply frame, actually received " + receivedReplyCount, receivedReplyCount > 2); Buffer expectedReplyBuffer = Buffer.buffer("a[\"").appendBuffer(largeReplyBuffer).appendBuffer(Buffer.buffer("\"]")); Buffer clientReplyBuffer = combineReplies(receivedMessages.subList(0, receivedMessages.size() - 1)); assertEquals(String.format("Combined reply on client (length %s) should equal message from server (%s)", clientReplyBuffer.length(), expectedReplyBuffer.length()), expectedReplyBuffer, clientReplyBuffer); Buffer finalMessage = receivedMessages.get(receivedMessages.size() - 1); assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, finalMessage); }
Example #30
Source File: GetNodeStats.java From sfs with Apache License 2.0 | 5 votes |
@Override public void handle(final SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid() .flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(aVoid -> { NodeStats nodeStats = vertxContext.verticle().getNodeStats(); Optional<TransientServiceDef> oNodeStats = nodeStats.getStats(); if (oNodeStats.isPresent()) { return oNodeStats.get().toJsonObject(); } else { return new JsonObject(); } }) .single() .subscribe(new Terminus<JsonObject>(httpServerRequest) { @Override public void onNext(JsonObject jsonObject) { Buffer encoded = buffer(jsonObject.encode().getBytes(UTF_8)); httpServerRequest.response() .setStatusCode(HTTP_OK) .putHeader(CONTENT_LENGTH, valueOf(encoded.length())) .write(encoded); } }); }