Search in sources :

Example 1 with EventDriver

use of org.eclipse.jetty.websocket.common.events.EventDriver in project jetty.project by eclipse.

the class OnPartialTest method testOnTextPartial.

@Test
public void testOnTextPartial() throws Throwable {
    List<WebSocketFrame> frames = new ArrayList<>();
    frames.add(new TextFrame().setPayload("Saved").setFin(false));
    frames.add(new ContinuationFrame().setPayload(" by ").setFin(false));
    frames.add(new ContinuationFrame().setPayload("zero").setFin(true));
    PartialTrackingSocket socket = new PartialTrackingSocket();
    EventDriver driver = toEventDriver(socket);
    driver.onConnect();
    for (WebSocketFrame frame : frames) {
        driver.incomingFrame(frame);
    }
    Assert.assertThat("Captured Event Queue size", socket.eventQueue.size(), is(3));
    Assert.assertThat("Event[0]", socket.eventQueue.poll(), is("onPartial(\"Saved\",false)"));
    Assert.assertThat("Event[1]", socket.eventQueue.poll(), is("onPartial(\" by \",false)"));
    Assert.assertThat("Event[2]", socket.eventQueue.poll(), is("onPartial(\"zero\",true)"));
}
Also used : EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) PartialTrackingSocket(org.eclipse.jetty.websocket.jsr356.server.samples.partial.PartialTrackingSocket) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) Test(org.junit.Test)

Example 2 with EventDriver

use of org.eclipse.jetty.websocket.common.events.EventDriver in project jetty.project by eclipse.

the class OnCloseTest method testOnCloseCall.

@Test
public void testOnCloseCall() throws Exception {
    // Scan annotations
    AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container, testcase.closeClass);
    AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
    scanner.scan();
    // Build up EventDriver
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    ClientEndpointConfig config = metadata.getConfig();
    TrackingSocket endpoint = (TrackingSocket) testcase.closeClass.newInstance();
    EndpointInstance ei = new EndpointInstance(endpoint, config, metadata);
    JsrEvents<ClientEndpoint, ClientEndpointConfig> jsrevents = new JsrEvents<>(metadata);
    EventDriver driver = new JsrAnnotatedEventDriver(policy, ei, jsrevents);
    // Execute onClose call
    driver.onClose(new CloseInfo(StatusCode.NORMAL, "normal"));
    // Test captured event
    EventQueue<String> events = endpoint.eventQueue;
    Assert.assertThat("Number of Events Captured", events.size(), is(1));
    String closeEvent = events.poll();
    Assert.assertThat("Close Event", closeEvent, is(testcase.expectedCloseEvent));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) JsrEvents(org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) Test(org.junit.Test)

Example 3 with EventDriver

use of org.eclipse.jetty.websocket.common.events.EventDriver in project jetty.project by eclipse.

the class WebSocketServerFactory method acceptWebSocket.

@Override
public boolean acceptWebSocket(WebSocketCreator creator, HttpServletRequest request, HttpServletResponse response) throws IOException {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(contextClassloader);
        // Create Servlet Specific Upgrade Request/Response objects
        ServletUpgradeRequest sockreq = new ServletUpgradeRequest(request);
        ServletUpgradeResponse sockresp = new ServletUpgradeResponse(response);
        Object websocketPojo = creator.createWebSocket(sockreq, sockresp);
        // Handle response forbidden (and similar paths)
        if (sockresp.isCommitted()) {
            return false;
        }
        if (websocketPojo == null) {
            // no creation, sorry
            sockresp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Endpoint Creation Failed");
            return false;
        }
        // Allow Decorators to do their thing
        websocketPojo = getObjectFactory().decorate(websocketPojo);
        // Get the original HTTPConnection
        HttpConnection connection = (HttpConnection) request.getAttribute("org.eclipse.jetty.server.HttpConnection");
        // Send the upgrade
        EventDriver driver = eventDriverFactory.wrap(websocketPojo);
        return upgrade(connection, sockreq, sockresp, driver);
    } catch (URISyntaxException e) {
        throw new IOException("Unable to accept websocket due to mangled URI", e);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) HttpConnection(org.eclipse.jetty.server.HttpConnection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)

