Search in sources :

Example 1 with ServerEndpoint

use of javax.websocket.server.ServerEndpoint in project jetty.project by eclipse.

the class ServerAnnotatedEndpointScanner_InvalidSignaturesTest method testScan_InvalidSignature.

@Test
public void testScan_InvalidSignature() throws DeploymentException {
    WebSocketContainerScope container = new SimpleContainerScope(WebSocketPolicy.newClientPolicy());
    AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(container, pojo, null);
    AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> 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) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) ServerEndpoint(javax.websocket.server.ServerEndpoint) Test(org.junit.Test)

Example 2 with ServerEndpoint

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

use of javax.websocket.server.ServerEndpoint in project tomcat by apache.

the class WsServerContainer method addEndpoint.

/**
     * Provides the equivalent of {@link #addEndpoint(ServerEndpointConfig)}
     * for publishing plain old java objects (POJOs) that have been annotated as
     * WebSocket endpoints.
     *
     * @param pojo   The annotated POJO
     */
@Override
public void addEndpoint(Class<?> pojo) throws DeploymentException {
    ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class);
    if (annotation == null) {
        throw new DeploymentException(sm.getString("serverContainer.missingAnnotation", pojo.getName()));
    }
    String path = annotation.value();
    // Validate encoders
    validateEncoders(annotation.encoders());
    // ServerEndpointConfig
    ServerEndpointConfig sec;
    Class<? extends Configurator> configuratorClazz = annotation.configurator();
    Configurator configurator = null;
    if (!configuratorClazz.equals(Configurator.class)) {
        try {
            configurator = annotation.configurator().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DeploymentException(sm.getString("serverContainer.configuratorFail", annotation.configurator().getName(), pojo.getClass().getName()), e);
        }
    }
    sec = ServerEndpointConfig.Builder.create(pojo, path).decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).subprotocols(Arrays.asList(annotation.subprotocols())).configurator(configurator).build();
    addEndpoint(sec);
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Configurator(javax.websocket.server.ServerEndpointConfig.Configurator) DeploymentException(javax.websocket.DeploymentException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 4 with ServerEndpoint

use of javax.websocket.server.ServerEndpoint in project jetty.project by eclipse.

the class ServerContainer method getServerEndpointMetadata.

public ServerEndpointMetadata getServerEndpointMetadata(final Class<?> endpoint, final ServerEndpointConfig config) throws DeploymentException {
    ServerEndpointMetadata metadata = null;
    ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
    if (anno != null) {
        // Annotated takes precedence here
        AnnotatedServerEndpointMetadata ametadata = new AnnotatedServerEndpointMetadata(this, endpoint, config);
        AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(ametadata);
        metadata = ametadata;
        scanner.scan();
    } else if (Endpoint.class.isAssignableFrom(endpoint)) {
        // extends Endpoint
        @SuppressWarnings("unchecked") Class<? extends Endpoint> eendpoint = (Class<? extends Endpoint>) endpoint;
        metadata = new SimpleServerEndpointMetadata(eendpoint, config);
    } else {
        StringBuilder err = new StringBuilder();
        err.append("Not a recognized websocket [");
        err.append(endpoint.getName());
        err.append("] does not extend @").append(ServerEndpoint.class.getName());
        err.append(" or extend from ").append(Endpoint.class.getName());
        throw new DeploymentException("Unable to identify as valid Endpoint: " + endpoint);
    }
    return metadata;
}
Also used : ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) DeploymentException(javax.websocket.DeploymentException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 5 with ServerEndpoint

use of javax.websocket.server.ServerEndpoint in project jetty.project by eclipse.

the class WebSocketServerContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
    if (!isEnabledViaContext(context, ENABLE_KEY, true)) {
        LOG.info("JSR-356 is disabled by configuration");
        return;
    }
    ContextHandler handler = ContextHandler.getContextHandler(context);
    if (handler == null) {
        throw new ServletException("Not running on Jetty, JSR-356 support unavailable");
    }
    if (!(handler instanceof ServletContextHandler)) {
        throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support unavailable");
    }
    ServletContextHandler jettyContext = (ServletContextHandler) handler;
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(context.getClassLoader());
        // Create the Jetty ServerContainer implementation
        ServerContainer jettyContainer = configureContext(jettyContext);
        // make sure context is cleaned up when the context stops
        context.addListener(new ContextDestroyListener());
        if (c.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No JSR-356 annotations or interfaces discovered");
            }
            return;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found {} classes", c.size());
        }
        // Now process the incoming classes
        Set<Class<? extends Endpoint>> discoveredExtendedEndpoints = new HashSet<>();
        Set<Class<?>> discoveredAnnotatedEndpoints = new HashSet<>();
        Set<Class<? extends ServerApplicationConfig>> serverAppConfigs = new HashSet<>();
        filterClasses(c, discoveredExtendedEndpoints, discoveredAnnotatedEndpoints, serverAppConfigs);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Discovered {} extends Endpoint classes", discoveredExtendedEndpoints.size());
            LOG.debug("Discovered {} @ServerEndpoint classes", discoveredAnnotatedEndpoints.size());
            LOG.debug("Discovered {} ServerApplicationConfig classes", serverAppConfigs.size());
        }
        // Process the server app configs to determine endpoint filtering
        boolean wasFiltered = false;
        Set<ServerEndpointConfig> deployableExtendedEndpointConfigs = new HashSet<>();
        Set<Class<?>> deployableAnnotatedEndpoints = new HashSet<>();
        for (Class<? extends ServerApplicationConfig> clazz : serverAppConfigs) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found ServerApplicationConfig: {}", clazz);
            }
            try {
                ServerApplicationConfig config = clazz.newInstance();
                Set<ServerEndpointConfig> seconfigs = config.getEndpointConfigs(discoveredExtendedEndpoints);
                if (seconfigs != null) {
                    wasFiltered = true;
                    deployableExtendedEndpointConfigs.addAll(seconfigs);
                }
                Set<Class<?>> annotatedClasses = config.getAnnotatedEndpointClasses(discoveredAnnotatedEndpoints);
                if (annotatedClasses != null) {
                    wasFiltered = true;
                    deployableAnnotatedEndpoints.addAll(annotatedClasses);
                }
            } catch (InstantiationException | IllegalAccessException e) {
                throw new ServletException("Unable to instantiate: " + clazz.getName(), e);
            }
        }
        // Default behavior if nothing filtered
        if (!wasFiltered) {
            deployableAnnotatedEndpoints.addAll(discoveredAnnotatedEndpoints);
            // Note: it is impossible to determine path of "extends Endpoint" discovered classes
            deployableExtendedEndpointConfigs = new HashSet<>();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deploying {} ServerEndpointConfig(s)", deployableExtendedEndpointConfigs.size());
        }
        // Deploy what should be deployed.
        for (ServerEndpointConfig config : deployableExtendedEndpointConfigs) {
            try {
                jettyContainer.addEndpoint(config);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deploying {} @ServerEndpoint(s)", deployableAnnotatedEndpoints.size());
        }
        for (Class<?> annotatedClass : deployableAnnotatedEndpoints) {
            try {
                jettyContainer.addEndpoint(annotatedClass);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletException(javax.servlet.ServletException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) DeploymentException(javax.websocket.DeploymentException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServerContainer(org.eclipse.jetty.websocket.jsr356.server.ServerContainer) HashSet(java.util.HashSet)

Aggregations

ServerEndpoint (javax.websocket.server.ServerEndpoint)15 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)11 DeploymentException (javax.websocket.DeploymentException)5 Endpoint (javax.websocket.Endpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 ClientEndpoint (javax.websocket.ClientEndpoint)3 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)3 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)3 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)3 PathTemplate (io.undertow.util.PathTemplate)2 AnnotatedEndpointFactory (io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 ServletException (javax.servlet.ServletException)2 ServerApplicationConfig (javax.websocket.server.ServerApplicationConfig)2 Configurator (javax.websocket.server.ServerEndpointConfig.Configurator)2 GsonBuilder (com.google.gson.GsonBuilder)1 InstanceFactory (io.undertow.servlet.api.InstanceFactory)1 ConstructorInstanceFactory (io.undertow.servlet.util.ConstructorInstanceFactory)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1