Search in sources :

Example 1 with AtmosphereInterceptor

use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.

the class AtmosphereHandlerServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<AtmosphereHandler> annotatedClass) {
    try {
        AtmosphereHandlerService a = annotatedClass.getAnnotation(AtmosphereHandlerService.class);
        atmosphereConfig(a.atmosphereConfig(), framework);
        filters(a.broadcastFilters(), framework);
        Class<?>[] interceptors = a.interceptors();
        LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        for (Class i : interceptors) {
            try {
                AtmosphereInterceptor ai = (AtmosphereInterceptor) framework.newClassInstance(AtmosphereHandler.class, i);
                l.add(ai);
            } catch (Throwable e) {
                logger.warn("", e);
            }
        }
        AtmosphereInterceptor aa = listeners(a.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        if (a.path().contains("{")) {
            l.addFirst(framework.newClassInstance(AtmosphereInterceptor.class, AtmosphereHandlerServiceInterceptor.class));
        }
        framework.sessionSupport(a.supportSession());
        AtmosphereHandler handler = framework.newClassInstance(AtmosphereHandler.class, annotatedClass);
        for (String s : a.properties()) {
            String[] nv = s.split("=");
            IntrospectionUtils.setProperty(handler, nv[0], nv[1]);
            IntrospectionUtils.addProperty(handler, nv[0], nv[1]);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(a.interceptors()), l);
        framework.addAtmosphereHandler(a.path(), handler, broadcaster(framework, a.broadcaster(), a.path()), l);
        framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereHandlerService(org.atmosphere.config.service.AtmosphereHandlerService) AtmosphereHandlerServiceInterceptor(org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) LinkedList(java.util.LinkedList)

Example 2 with AtmosphereInterceptor

use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.

the class AtmosphereInterceptorServiceProcessor method handle.

@Override
public void handle(final AtmosphereFramework framework, Class<AtmosphereInterceptor> annotatedClass) {
    try {
        final AtmosphereInterceptor a = (AtmosphereInterceptor) framework.newClassInstance(AtmosphereInterceptor.class, annotatedClass);
        framework.getAtmosphereConfig().startupHook(new AtmosphereConfig.StartupHook() {

            @Override
            public void started(AtmosphereFramework framework) {
                framework.interceptor(a);
            }
        });
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereConfig(org.atmosphere.runtime.AtmosphereConfig) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework)

Example 3 with AtmosphereInterceptor

use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.

the class AtmosphereServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Object> annotatedClass) {
    try {
        Class<?> aClass = annotatedClass;
        AtmosphereService a = aClass.getAnnotation(AtmosphereService.class);
        framework.setBroadcasterCacheClassName(a.broadcasterCache().getName());
        atmosphereConfig(a.atmosphereConfig(), framework);
        framework.setDefaultBroadcasterClassName(a.broadcaster().getName());
        filters(a.broadcastFilters(), framework);
        LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        AtmosphereInterceptor aa = listeners(a.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        if (!a.servlet().isEmpty()) {
            final ReflectorServletProcessor r = framework.newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
            r.setServletClassName(a.servlet());
            String mapping = a.path();
            AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(a.interceptors()), l);
            if (!a.dispatch()) {
                AtmosphereHandler proxy = new AtmosphereServletProcessor() {

                    private String method = "GET";

                    @Override
                    public void onRequest(AtmosphereResource resource) throws IOException {
                        if (!resource.getRequest().getMethod().equalsIgnoreCase(method)) {
                            r.onRequest(resource);
                        }
                    }

                    @Override
                    public void onStateChange(AtmosphereResourceEvent event) throws IOException {
                        r.onStateChange(event);
                    }

                    @Override
                    public void destroy() {
                        r.destroy();
                    }

                    @Override
                    public void init(AtmosphereConfig config) throws ServletException {
                        String s = config.getInitParameter(ATMOSPHERERESOURCE_INTERCEPTOR_METHOD);
                        if (s != null) {
                            method = s;
                        }
                        r.init(config);
                    }
                };
                framework.addAtmosphereHandler(mapping, proxy, l);
            } else {
                framework.addAtmosphereHandler(mapping, r, l);
            }
        } else {
            interceptors(a.interceptors(), framework);
        }
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereService(org.atmosphere.config.service.AtmosphereService) AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereConfig(org.atmosphere.runtime.AtmosphereConfig) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) LinkedList(java.util.LinkedList) AtmosphereHandler(org.atmosphere.runtime.AtmosphereHandler) AtmosphereServletProcessor(org.atmosphere.runtime.AtmosphereServletProcessor) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor)

Example 4 with AtmosphereInterceptor

use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.

the class MeteorServiceProcessor method handle.

@Override
public void handle(AtmosphereFramework framework, Class<Servlet> annotatedClass) {
    try {
        ReflectorServletProcessor r = framework.newClassInstance(ReflectorServletProcessor.class, ReflectorServletProcessor.class);
        r.setServletClassName(annotatedClass.getName());
        LinkedList<AtmosphereInterceptor> l = new LinkedList<AtmosphereInterceptor>();
        MeteorService m = annotatedClass.getAnnotation(MeteorService.class);
        framework.setBroadcasterCacheClassName(m.broadcasterCache().getName());
        String mapping = m.path();
        atmosphereConfig(m.atmosphereConfig(), framework);
        filters(m.broadcastFilters(), framework);
        AtmosphereInterceptor aa = listeners(m.listeners(), framework);
        if (aa != null) {
            l.add(aa);
        }
        AnnotationUtil.interceptorsForHandler(framework, Arrays.asList(m.interceptors()), l);
        if (m.path().contains("{")) {
            l.addFirst(framework.newClassInstance(AtmosphereInterceptor.class, MeteorServiceInterceptor.class));
        }
        framework.addAtmosphereHandler(mapping, r, broadcaster(framework, m.broadcaster(), m.path()), l);
    } catch (Throwable e) {
        logger.warn("", e);
    }
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) MeteorService(org.atmosphere.config.service.MeteorService) ReflectorServletProcessor(org.atmosphere.handler.ReflectorServletProcessor) MeteorServiceInterceptor(org.atmosphere.config.managed.MeteorServiceInterceptor) LinkedList(java.util.LinkedList)

Example 5 with AtmosphereInterceptor

use of org.atmosphere.runtime.AtmosphereInterceptor in project atmosphere by Atmosphere.

the class JavaScriptProtocol method inspect.

@Override
public Action inspect(final AtmosphereResource ar) {
    if (Utils.webSocketMessage(ar))
        return Action.CONTINUE;
    final AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(ar);
    final AtmosphereRequest request = r.getRequest(false);
    final AtmosphereResponse response = r.getResponse(false);
    String uuid = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID);
    String handshakeUUID = request.getHeader(HeaderConfig.X_ATMO_PROTOCOL);
    if (uuid != null && uuid.equals("0") && handshakeUUID != null) {
        if (enforceAtmosphereVersion) {
            String javascriptVersion = request.getHeader(HeaderConfig.X_ATMOSPHERE_FRAMEWORK);
            int version = 0;
            if (javascriptVersion != null) {
                version = parseVersion(javascriptVersion.split("-")[0]);
            }
            if (version < 221) {
                logger.error("Invalid Atmosphere Version {}", javascriptVersion);
                response.setStatus(501);
                response.addHeader(X_ATMOSPHERE_ERROR, "Atmosphere Protocol version not supported.");
                try {
                    response.flushBuffer();
                } catch (IOException e) {
                }
                return Action.CANCELLED;
            }
        }
        request.header(HeaderConfig.X_ATMO_PROTOCOL, null);
        // Extract heartbeat data
        int heartbeatInterval = 0;
        String heartbeatData = "";
        for (final AtmosphereInterceptor interceptor : framework.interceptors()) {
            if (HeartbeatInterceptor.class.isAssignableFrom(interceptor.getClass())) {
                final HeartbeatInterceptor heartbeatInterceptor = HeartbeatInterceptor.class.cast(interceptor);
                heartbeatInterval = heartbeatInterceptor.clientHeartbeatFrequencyInSeconds() * 1000;
                heartbeatData = new String(heartbeatInterceptor.getPaddingBytes());
                break;
            }
        }
        String message;
        if (enforceAtmosphereVersion) {
            // UUID since 1.0.10
            message = new StringBuilder(r.uuid()).append(wsDelimiter).append(heartbeatInterval).append(wsDelimiter).append(heartbeatData).append(wsDelimiter).toString();
        } else {
            // UUID since 1.0.10
            message = r.uuid();
        }
        // https://github.com/Atmosphere/atmosphere/issues/993
        final AtomicReference<String> protocolMessage = new AtomicReference<String>(message);
        if (r.getBroadcaster().getBroadcasterConfig().hasFilters()) {
            for (BroadcastFilter bf : r.getBroadcaster().getBroadcasterConfig().filters()) {
                if (TrackMessageSizeFilter.class.isAssignableFrom(bf.getClass())) {
                    protocolMessage.set((String) f.filter(r.getBroadcaster().getID(), r, protocolMessage.get(), protocolMessage.get()).message());
                    break;
                }
            }
        }
        if (!Utils.resumableTransport(r.transport())) {
            OnSuspend a = new OnSuspend() {

                @Override
                public void onSuspend(AtmosphereResourceEvent event) {
                    response.write(protocolMessage.get());
                    try {
                        response.flushBuffer();
                    } catch (IOException e) {
                        logger.trace("", e);
                    }
                    r.removeEventListener(this);
                }
            };
            // Pass the information to Servlet Based Framework
            request.setAttribute(CALLBACK_JAVASCRIPT_PROTOCOL, a);
            r.addEventListener(a);
        } else {
            response.write(protocolMessage.get());
        }
        // We don't need to reconnect here
        if (r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET || r.transport() == AtmosphereResource.TRANSPORT.STREAMING || r.transport() == AtmosphereResource.TRANSPORT.SSE) {
            return Action.CONTINUE;
        } else {
            return Action.CANCELLED;
        }
    }
    return Action.CONTINUE;
}
Also used : AtmosphereInterceptor(org.atmosphere.runtime.AtmosphereInterceptor) AtmosphereResponse(org.atmosphere.runtime.AtmosphereResponse) OnSuspend(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnSuspend) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) BroadcastFilter(org.atmosphere.runtime.BroadcastFilter)

Aggregations

AtmosphereInterceptor (org.atmosphere.runtime.AtmosphereInterceptor)8 LinkedList (java.util.LinkedList)5 AtmosphereHandler (org.atmosphere.runtime.AtmosphereHandler)3 IOException (java.io.IOException)2 WebSocketHandlerService (org.atmosphere.config.service.WebSocketHandlerService)2 ReflectorServletProcessor (org.atmosphere.handler.ReflectorServletProcessor)2 AtmosphereConfig (org.atmosphere.runtime.AtmosphereConfig)2 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)2 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)2 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)2 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)2 BroadcastFilter (org.atmosphere.runtime.BroadcastFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AtmosphereHandlerServiceInterceptor (org.atmosphere.config.managed.AtmosphereHandlerServiceInterceptor)1 ManagedAtmosphereHandler (org.atmosphere.config.managed.ManagedAtmosphereHandler)1 MeteorServiceInterceptor (org.atmosphere.config.managed.MeteorServiceInterceptor)1