org.eclipse.jetty.http2.api.Stream Java Examples

The following examples show how to use org.eclipse.jetty.http2.api.Stream. 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: EchoServiceClient.java    From java-11-examples with Apache License 2.0 6 votes vote down vote up
public List<String> ping(List<String> messages) {
    try {
        List<String> responses = new ArrayList<>();
        GetListener getListener = new GetListener();
        Stream stream = createStream(session, echoServiceURI, getListener);
        for (int i=0; i<messages.size(); i++) {
            boolean endStream = (i == (messages.size() - 1));
            String response = sendMessage(getListener, stream, messages.get(i), endStream);
            responses.add(response);
            getListener.restart();
        }
        return responses;
    } catch (Exception e) {
        throw new HttpAccessException(e);
    }
}
 
Example #2
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 6 votes vote down vote up
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
    LOG.info("onNewStream");
    if (frame.getMetaData().isRequest()) {
        MetaData.Request request = (MetaData.Request)frame.getMetaData();
        String uriPath = request.getURI().getPath();
        LOG.info("isRequest {} {}", request.getMethod(), uriPath);
        StreamProcessorRegistration streamProcessorRegistration = streamProcessors.get(uriPath);
        if (streamProcessorRegistration != null) {
            LOG.info("diverting to stream processor {}", uriPath);
            return streamProcessorRegistration.getFactory().create(stream);
        }
    }
    getConnection().onNewStream(connector, (IStream)stream, frame);
    return this;
}
 
Example #3
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 #4
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 #5
Source File: EchoServiceClient.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public String ping(String message) {
    try {
        GetListener getListener = new GetListener();
        Stream stream = createStream(session, echoServiceURI, getListener);
        return sendMessage(getListener, stream, message, true);
    } catch (Exception e) {
        throw new HttpAccessException(e);
    }
}
 
Example #6
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 #7
Source File: RestClient20.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
public Stream createStream(Session session, HttpURI uri, Stream.Listener listener) throws Exception {
    HttpFields requestFields = new HttpFields();
    requestFields.put(USER_AGENT, USER_AGENT_VERSION);
    MetaData.Request request = new MetaData.Request("GET", uri, HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> streamPromise = new FuturePromise<>();
    session.newStream(headersFrame, streamPromise, listener);
    return streamPromise.get();
}
 
Example #8
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    LOG.info("onHeaders");
    if (frame.isEndStream()) {
        getConnection().onTrailers((IStream) stream, frame);
    } else {
        close(stream, "invalid_trailers");
    }
}
 
Example #9
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    LOG.info("onPush");
    // Servers do not receive pushes.
    close(stream, "push_promise");
    return null;
}
 
Example #10
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void onReset(Stream stream, ResetFrame frame) {
    LOG.info("onReset");
    ErrorCode error = ErrorCode.from(frame.getError());
    if (error == null) {
        error = ErrorCode.CANCEL_STREAM_ERROR;
    }
    getConnection().onStreamFailure((IStream)stream, new EofException("HTTP/2 " + error), Callback.NOOP);
}
 
Example #11
Source File: StreamEchoProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onReset(Stream stream, ResetFrame frame) {
    LOG.info("onReset");
}
 
Example #12
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    return null;
}
 
Example #13
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onIdleTimeout(Stream stream, Throwable x) {
    return false;
}
 
Example #14
Source File: StreamEchoProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    LOG.info("onHeaders");
}
 
Example #15
Source File: StreamEchoProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    LOG.info("onPush");
    return null;
}
 
Example #16
Source File: GetListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
    LOG.info("onHeaders");
}
 
Example #17
Source File: StreamEchoProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onIdleTimeout(Stream stream, Throwable x) {
    LOG.info("onIdleTimeout");
    return false;
}
 
Example #18
Source File: StreamEchoProcessorFactory.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Stream.Listener create(Stream stream) {
    return new StreamEchoProcessor(echoService);
}
 
Example #19
Source File: StreamMessageProcessorFactory.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Stream.Listener create(Stream stream) {
    return new StreamMessageProcessor(stream, messageService);
}
 
Example #20
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
public StreamMessageProcessor(Stream stream, MessageServiceAsync messageService) {
    LOG.info("init ...");
    this.stream = stream;
    this.messageService = messageService;
}
 
Example #21
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
private void close(Stream stream, String reason) {
    LOG.info("close");
    stream.getSession().close(ErrorCode.PROTOCOL_ERROR.code, reason, Callback.NOOP);
}
 
Example #22
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onIdleTimeout(Stream stream, Throwable x) {
    LOG.info("onIdleTimeout");
    return getConnection().onStreamTimeout((IStream)stream, x);
}
 
Example #23
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
    LOG.info("onData");
    getConnection().onData((IStream)stream, frame, callback);
}
 
Example #24
Source File: GetListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onReset(Stream stream, ResetFrame frame) {
    LOG.info("onReset");
}
 
Example #25
Source File: GetListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    LOG.info("onPush");
    return null;
}
 
Example #26
Source File: DicomWebClientJetty.java    From healthcare-dicom-dicomweb-adapter with Apache License 2.0 4 votes vote down vote up
public DataStream(Stream http2stream, InputStream inputStream) {
  this.stream = http2stream;
  this.in = inputStream;
}
 
Example #27
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 #28
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 2 votes vote down vote up
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {

}
 
Example #29
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) {

}
 
Example #30
Source File: StreamMessageProcessor.java    From java-11-examples with Apache License 2.0 2 votes vote down vote up
@Override
public void onReset(Stream stream, ResetFrame frame) {

}