Java Code Examples for org.eclipse.jetty.http2.api.Stream#Listener

The following examples show how to use org.eclipse.jetty.http2.api.Stream#Listener . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: StreamProcessorFactory.java    From java-11-examples with Apache License 2.0 2 votes vote down vote up
/**
 * creates an instance of stream listener
 * @param stream
 *   initial stream
 * @return
 *   new instance of stream listener
 */
Stream.Listener create(Stream stream);