Example 4 with EventDriver

use of org.eclipse.jetty.websocket.common.events.EventDriver in project jetty.project by eclipse.

the class MessageWriterTest method setupSession.

@Before
public void setupSession() throws Exception {
    policy = WebSocketPolicy.newServerPolicy();
    policy.setInputBufferSize(1024);
    policy.setMaxTextMessageBufferSize(1024);
    // Container
    WebSocketContainerScope containerScope = new SimpleContainerScope(policy, bufferPool);
    // Event Driver factory
    EventDriverFactory factory = new EventDriverFactory(containerScope);
    // local socket
    EventDriver driver = factory.wrap(new TrackingSocket("local"));
    // remote socket
    socket = new TrackingSocket("remote");
    OutgoingFrames socketPipe = FramePipes.to(factory.wrap(socket));
    session = new LocalWebSocketSession(containerScope, testname, driver);
    session.setPolicy(policy);
    // talk to our remote socket
    session.setOutgoingHandler(socketPipe);
    // start session
    session.start();
    // open connection
    session.open();
}
Also used : EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) LocalWebSocketSession(org.eclipse.jetty.websocket.common.io.LocalWebSocketSession) EventDriverFactory(org.eclipse.jetty.websocket.common.events.EventDriverFactory) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) Before(org.junit.Before)

Example 5 with EventDriver

use of org.eclipse.jetty.websocket.common.events.EventDriver in project jetty.project by eclipse.

the class OnPartialTest method toEventDriver.

public EventDriver toEventDriver(Object websocket) throws Throwable {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    policy.setInputBufferSize(1024);
    policy.setMaxBinaryMessageBufferSize(1024);
    policy.setMaxTextMessageBufferSize(1024);
    // Create EventDriver
    EventDriverImpl driverImpl = new JsrServerEndpointImpl();
    Class<?> endpoint = websocket.getClass();
    ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
    Assert.assertThat("Endpoint: " + endpoint + " should be annotated with @ServerEndpoint", anno, notNullValue());
    WebSocketContainerScope containerScope = new SimpleContainerScope(policy);
    // Event Driver Factory
    EventDriverFactory factory = new EventDriverFactory(containerScope);
    factory.addImplementation(new JsrServerEndpointImpl());
    ServerEndpointConfig config = new BasicServerEndpointConfig(containerScope, endpoint, "/");
    AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(containerScope, endpoint, config);
    AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
    scanner.scan();
    EndpointInstance ei = new EndpointInstance(websocket, config, metadata);
    EventDriver driver = driverImpl.create(ei, policy);
    Assert.assertThat("EventDriver", driver, notNullValue());
    // Create Local JsrSession
    String id = testname.getMethodName();
    URI requestURI = URI.create("ws://localhost/" + id);
    DummyConnection connection = new DummyConnection();
    ClientContainer container = new ClientContainer();
    container.start();
    @SuppressWarnings("resource") JsrSession session = new JsrSession(container, id, requestURI, driver, connection);
    session.setPolicy(policy);
    session.start();
    session.open();
    return driver;
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) EventDriverFactory(org.eclipse.jetty.websocket.common.events.EventDriverFactory) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) DummyConnection(org.eclipse.jetty.websocket.common.test.DummyConnection) ClientContainer(org.eclipse.jetty.websocket.jsr356.ClientContainer) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) ServerEndpoint(javax.websocket.server.ServerEndpoint) URI(java.net.URI) EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) EventDriverImpl(org.eclipse.jetty.websocket.common.events.EventDriverImpl)

Aggregations

EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)7 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)3 EventDriverFactory (org.eclipse.jetty.websocket.common.events.EventDriverFactory)3 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)3 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)3 Before (org.junit.Before)3 URI (java.net.URI)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)2 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)2 DummyConnection (org.eclipse.jetty.websocket.common.test.DummyConnection)2 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)2 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 ClientEndpoint (javax.websocket.ClientEndpoint)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1