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

The following examples show how to use org.eclipse.jetty.http2.api.Session. 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: SystemInfoServiceClient.java    From java-11-examples with Apache License 2.0 6 votes vote down vote up
@Override
public SystemInfo getSystemInfo() {
    try {
        Session session = createSession();
        HttpFields requestFields = new HttpFields();
        requestFields.put(USER_AGENT, USER_AGENT_VERSION);
        MetaData.Request request = new MetaData.Request("GET", getSystemInfoURI, HttpVersion.HTTP_2, requestFields);
        HeadersFrame headersFrame = new HeadersFrame(request, null, true);
        GetListener getListener = new GetListener();
        session.newStream(headersFrame, new FuturePromise<>(), getListener);
        SystemInfo response = getListener.get(SystemInfo.class);
        session.close(0, null, new Callback() {});
        return response;
    } catch (Exception e) {
        throw new HttpAccessException(e);
    }
}
 
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 Map<Integer, Integer> onPreface(Session session) {
    LOG.info("onPreface");
    Map<Integer, Integer> settings = new HashMap<>();
    settings.put(SettingsFrame.HEADER_TABLE_SIZE, maxDynamicTableSize);
    settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, initialSessionRecvWindow);
    //int maxConcurrentStreams = getMaxConcurrentStreams();
    if (maxConcurrentStreams >= 0) {
        settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, maxConcurrentStreams);
    }
    settings.put(SettingsFrame.MAX_HEADER_LIST_SIZE, requestHeaderSize);
    return settings;
}
 
Example #4
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onIdleTimeout(Session session) {
    LOG.info("onIdleTimeout");
    boolean close = super.onIdleTimeout(session);
    if (!close)
        return false;

    long idleTimeout = getConnection().getEndPoint().getIdleTimeout();
    return getConnection().onSessionTimeout(new TimeoutException("Session idle timeout " + idleTimeout + " ms"));
}
 
Example #5
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void onClose(Session session, GoAwayFrame frame, Callback callback) {
    LOG.info("onClose");
    ErrorCode error = ErrorCode.from(frame.getError());
    if (error == null) {
        error = ErrorCode.STREAM_CLOSED_ERROR;
    }
    String reason = frame.tryConvertPayload();
    if (reason != null && !reason.isEmpty()) {
        reason = " (" + reason + ")";
    }
    getConnection().onSessionFailure(new EofException("HTTP/2 " + error + reason), callback);
}
 
Example #6
Source File: RestClient20.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
public Session createSession() throws InterruptedException, ExecutionException, TimeoutException {
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, address, new ServerSessionListener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(5, TimeUnit.SECONDS);
    return session;
}
 
Example #7
Source File: CustomSessionListener.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
@Override
public void onFailure(Session session, Throwable failure, Callback callback) {
    LOG.info("onFailure");
    getConnection().onSessionFailure(failure, callback);
}
 
Example #8
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();
}