Search in sources :

Example 66 with Session

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

the class WebSocketScopeSessionTest method testMultiSession_Sequential.

@Test
public void testMultiSession_Sequential() throws Exception {
    ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
    ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
    WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
    wsScope1.create();
    try {
        // Scope 1
        wsScope1.begin();
        BogusSession sess = new BogusSession("1");
        wsScope1.setSession(sess);
        ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
        BogusSocket sock1 = sock1Bean.instance;
        assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
        sock1Bean.destroy();
    } finally {
        wsScope1.end();
    }
    wsScope1.destroy();
    wsScope1Bean.destroy();
    wsScope2.create();
    try {
        // Scope 2
        wsScope2.begin();
        BogusSession sess = new BogusSession("2");
        wsScope2.setSession(sess);
        ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
        BogusSocket sock2 = sock2Bean.instance;
        assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
        sock2Bean.destroy();
    } finally {
        wsScope2.end();
    }
    wsScope2.destroy();
    wsScope2Bean.destroy();
}
Also used : WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 67 with Session

use of org.eclipse.jetty.websocket.api.Session in project spring-framework by spring-projects.

the class JettyWebSocketClient method doHandshakeInternal.

@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    final ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(protocols);
    for (WebSocketExtension e : extensions) {
        request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
    }
    for (String header : headers.keySet()) {
        request.setHeader(header, headers.get(header));
    }
    Principal user = getUser();
    final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
    final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            Future<Session> future = client.connect(listener, uri, request);
            future.get();
            return wsSession;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : WebSocketExtension(org.springframework.web.socket.WebSocketExtension) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) JettyWebSocketHandlerAdapter(org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) WebSocketToJettyExtensionConfigAdapter(org.springframework.web.socket.adapter.jetty.WebSocketToJettyExtensionConfigAdapter) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Principal(java.security.Principal) WebSocketSession(org.springframework.web.socket.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) JettyWebSocketSession(org.springframework.web.socket.adapter.jetty.JettyWebSocketSession)

Example 68 with Session

use of org.eclipse.jetty.websocket.api.Session in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getPrincipalNotAvailable.

@Test
@SuppressWarnings("resource")
public void getPrincipalNotAvailable() {
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(null);
    UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
    given(response.getAcceptedSubProtocol()).willReturn(null);
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUpgradeRequest()).willReturn(request);
    given(nativeSession.getUpgradeResponse()).willReturn(response);
    JettyWebSocketSession session = new JettyWebSocketSession(attributes);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertNull(session.getPrincipal());
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 69 with Session

use of org.eclipse.jetty.websocket.api.Session in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getPrincipalFromNativeSession.

@Test
@SuppressWarnings("resource")
public void getPrincipalFromNativeSession() {
    TestPrincipal user = new TestPrincipal("joe");
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(user);
    UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
    given(response.getAcceptedSubProtocol()).willReturn(null);
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUpgradeRequest()).willReturn(request);
    given(nativeSession.getUpgradeResponse()).willReturn(response);
    JettyWebSocketSession session = new JettyWebSocketSession(attributes);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertSame(user, session.getPrincipal());
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) TestPrincipal(org.springframework.web.socket.handler.TestPrincipal) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 70 with Session

use of org.eclipse.jetty.websocket.api.Session in project pulsar by yahoo.

the class ProxyAuthenticationTest method socketTest.

@Test
public void socketTest() throws InterruptedException {
    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
        Thread.sleep(1000);
        Assert.assertTrue(consumerFuture.get().isOpen());
        Assert.assertTrue(producerFuture.get().isOpen());
        consumeSocket.awaitClose(1, TimeUnit.SECONDS);
        produceSocket.awaitClose(1, TimeUnit.SECONDS);
        Assert.assertTrue(produceSocket.getBuffer().size() > 0);
        Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
    } catch (Throwable t) {
        log.error(t.getMessage());
    } 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)

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