Examples of AtmosphereResource


Examples of org.atmosphere.cpr.AtmosphereResource

   * @param event
   * @param resourceUuid
   */
  public void post(Object event, String resourceUuid)
  {
    AtmosphereResource resource = AtmosphereResourceFactory.getDefault().find(resourceUuid);
    if (resource != null)
    {
      post(event, resource);
    }
  }
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
        framework.doCometSupport(request, AtmosphereResponse.newInstance());

        // Check suspend
        final AtmosphereResource res = r.get();
        assertNotNull(res);

        // Send heartbeat
        request = new AtmosphereRequest.Builder()
                .pathInfo("/heartbeat")
                .method("GET")
                .body(Heartbeat.paddingData)
                .build();
        request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
        request.setAttribute(HeartbeatInterceptor.INTERCEPTOR_ADDED, "");
        res.initialize(res.getAtmosphereConfig(), res.getBroadcaster(), request, AtmosphereResponse.newInstance(), framework.getAsyncSupport(), res.getAtmosphereHandler());
        request.setAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE, res);
        framework.doCometSupport(request, AtmosphereResponse.newInstance());
        assertNotNull(message.get());
        assertEquals(message.get(), Heartbeat.paddingData);
    }
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        public class BroadcasterStringReader implements StringReader {
            @Override
            public Object fromString(String topic) {
                Broadcaster broadcaster;
                try {
                    AtmosphereResource r =
                            (AtmosphereResource)
                                    req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);
                    BroadcasterFactory bp = r.getAtmosphereConfig().getBroadcasterFactory();

                    Class<? extends Broadcaster> c;
                    try {
                        c = (Class<Broadcaster>) Class.forName((String) req.getAttribute(ApplicationConfig.BROADCASTER_CLASS));
                    } catch (Throwable e) {
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

    HttpServletRequest req;

    protected AtmosphereResource getAtmosphereResource(Class injectType, boolean session) {

        try {
            AtmosphereResource r = (AtmosphereResource)
                    req.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);

            return r;
        } catch (IllegalStateException ex) {
            throw new IllegalStateException("An instance of the class " + injectType.getName() + " could not be injected because there is no HTTP request in scope", ex);
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

            }
        }

        request.headers(configureHeader(request)).setAttribute(WebSocket.WEBSOCKET_SUSPEND, true);

        AtmosphereResource r = framework.atmosphereFactory().create(framework.getAtmosphereConfig(),
                response,
                framework.getAsyncSupport());

        boolean cleanUpAfterDisconnect = false;
        try {
            request.setAttribute(INJECTED_ATMOSPHERE_RESOURCE, r);
            request.setAttribute(SUSPENDED_ATMOSPHERE_RESOURCE_UUID, r.uuid());

            if (Utils.firefoxWebSocketEnabled(request)) {
                request.setAttribute("firefox", "true");
            }

            AtmosphereResourceImpl.class.cast(r).webSocket(webSocket);
            webSocket.resource(r);
            webSocketProtocol.onOpen(webSocket);
            WebSocketHandler proxy = null;
            if (handlers.size() != 0) {
                WebSocketHandlerProxy handler = mapper.map(request, handlers);
                if (handler == null) {
                    logger.debug("No WebSocketHandler maps request for {} with mapping {}", request.getRequestURI(), handlers);
                    throw new AtmosphereMappingException("No AtmosphereHandler maps request for " + request.getRequestURI());
                }
                proxy = postProcessMapping(webSocket, request, handler);
                AtmosphereResourceImpl.class.cast(webSocket.resource()).action().type(asynchronousProcessor.invokeInterceptors(handler.interceptors(), webSocket.resource(), 0).type());
            }

            // We must dispatch to execute AtmosphereInterceptor
            dispatch(webSocket, request, response);

            if (proxy != null) {
                webSocket.webSocketHandler(proxy).resource().suspend(-1);
                proxy.onOpen(webSocket);
            }

            request.removeAttribute(INJECTED_ATMOSPHERE_RESOURCE);

            // Resource can be null if the client disconnect.
            if (webSocket.resource() != null) {
                final Action action = ((AtmosphereResourceImpl) webSocket.resource()).action();
                if (action.timeout() != -1 && !framework.getAsyncSupport().getContainerName().contains("Netty")) {
                    final AtomicReference<Future<?>> f = new AtomicReference();
                    f.set(scheduler.scheduleAtFixedRate(new Runnable() {
                        @Override
                        public void run() {
                            if (WebSocket.class.isAssignableFrom(webSocket.getClass())
                                    && System.currentTimeMillis() - WebSocket.class.cast(webSocket).lastWriteTimeStampInMilliseconds() > action.timeout()) {
                                asynchronousProcessor.endRequest(((AtmosphereResourceImpl) webSocket.resource()), false);
                                f.get().cancel(true);
                            }
                        }
                    }, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS));
                }
            } else {
                logger.warn("AtmosphereResource was null");
                cleanUpAfterDisconnect = true;
            }
            notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent("", CONNECT, webSocket));
        } catch (AtmosphereMappingException ex) {
            cleanUpAfterDisconnect = true;
            throw ex;
        } catch (IOException ex) {
            cleanUpAfterDisconnect = true;
            throw ex;
        } catch (Exception ex) {
            logger.trace("onOpen exception", ex);
            cleanUpAfterDisconnect = true;
        } finally {
            if (cleanUpAfterDisconnect) {
                logger.warn("Problem opening websocket for {}", r.uuid());
                framework.atmosphereFactory().remove(r.uuid());

                AtmosphereResourceEventImpl.class.cast(r.getAtmosphereResourceEvent()).setCancelled(true);
                AsynchronousProcessor.class.cast(framework.getAsyncSupport()).completeLifecycle(r, true);
            }
        }
    }
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent<Reader>(reader, MESSAGE, webSocket));
    }

    private void handleException(Exception ex, WebSocket webSocket, WebSocketHandler webSocketHandler) {
        logger.error("", ex);
        AtmosphereResource r = webSocket.resource();
        if (r != null) {
            webSocketHandler.onError(webSocket, new WebSocketException(ex,
                    new AtmosphereResponse.Builder()
                            .request(r != null ? AtmosphereResourceImpl.class.cast(r).getRequest(false) : null)
                            .status(500)
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        }
    }

    @Override
    public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) {
        AtmosphereResource resource = webSocket.resource();
        if (resource == null) return;

        AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(resource);
        for (AtmosphereResourceEventListener l : r.atmosphereResourceEventListener()) {
            if (WebSocketEventListener.class.isAssignableFrom(l.getClass())) {
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

            logger.trace("Event Type {} for {}", event.getType(), req.getRequestURL().toString());
        }

        Action action = null;
        // For now, we are just interested in HttpEvent.REA
        AtmosphereResource r = req.resource();
        if (event.getType() == HttpEvent.EventType.BEGIN) {
            action = suspended(req, res);
            if (action.type() == Action.TYPE.SUSPEND) {
                // Do nothing except setting the times out
                try {
                    if (action.timeout() != -1) {
                        event.setTimeout((int) action.timeout());
                    } else {
                        event.setTimeout(Integer.MAX_VALUE);
                    }
                    req.setAttribute(SUSPENDED, true);
                } catch (UnsupportedOperationException ex) {
                    // Swallow s Tomcat APR isn't supporting time out
                    // TODO: Must implement the same functionality using a Scheduler
                }
            } else if (action.type() == Action.TYPE.RESUME) {
                event.close();
            } else {
                event.close();
            }
        } else if (event.getType() == HttpEvent.EventType.READ) {
            // Not implemented
            logger.debug("Receiving bytes, unable to process them.");
        } else if (event.getType() == HttpEvent.EventType.EOF
                || event.getType() == HttpEvent.EventType.ERROR
                || event.getType() == HttpEvent.EventType.END) {

            if (r != null && r.isResumed()) {
                AtmosphereResourceImpl.class.cast(req.resource()).cancel();
            } else if (req.getAttribute(SUSPENDED) != null && closeConnectionOnInputStream) {
                req.setAttribute(SUSPENDED, null);
                action = cancelled(req, res);
            } else {
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        final CountDownLatch latch = new CountDownLatch(1);
        req.setAttribute(LATCH, latch);

        boolean ok = true;
        AtmosphereResource resource = req.resource();
        if (resource != null) {
            try {
                resource.addEventListener(new OnResume() {
                    @Override
                    public void onResume(AtmosphereResourceEvent event) {
                        latch.countDown();
                    }
                });
View Full Code Here

Examples of org.atmosphere.cpr.AtmosphereResource

        if (Utils.webSocketMessage(r)) return Action.CONTINUE;

        AtmosphereRequest request = AtmosphereResourceImpl.class.cast(r).getRequest(false);
        String uuid = r.uuid();
        if (closeMessage(request)) {
            AtmosphereResource ss = config.resourcesFactory().find(uuid);

            if (ss == null) {
                logger.debug("No Suspended Connection found for {}. Using the AtmosphereResource associated with the close message", uuid);
                ss = r;
            }

            if (ss == null) {
                logger.debug("Was unable to execute onDisconnect on {}", r.uuid());
                return Action.CONTINUE;
            }
            logger.debug("AtmosphereResource {} disconnected", uuid);

            // Block websocket closing detection
            AtmosphereResourceEventImpl.class.cast(ss.getAtmosphereResourceEvent()).isClosedByClient(true);

            p.completeLifecycle(ss, false);
            return Action.CANCELLED;
        }
        return Action.CONTINUE;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.