Search in sources :

Example 1 with JsrEvents

use of org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents 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 2 with JsrEvents

use of org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents in project jetty.project by eclipse.

the class JsrServerEndpointImpl method create.

@Override
public EventDriver create(Object websocket, WebSocketPolicy policy) throws Throwable {
    if (!(websocket instanceof EndpointInstance)) {
        throw new IllegalStateException(String.format("Websocket %s must be an %s", websocket.getClass().getName(), EndpointInstance.class.getName()));
    }
    EndpointInstance ei = (EndpointInstance) websocket;
    AnnotatedServerEndpointMetadata metadata = (AnnotatedServerEndpointMetadata) ei.getMetadata();
    JsrEvents<ServerEndpoint, ServerEndpointConfig> events = new JsrEvents<>(metadata);
    // Handle @OnMessage maxMessageSizes
    int maxBinaryMessage = getMaxMessageSize(policy.getMaxBinaryMessageSize(), metadata.onBinary, metadata.onBinaryStream);
    int maxTextMessage = getMaxMessageSize(policy.getMaxTextMessageSize(), metadata.onText, metadata.onTextStream);
    policy.setMaxBinaryMessageSize(maxBinaryMessage);
    policy.setMaxTextMessageSize(maxTextMessage);
    JsrAnnotatedEventDriver driver = new JsrAnnotatedEventDriver(policy, ei, events);
    // Handle @PathParam values
    ServerEndpointConfig config = (ServerEndpointConfig) ei.getConfig();
    if (config instanceof PathParamServerEndpointConfig) {
        PathParamServerEndpointConfig ppconfig = (PathParamServerEndpointConfig) config;
        driver.setPathParameters(ppconfig.getPathParamMap());
    }
    return driver;
}
Also used : JsrEvents(org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) ServerEndpoint(javax.websocket.server.ServerEndpoint) JsrAnnotatedEventDriver(org.eclipse.jetty.websocket.jsr356.endpoints.JsrAnnotatedEventDriver) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 3 with JsrEvents

use of org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents in project jetty.project by eclipse.

the class JsrClientEndpointImpl method create.

@Override
public EventDriver create(Object websocket, WebSocketPolicy policy) throws DeploymentException {
    if (!(websocket instanceof EndpointInstance)) {
        throw new IllegalStateException(String.format("Websocket %s must be an %s", websocket.getClass().getName(), EndpointInstance.class.getName()));
    }
    EndpointInstance ei = (EndpointInstance) websocket;
    AnnotatedClientEndpointMetadata metadata = (AnnotatedClientEndpointMetadata) ei.getMetadata();
    JsrEvents<ClientEndpoint, ClientEndpointConfig> events = new JsrEvents<>(metadata);
    // Handle @OnMessage maxMessageSizes
    int maxBinaryMessage = getMaxMessageSize(policy.getMaxBinaryMessageSize(), metadata.maxBinaryMessageSize());
    int maxTextMessage = getMaxMessageSize(policy.getMaxTextMessageSize(), metadata.maxTextMessageSize());
    policy.setMaxBinaryMessageSize(maxBinaryMessage);
    policy.setMaxTextMessageSize(maxTextMessage);
    return new JsrAnnotatedEventDriver(policy, ei, events);
}
Also used : JsrEvents(org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint) JsrAnnotatedEventDriver(org.eclipse.jetty.websocket.jsr356.endpoints.JsrAnnotatedEventDriver) ClientEndpoint(javax.websocket.ClientEndpoint)

Aggregations

JsrEvents (org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents)3 ClientEndpoint (javax.websocket.ClientEndpoint)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)2 JsrAnnotatedEventDriver (org.eclipse.jetty.websocket.jsr356.endpoints.JsrAnnotatedEventDriver)2 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)1 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)1 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)1 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)1 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)1 Test (org.junit.Test)1