org.eclipse.jetty.http2.frames.DataFrame Java Examples

The following examples show how to use org.eclipse.jetty.http2.frames.DataFrame. 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: StreamEchoProcessor.java    From java-11-examples with Apache License 2.0 6 votes vote down vote up
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
    LOG.info("onData {}", stream.getId());
    try {
        boolean endStream = frame.isEndStream();
        byte[] bytes = new byte[frame.getData().remaining()];
        frame.getData().get(bytes);
        EchoMessage echoMessage = objectMapper.readValue(bytes, EchoMessage.class);
        LOG.info("got echo {}", echoMessage.getMessage());
        String response = echoService.ping(echoMessage.getMessage());
        EchoMessage echoResponse = new EchoMessage(response);
        LOG.info("echo response {}", echoResponse.getMessage());
        DataFrame responseFrame = new DataFrame(stream.getId(),
                ByteBuffer.wrap(objectMapper.writeValueAsBytes(echoResponse)), endStream);
        stream.data(responseFrame, new Callback() {});
        callback.succeeded();
        LOG.info("echo done");
    } catch (IOException e) {
        LOG.error("IOException: ", e);
    }
}
 
Example #2
Source File: DicomWebClientJetty.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 5 votes vote down vote up
public DataFrame nextFrame() throws IOException {
  int read = in.read(buffer);
  if (read == -1) {
    endStream = true;
    return new DataFrame(stream.getId(),
        ByteBuffer.wrap(buffer, 0, 0), true);
  } else {
    return new DataFrame(stream.getId(),
        ByteBuffer.wrap(buffer, 0, read), false);
  }
}
 
Example #3
Source File: GetListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
    LOG.info("onData");
    bytes = new byte[frame.getData().remaining()];
    frame.getData().get(bytes);
    countDownLatch.countDown();
    callback.succeeded();
}
 
Example #4
Source File: EchoServiceClient.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
private String sendMessage(GetListener getListener, Stream stream, String message, boolean endStream) throws JsonProcessingException {
    EchoMessage echoMessage = new EchoMessage(message);
    DataFrame dataFrame = new DataFrame(stream.getId(),
            ByteBuffer.wrap(objectMapper.writeValueAsBytes(echoMessage)), endStream);
    stream.data(dataFrame , new Callback() {});
    EchoMessage echoResponse = getListener.get(EchoMessage.class);
    getListener.restart();
    return echoResponse.getMessage();
}
 
Example #5
Source File: JettyClientExample.java    From http2-examples with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {
    long startTime = System.nanoTime();

    // Create and start HTTP2Client.
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();

    // Connect to host.
    String host = "localhost";
    int port = 8443;

    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);

    // Obtain the client Session object.
    Session session = sessionPromise.get(5, TimeUnit.SECONDS);

    // Prepare the HTTP request headers.
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    // Prepare the HTTP request object.
    MetaData.Request request = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    // Create the HTTP/2 HEADERS frame representing the HTTP request.
    HeadersFrame headersFrame = new HeadersFrame(request, null, true);

    // Prepare the listener to receive the HTTP response frames.
    Stream.Listener responseListener = new Stream.Listener.Adapter()
    {
        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback)
        {
            byte[] bytes = new byte[frame.getData().remaining()];
            frame.getData().get(bytes);
            int duration = (int) TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime);
            System.out.println("After " + duration + " seconds: " + new String(bytes));
            callback.succeeded();
        }
    };

    session.newStream(headersFrame, new FuturePromise<>(), responseListener);
    session.newStream(headersFrame, new FuturePromise<>(), responseListener);
    session.newStream(headersFrame, new FuturePromise<>(), responseListener);

    Thread.sleep(TimeUnit.SECONDS.toMillis(20));

    client.stop();
}
 
Example #6
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 2 votes vote down vote up
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {

}