Search in sources :

Example 6 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project pulsar by yahoo.

the class ProxyPublishConsumeWithoutZKTest method socketTest.

@Test(timeOut = 30000)
public void socketTest() throws Exception {
    URI consumeUri = URI.create(CONSUME_URI);
    URI produceUri = URI.create(PRODUCE_URI);
    WebSocketClient consumeClient = new WebSocketClient();
    SimpleConsumerSocket consumeSocket = new SimpleConsumerSocket();
    WebSocketClient produceClient = new WebSocketClient();
    SimpleProducerSocket produceSocket = new SimpleProducerSocket();
    try {
        consumeClient.start();
        ClientUpgradeRequest consumeRequest = new ClientUpgradeRequest();
        Future<Session> consumerFuture = consumeClient.connect(consumeSocket, consumeUri, consumeRequest);
        log.info("Connecting to : {}", consumeUri);
        ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
        produceClient.start();
        Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
        // let it connect
        Assert.assertTrue(consumerFuture.get().isOpen());
        Assert.assertTrue(producerFuture.get().isOpen());
        while (consumeSocket.getReceivedMessagesCount() < 10) {
            Thread.sleep(10);
        }
        Assert.assertTrue(produceSocket.getBuffer().size() > 0);
        Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
    } finally {
        ExecutorService executor = newFixedThreadPool(1);
        try {
            executor.submit(() -> {
                try {
                    consumeClient.stop();
                    produceClient.stop();
                    log.info("proxy clients are stopped successfully");
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }).get(2, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("failed to close clients ", e);
        }
        executor.shutdownNow();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Example 7 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project zeppelin by apache.

the class ZeppelinClient method openWatcherSession.

private Session openWatcherSession() {
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setHeader(WatcherSecurityKey.HTTP_HEADER, WatcherSecurityKey.getKey());
    WatcherWebsocket socket = WatcherWebsocket.createInstace();
    Future<Session> future = null;
    Session session = null;
    try {
        future = wsClient.connect(socket, zeppelinWebsocketUrl, request);
        session = future.get();
    } catch (IOException | InterruptedException | ExecutionException e) {
        LOG.error("Couldn't establish websocket connection to Zeppelin ", e);
        return session;
    }
    return session;
}
Also used : WatcherWebsocket(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.listener.WatcherWebsocket) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Session(org.eclipse.jetty.websocket.api.Session)

Example 8 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project zeppelin by apache.

the class ZeppelinClient method openNoteSession.

private Session openNoteSession(String noteId) {
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    ZeppelinWebsocket socket = new ZeppelinWebsocket(noteId);
    Future<Session> future = null;
    Session session = null;
    try {
        future = wsClient.connect(socket, zeppelinWebsocketUrl, request);
        session = future.get();
    } catch (IOException | InterruptedException | ExecutionException e) {
        LOG.error("Couldn't establish websocket connection to Zeppelin ", e);
        return session;
    }
    if (notesConnection.containsKey(noteId)) {
        session.close();
        session = notesConnection.get(noteId);
    } else {
        String getNote = serialize(zeppelinGetNoteMsg(noteId));
        session.getRemote().sendStringByFuture(getNote);
        notesConnection.put(noteId, session);
    }
    return session;
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ZeppelinWebsocket(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.listener.ZeppelinWebsocket) Session(org.eclipse.jetty.websocket.api.Session)

Example 9 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project zeppelin by apache.

the class ZeppelinhubClient method setConnectionrequest.

private ClientUpgradeRequest setConnectionrequest(String token) {
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setCookies(Lists.newArrayList(new HttpCookie(ZeppelinHubRepo.TOKEN_HEADER, token)));
    return request;
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) HttpCookie(java.net.HttpCookie)

Example 10 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest in project jetty.project by eclipse.

the class ClientContainer method connect.

private Session connect(EndpointInstance instance, URI path) throws IOException {
    Objects.requireNonNull(instance, "EndpointInstance cannot be null");
    Objects.requireNonNull(path, "Path cannot be null");
    ClientEndpointConfig config = (ClientEndpointConfig) instance.getConfig();
    ClientUpgradeRequest req = new ClientUpgradeRequest();
    UpgradeListener upgradeListener = null;
    for (Extension ext : config.getExtensions()) {
        req.addExtensions(new JsrExtensionConfig(ext));
    }
    if (config.getPreferredSubprotocols().size() > 0) {
        req.setSubProtocols(config.getPreferredSubprotocols());
    }
    if (config.getConfigurator() != null) {
        upgradeListener = new JsrUpgradeListener(config.getConfigurator());
    }
    Future<org.eclipse.jetty.websocket.api.Session> futSess = client.connect(instance, path, req, upgradeListener);
    try {
        return (JsrSession) futSess.get();
    } catch (InterruptedException e) {
        throw new IOException("Connect failure", e);
    } catch (ExecutionException e) {
        // Unwrap Actual Cause
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            // Just rethrow
            throw (IOException) cause;
        } else {
            throw new IOException("Connect failure", cause);
        }
    }
}
Also used : IOException(java.io.IOException) Extension(javax.websocket.Extension) UpgradeListener(org.eclipse.jetty.websocket.client.io.UpgradeListener) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) ExecutionException(java.util.concurrent.ExecutionException) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(javax.websocket.Session)

Aggregations

ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)17 URI (java.net.URI)10 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)10 Session (org.eclipse.jetty.websocket.api.Session)9 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ParameterException (com.beust.jcommander.ParameterException)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 HttpCookie (java.net.HttpCookie)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1