Search in sources :

Example 1 with ClientEndpoint

use of javax.websocket.ClientEndpoint 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 ClientEndpoint

use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.

the class ClientAnnotatedEndpointScanner_InvalidSignaturesTest method testScan_InvalidSignature.

@Test
public void testScan_InvalidSignature() throws DeploymentException {
    AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container, pojo);
    AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
    try {
        scanner.scan();
        Assert.fail("Expected " + InvalidSignatureException.class + " with message that references " + expectedAnnoClass + " annotation");
    } catch (InvalidSignatureException e) {
        if (LOG.isDebugEnabled())
            LOG.debug("{}:{}", e.getClass(), e.getMessage());
        Assert.assertThat("Message", e.getMessage(), containsString(expectedAnnoClass.getSimpleName()));
    }
}
Also used : InvalidSignatureException(org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException) 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 ClientEndpoint

use of javax.websocket.ClientEndpoint 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)

Example 4 with ClientEndpoint

use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.

the class JsrClientEndpointImpl method supports.

@Override
public boolean supports(Object websocket) {
    if (!(websocket instanceof EndpointInstance)) {
        return false;
    }
    EndpointInstance ei = (EndpointInstance) websocket;
    Object endpoint = ei.getEndpoint();
    ClientEndpoint anno = endpoint.getClass().getAnnotation(ClientEndpoint.class);
    return (anno != null);
}
Also used : EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) ClientEndpoint(javax.websocket.ClientEndpoint)

Example 5 with ClientEndpoint

use of javax.websocket.ClientEndpoint in project tomcat70 by apache.

the class WsWebSocketContainer method connectToServer.

@Override
public Session connectToServer(Object pojo, URI path) throws DeploymentException {
    ClientEndpoint annotation = pojo.getClass().getAnnotation(ClientEndpoint.class);
    if (annotation == null) {
        throw new DeploymentException(sm.getString("wsWebSocketContainer.missingAnnotation", pojo.getClass().getName()));
    }
    Endpoint ep = new PojoEndpointClient(pojo, Arrays.asList(annotation.decoders()));
    Class<? extends ClientEndpointConfig.Configurator> configuratorClazz = annotation.configurator();
    ClientEndpointConfig.Configurator configurator = null;
    if (!ClientEndpointConfig.Configurator.class.equals(configuratorClazz)) {
        try {
            configurator = configuratorClazz.newInstance();
        } catch (InstantiationException e) {
            throw new DeploymentException(sm.getString("wsWebSocketContainer.defaultConfiguratorFail"), e);
        } catch (IllegalAccessException e) {
            throw new DeploymentException(sm.getString("wsWebSocketContainer.defaultConfiguratorFail"), e);
        }
    }
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    // Avoid NPE when using RI API JAR - see BZ 56343
    if (configurator != null) {
        builder.configurator(configurator);
    }
    ClientEndpointConfig config = builder.decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).preferredSubprotocols(Arrays.asList(annotation.subprotocols())).build();
    return connectToServer(ep, config, path);
}
Also used : Endpoint(javax.websocket.Endpoint) ClientEndpoint(javax.websocket.ClientEndpoint) PojoEndpointClient(org.apache.tomcat.websocket.pojo.PojoEndpointClient) DeploymentException(javax.websocket.DeploymentException) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint)

Aggregations

ClientEndpoint (javax.websocket.ClientEndpoint)8 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)7 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)4 Test (org.junit.Test)3 DeploymentException (javax.websocket.DeploymentException)2 Endpoint (javax.websocket.Endpoint)2 JsrEvents (org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents)2 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)2 InstanceFactory (io.undertow.servlet.api.InstanceFactory)1 ConstructorInstanceFactory (io.undertow.servlet.util.ConstructorInstanceFactory)1 PathTemplate (io.undertow.util.PathTemplate)1 AnnotatedEndpointFactory (io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory)1 IOException (java.io.IOException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ServletException (javax.servlet.ServletException)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 PojoEndpointClient (org.apache.tomcat.websocket.pojo.PojoEndpointClient)1