io.vertx.rxjava.core.buffer.Buffer Java Examples

The following examples show how to use io.vertx.rxjava.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: RxifiedExamples.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
public void delayToObservable(HttpServer server) {
  server.requestHandler(request -> {
    if (request.method() == HttpMethod.POST) {

      // Stop receiving buffers
      request.pause();

      checkAuth(res -> {

        // Now we can receive buffers again
        request.resume();

        if (res.succeeded()) {
          Observable<Buffer> observable = request.toObservable();
          observable.subscribe(buff -> {
            // Get buffers
          });
        }
      });
    }
  });
}
 
Example #2
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testWebsocketClientFlatMap() {
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.webSocketStream().handler(ws -> {
    ws.write(Buffer.buffer("some_content"));
    ws.close();
  });
  server.listen(ar -> {
    HttpClient client = vertx.createHttpClient(new HttpClientOptions());
    Buffer content = Buffer.buffer();
    client.
        rxWebSocket(8080, "localhost", "/the_uri").
        flatMapObservable(WebSocket::toObservable).
        forEach(content::appendBuffer, err -> fail(), () -> {
          server.close();
          assertEquals("some_content", content.toString("UTF-8"));
          testComplete();
        });
  });
  await();
}
 
Example #3
Source File: CoreApiTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpClient() {
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> {
    req.response().setChunked(true).end("some_content");
  });
  try {
    server.listen(ar -> {
      HttpClient client = vertx.createHttpClient(new HttpClientOptions());
      client.rxGet(8080, "localhost", "/the_uri").subscribe(resp -> {
        Buffer content = Buffer.buffer();
        Observable<Buffer> observable = resp.toObservable();
        observable.forEach(content::appendBuffer, err -> fail(), () -> {
          assertEquals("some_content", content.toString("UTF-8"));
          testComplete();
        });
      });
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #4
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseMissingBody() throws Exception {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().setStatusCode(403).end());
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<Buffer>> single = client
        .get(8080, "localhost", "/the_uri")
        .rxSend();
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> {
          assertEquals(403, resp.statusCode());
          assertNull(resp.body());
          complete();
        }, this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #5
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testPost() {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.bodyHandler(buff -> {
    assertEquals("onetwothree", buff.toString());
    req.response().end();
  }));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Observable<Buffer> stream = Observable.just(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three"));
      Single<HttpResponse<Buffer>> single = client
        .post(8080, "localhost", "/the_uri")
        .rxSendStream(stream);
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> complete(), this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #6
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().setChunked(true).end("some_content"));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<Buffer>> single = client
        .get(8080, "localhost", "/the_uri")
        .as(BodyCodec.buffer())
        .rxSend();
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> {
          Buffer body = resp.body();
          assertEquals("some_content", body.toString("UTF-8"));
          complete();
        }, this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #7
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
public void writeStreamSubscriberAdapterCallbacks(Observable<Buffer> observable, HttpServerResponse response) {
  response.setChunked(true);

  WriteStreamSubscriber<Buffer> subscriber = response.toSubscriber();

  subscriber.onError(throwable -> {
    if (!response.headWritten() && response.closed()) {
      response.setStatusCode(500).end("oops");
    } else {
      // log error
    }
  });

  subscriber.onWriteStreamError(throwable -> {
    // log error
  });

  subscriber.onWriteStreamEnd(() -> {
    // log end of transaction to audit system...
  });

  observable.subscribe(subscriber);
}
 
Example #8
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void httpClientResponse(HttpClient client) {
  Single<HttpClientResponse> request = client.rxGet(8080, "localhost", "/the_uri");
  request.toObservable().
      subscribe(
          response -> {
            Observable<Buffer> observable = response.toObservable();
            observable.forEach(
                buffer -> {
                  // Process buffer
                }
            );
          }
      );
}
 
Example #9
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void websocketClientBuffer(Observable<WebSocket> socketObservable) {
  socketObservable.subscribe(
      socket -> {
        Observable<Buffer> dataObs = socket.toObservable();
        dataObs.subscribe(buffer -> {
          System.out.println("Got message " + buffer.toString("UTF-8"));
        });
      }
  );
}
 
Example #10
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void websocketServerBuffer(Observable<ServerWebSocket> socketObservable) {
  socketObservable.subscribe(
      socket -> {
        Observable<Buffer> dataObs = socket.toObservable();
        dataObs.subscribe(buffer -> {
          System.out.println("Got message " + buffer.toString("UTF-8"));
        });
      }
  );
}
 
Example #11
Source File: EqualityTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Test
public void testBufferEquality() {
  Buffer buf1 = Buffer.buffer("The quick brown fox jumps over the lazy dog");
  Buffer buf2 = buf1.copy();
  assertNotSame(buf1, buf2);
  assertEquals(buf1, buf2);
}
 
Example #12
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void unmarshaller(FileSystem fileSystem) {
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = file.toObservable();
    observable.lift(io.vertx.rxjava.core.RxHelper.unmarshaller(MyPojo.class)).subscribe(
        mypojo -> {
          // Process the object
        }
    );
  });
}
 
Example #13
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void toObservable(Vertx vertx) {
  FileSystem fs = vertx.fileSystem();
  fs.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = file.toObservable();
    observable.forEach(data -> System.out.println("Read data: " + data.toString("UTF-8")));
  });
}
 
