use of java.util.Base64.Encoder in project webpieces by deanhiller.
the class SettingsMarshaller method marshalPayload.
public String marshalPayload(List<Http2Setting> settingsPayload) {
DataWrapper data = marshalOut(settingsPayload);
byte[] byteBuf = data.createByteArray();
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(byteBuf);
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testPromiseStreamError.
@Test
public void testPromiseStreamError() throws Exception {
Context ctx = vertx.getOrCreateContext();
waitFor(3);
Future<Void> when = Future.future();
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/wibble", ar -> {
assertTrue(ar.succeeded());
assertOnIOContext(ctx);
when.complete();
HttpServerResponse resp = ar.result();
resp.exceptionHandler(err -> {
assertSame(ctx, Vertx.currentContext());
complete();
});
resp.closeHandler(v -> {
assertSame(ctx, Vertx.currentContext());
complete();
});
resp.endHandler(v -> {
assertSame(ctx, Vertx.currentContext());
complete();
});
resp.setChunked(true).write("whatever");
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
when.setHandler(ar -> {
Http2ConnectionEncoder encoder = request.encoder;
encoder.frameWriter().writeHeaders(request.context, promisedStreamId, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
}
});
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testStreamWritability.
private void testStreamWritability(Function<HttpServerRequest, WriteStream<Buffer>> streamProvider) throws Exception {
Context ctx = vertx.getOrCreateContext();
String content = TestUtils.randomAlphaString(1024);
StringBuilder expected = new StringBuilder();
Future<Void> whenFull = Future.future();
AtomicBoolean drain = new AtomicBoolean();
server.requestHandler(req -> {
WriteStream<Buffer> stream = streamProvider.apply(req);
vertx.setPeriodic(1, timerID -> {
if (stream.writeQueueFull()) {
stream.drainHandler(v -> {
assertOnIOContext(ctx);
expected.append("last");
stream.end(Buffer.buffer("last"));
});
vertx.cancelTimer(timerID);
drain.set(true);
whenFull.complete();
} else {
expected.append(content);
Buffer buf = Buffer.buffer(content);
stream.write(buf);
}
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
AtomicInteger toAck = new AtomicInteger();
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.decoder.frameListener(new Http2FrameAdapter() {
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.append(data.toString(StandardCharsets.UTF_8));
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(expected.toString(), received.toString());
testComplete();
});
return delta;
} else {
if (drain.get()) {
return delta;
} else {
toAck.getAndAdd(delta);
return 0;
}
}
}
});
whenFull.setHandler(ar -> {
request.context.executor().execute(() -> {
try {
request.decoder.flowController().consumeBytes(request.connection.stream(id), toAck.intValue());
request.context.flush();
} catch (Http2Exception e) {
e.printStackTrace();
fail(e);
}
});
});
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testServerClose.
@Test
public void testServerClose() throws Exception {
waitFor(2);
AtomicInteger status = new AtomicInteger();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
conn.shutdownHandler(v -> {
assertEquals(0, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(1, status.getAndIncrement());
complete();
});
conn.close();
};
server.requestHandler(requestHandler);
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.channel.closeFuture().addListener(v1 -> {
vertx.runOnContext(v2 -> {
complete();
});
});
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(0, errorCode);
});
}
});
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
Aggregations