Search in sources :

Example 1 with WebSocketHandlerService

use of org.atmosphere.config.service.WebSocketHandlerService in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method postProcessMapping.

protected WebSocketHandler postProcessMapping(WebSocket webSocket, AtmosphereRequest request, WebSocketHandlerProxy w) {
    WebSocketHandlerProxy p = null;
    String path = w.path();
    if (wildcardMapping()) {
        String pathInfo = null;
        try {
            pathInfo = request.getPathInfo();
        } catch (IllegalStateException ex) {
        // http://java.net/jira/browse/GRIZZLY-1301
        }
        if (pathInfo != null) {
            path = request.getServletPath() + pathInfo;
        } else {
            path = request.getServletPath();
        }
        if (path == null || path.isEmpty()) {
            path = "/";
        }
        synchronized (handlers) {
            p = handlers.get(path);
            if (p == null) {
                // AtmosphereHandlerService
                WebSocketHandlerService a = w.proxied.getClass().getAnnotation(WebSocketHandlerService.class);
                if (a != null) {
                    String targetPath = a.path();
                    if (targetPath.indexOf("{") != -1 && targetPath.indexOf("}") != -1) {
                        try {
                            boolean singleton = w.proxied.getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                w = new WebSocketHandlerProxy(a.broadcaster(), framework.newClassInstance(WebSocketHandler.class, w.proxied.getClass()));
                            }
                            registerWebSocketHandler(path, new WebSocketHandlerProxy(a.broadcaster(), w));
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            p = handlers.get(path);
                        } catch (Throwable e) {
                            logger.warn("Unable to create WebSocketHandler", e);
                        }
                    }
                }
            }
        }
    }
    try {
        webSocket.resource().setBroadcaster(AnnotationUtil.broadcaster(framework, p != null ? p.broadcasterClazz : w.broadcasterClazz, path));
    } catch (Exception e) {
        logger.error("", e);
    }
    return p != null ? p : w;
}
Also used : Named(jakarta.inject.Named) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) Singleton(org.atmosphere.config.service.Singleton) ServletException(jakarta.servlet.ServletException) AtmosphereMappingException(org.atmosphere.cpr.AtmosphereMappingException) IOException(java.io.IOException)

Example 2 with WebSocketHandlerService

use of org.atmosphere.config.service.WebSocketHandlerService in project atmosphere by Atmosphere.

the class WebSocketHandlerServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<WebSocketHandler> annotatedClass) {
    try {
        WebSocketHandlerService m = annotatedClass.getAnnotation(WebSocketHandlerService.class);
        atmosphereConfig(m.atmosphereConfig(), framework);
        framework.addAtmosphereHandler(m.path(), AtmosphereFramework.REFLECTOR_ATMOSPHEREHANDLER).initWebSocket();
        framework.setDefaultBroadcasterClassName(m.broadcaster().getName());
        filters(m.broadcastFilters(), framework);
        final LinkedList<AtmosphereInterceptor> l = new LinkedList<>();
        AtmosphereInterceptor aa = listeners(m.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(m.interceptors()), l);
        framework.setBroadcasterCacheClassName(m.broadcasterCache().getName());
        WebSocketProcessor p = WebSocketProcessorFactory.getDefault().getWebSocketProcessor(framework);
        framework.addAtmosphereHandler(m.path(), REFLECTOR_ATMOSPHEREHANDLER, l);
        p.registerWebSocketHandler(m.path(), new WebSocketProcessor.WebSocketHandlerProxy(broadcasterClass(framework, m.broadcaster()), framework.newClassInstance(WebSocketHandler.class, annotatedClass)));
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.cpr.AtmosphereInterceptor) WebSocketHandlerService(org.atmosphere.config.service.WebSocketHandlerService) WebSocketProcessor(org.atmosphere.websocket.WebSocketProcessor) LinkedList(java.util.LinkedList)

Aggregations

WebSocketHandlerService (org.atmosphere.config.service.WebSocketHandlerService)2 Named (jakarta.inject.Named)1 ServletException (jakarta.servlet.ServletException)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Singleton (org.atmosphere.config.service.Singleton)1 AtmosphereInterceptor (org.atmosphere.cpr.AtmosphereInterceptor)1 AtmosphereMappingException (org.atmosphere.cpr.AtmosphereMappingException)1 WebSocketProcessor (org.atmosphere.websocket.WebSocketProcessor)1