Example #14
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void writeStreamSubscriberAdapter(Observable<io.vertx.core.buffer.Buffer> observable, io.vertx.core.http.HttpServerResponse response) {
  response.setChunked(true);
  WriteStreamSubscriber<io.vertx.core.buffer.Buffer> subscriber = io.vertx.rx.java.RxHelper.toSubscriber(response);
  observable.subscribe(subscriber);
}
 
Example #15
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void rxWriteStreamSubscriberAdapter(Observable<Buffer> observable, HttpServerResponse response) {
  response.setChunked(true);
  observable.subscribe(response.toSubscriber());
}
 
Example #16
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void httpServerRequestObservable(HttpServer server) {
  Observable<HttpServerRequest> requestObservable = server.requestStream().toObservable();
  requestObservable.subscribe(request -> {
    Observable<Buffer> observable = request.toObservable();
  });
}
 
Example #17
Source File: ReadStreamAdapterConvertingTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Override
protected void subscribe(Observable<Buffer> obs, TestSubscriber<Buffer> sub) {
  TestUtils.subscribe(obs, sub);
}
 
Example #18
Source File: ReadStreamAdapterConvertingTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Override
protected Observable<Buffer> concat(Observable<Buffer> obs1, Observable<Buffer> obs2) {
  return Observable.concat(obs1, obs2);
}
 
Example #19
Source File: ReadStreamAdapterConvertingTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Override
protected Buffer buffer(String s) {
  return Buffer.buffer(s);
}
 
Example #20
Source File: ReadStreamAdapterConvertingTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Override
protected String string(Buffer buffer) {
  return buffer.toString("UTF-8");
}
 
Example #21
Source File: ToStringTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Test
public void testBufferToString() {
  String string = "The quick brown fox jumps over the lazy dog";
  assertEquals(string, Buffer.buffer(string).toString());
}
 
Example #22
Source File: EqualityTest.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
@Test
public void testBufferSet() {
  Buffer buf1 = Buffer.buffer("The quick brown fox jumps over the lazy dog");
  Buffer buf2 = buf1.copy();
  assertEquals(1, Stream.of(buf1, buf2).collect(toSet()).size());
}
 
Example #23
Source File: ResolveSnapshotVersion.java    From vertx-deploy-tools with Apache License 2.0 4 votes vote down vote up
private String retrieveAndParseMetadata(Path fileLocation, ModuleRequest request) {
    Buffer b = rxVertx.fileSystem().readFileBlocking(fileLocation.toString());
    return MetadataXPathUtil.getRealSnapshotVersionFromMetadata(b.toString().getBytes(), request);
}