Search in sources :

Example 36 with Session

use of org.eclipse.jetty.websocket.api.Session in project chassis by Kixeye.

the class WebSocketTransportTest method testWebSocketServiceWithJsonWithWss.

@Test
public void testWebSocketServiceWithJsonWithWss() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("secureWebsocket.enabled", "true");
    properties.put("secureWebsocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("secureWebsocket.hostname", "localhost");
    properties.put("secureWebsocket.selfSigned", "true");
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(TestWebSocketService.class);
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setTrustAll(true);
    WebSocketClient wsClient = new WebSocketClient(sslContextFactory);
    try {
        context.refresh();
        final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);
        final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
        messageRegistry.registerType("stuff", TestObject.class);
        wsClient.start();
        QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);
        Session session = wsClient.connect(webSocket, new URI("wss://localhost:" + properties.get("secureWebsocket.port") + "/" + serDe.getMessageFormatName())).get(5000, TimeUnit.MILLISECONDS);
        Envelope envelope = new Envelope("getStuff", null, null, Lists.newArrayList(new Header("testheadername", Lists.newArrayList("testheaderval"))), null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);
        byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
        envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("stuff", response.value);
        envelope = new Envelope("getStuff", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        response = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals("more stuff", response.value);
        rawStuff = serDe.serialize(new TestObject(RandomStringUtils.randomAlphanumeric(100)));
        envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        ServiceError error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.code);
        envelope = new Envelope("expectedError", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.code, error.code);
        Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.description, error.description);
        envelope = new Envelope("unexpectedError", null, null, null);
        session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
        error = webSocket.getResponse(5, TimeUnit.SECONDS);
        Assert.assertNotNull(error);
        Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.code);
    } finally {
        try {
            wsClient.stop();
        } finally {
            context.close();
        }
    }
}
Also used : ServiceError(com.kixeye.chassis.transport.dto.ServiceError) HashMap(java.util.HashMap) WebSocketMessageRegistry(com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry) JsonJacksonMessageSerDe(com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe) ProtobufMessageSerDe(com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe) YamlJacksonMessageSerDe(com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe) XmlMessageSerDe(com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe) MessageSerDe(com.kixeye.chassis.transport.serde.MessageSerDe) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Envelope(com.kixeye.chassis.transport.dto.Envelope) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) URI(java.net.URI) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Header(com.kixeye.chassis.transport.dto.Header) MapPropertySource(org.springframework.core.env.MapPropertySource) QueuingWebSocketListener(com.kixeye.chassis.transport.websocket.QueuingWebSocketListener) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 37 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinClient method removeNoteConnection.

public void removeNoteConnection(String noteId) {
    if (StringUtils.isBlank(noteId)) {
        LOG.error("Cannot remove session for empty noteId");
        return;
    }
    if (notesConnection.containsKey(noteId)) {
        Session connection = notesConnection.get(noteId);
        if (connection.isOpen()) {
            connection.close();
        }
        notesConnection.remove(noteId);
    }
    LOG.info("Removed note websocket connection for note {}", noteId);
}
Also used : Session(org.eclipse.jetty.websocket.api.Session)

Example 38 with Session

use of org.eclipse.jetty.websocket.api.Session 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 39 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinClient method getNoteSession.

/*
  private Message zeppelinGetNoteMsg(String noteId) {
    Message getNoteMsg = new Message(Message.OP.GET_NOTE);
    HashMap<String, Object> data = new HashMap<>();
    data.put("id", noteId);
    getNoteMsg.data = data;
    return getNoteMsg;
  }
  */
private Session getNoteSession(String noteId) {
    LOG.info("Getting Note websocket connection for note {}", noteId);
    Session session = notesConnection.get(noteId);
    if (!isSessionOpen(session)) {
        LOG.info("No open connection for note {}, opening one", noteId);
        notesConnection.remove(noteId);
        session = openNoteSession(noteId);
    }
    return session;
}
Also used : Session(org.eclipse.jetty.websocket.api.Session)

Example 40 with Session

use of org.eclipse.jetty.websocket.api.Session 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)

Aggregations

Session (org.eclipse.jetty.websocket.api.Session)74 Test (org.junit.Test)57 URI (java.net.URI)46 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)32 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)21 ExecutionException (java.util.concurrent.ExecutionException)12 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)9 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7