Search in sources :

Example 1 with Action

use of org.atmosphere.cpr.Action in project atmosphere-play by Atmosphere.

the class AtmosphereCoordinator method route.

public boolean route(AtmosphereRequest request, AtmosphereResponse response) throws IOException {
    boolean resumeOnBroadcast = false;
    boolean keptOpen = true;
    boolean skipClose = false;
    final PlayAsyncIOWriter w = PlayAsyncIOWriter.class.cast(response.getAsyncIOWriter());
    try {
        Action a = framework.doCometSupport(request, response);
        final AtmosphereResourceImpl impl = (AtmosphereResourceImpl) request.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE);
        String transport = (String) request.getAttribute(FrameworkConfig.TRANSPORT_IN_USE);
        if (transport == null) {
            transport = request.getHeader(X_ATMOSPHERE_TRANSPORT);
        }
        if (a.type() == Action.TYPE.SUSPEND) {
            if (transport.equalsIgnoreCase(HeaderConfig.LONG_POLLING_TRANSPORT) || transport.equalsIgnoreCase(HeaderConfig.JSONP_TRANSPORT)) {
                resumeOnBroadcast = true;
            }
        } else {
            keptOpen = false;
        }
        logger.trace("Transport {} resumeOnBroadcast {}", transport, resumeOnBroadcast);
        final Action action = (Action) request.getAttribute(NettyCometSupport.SUSPEND);
        if (action != null && action.type() == Action.TYPE.SUSPEND && action.timeout() != -1) {
            final AtomicReference<Future<?>> f = new AtomicReference<Future<?>>();
            f.set(suspendTimer.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {
                    if (!w.isClosed() && (System.currentTimeMillis() - w.lastTick()) > action.timeout()) {
                        asynchronousProcessor.endRequest(impl, false);
                        f.get().cancel(true);
                    }
                }
            }, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS));
        } else if (action != null && action.type() == Action.TYPE.RESUME) {
            resumeOnBroadcast = false;
        }
        w.resumeOnBroadcast(resumeOnBroadcast);
    } catch (Throwable e) {
        logger.error("Unable to process request", e);
        keptOpen = false;
    } finally {
        if (w != null && !resumeOnBroadcast && !keptOpen) {
            if (!skipClose) {
                w.close((AtmosphereResponse) null);
            }
        }
    }
    return keptOpen;
}
Also used : Action(org.atmosphere.cpr.Action) Future(java.util.concurrent.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 2 with Action

use of org.atmosphere.cpr.Action in project atmosphere by Atmosphere.

the class BlockingIOCometSupport method cancelled.

@Override
public Action cancelled(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
    Action a = super.cancelled(req, res);
    if (req.getAttribute(LATCH) != null) {
        CountDownLatch latch = (CountDownLatch) req.getAttribute(LATCH);
        latch.countDown();
    }
    return a;
}
Also used : Action(org.atmosphere.cpr.Action) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with Action

use of org.atmosphere.cpr.Action in project atmosphere by Atmosphere.

the class NettyCometSupport method service.

@Override
public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
    Action action;
    action = suspended(req, res);
    if (action.type() == Action.TYPE.SUSPEND) {
        req.setAttribute(SUSPEND, action);
    } else if (action.type() == Action.TYPE.RESUME) {
        req.setAttribute(SUSPEND, action);
        // If resume occurs during a suspend operation, stop processing.
        Boolean resumeOnBroadcast = (Boolean) req.getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
        if (resumeOnBroadcast != null && resumeOnBroadcast) {
            return action;
        }
        Action nextAction = resumed(req, res);
        if (nextAction.type() == Action.TYPE.SUSPEND) {
            req.setAttribute(SUSPEND, action);
        }
    }
    return action;
}
Also used : Action(org.atmosphere.cpr.Action)

Example 4 with Action

use of org.atmosphere.cpr.Action in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method open.

@Override
public final void open(final WebSocket webSocket, final AtmosphereRequest request, final AtmosphereResponse response) throws IOException {
    if (framework.isDestroyed())
        return;
    // TODO: Fix this. Instead add an Interceptor.
    if (framework.getAtmosphereConfig().handlers().isEmpty()) {
        synchronized (framework) {
            if (handlers.isEmpty()) {
                logger.warn("No AtmosphereHandler or WebSocketHandler installed. Adding a default one.");
            }
            framework.addAtmosphereHandler(ROOT_MASTER, REFLECTOR_ATMOSPHEREHANDLER);
        }
    }
    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.isEmpty()) {
            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);
        }
        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);
        }
        webSocket.shiftAttributes();
    }
}
Also used : Action(org.atmosphere.cpr.Action) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtmosphereResourceEventImpl(org.atmosphere.cpr.AtmosphereResourceEventImpl) WebSocketEvent(org.atmosphere.websocket.WebSocketEventListener.WebSocketEvent) ServletException(jakarta.servlet.ServletException) AtmosphereMappingException(org.atmosphere.cpr.AtmosphereMappingException) IOException(java.io.IOException) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereMappingException(org.atmosphere.cpr.AtmosphereMappingException) Future(java.util.concurrent.Future) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl)

Example 5 with Action

use of org.atmosphere.cpr.Action in project atmosphere by Atmosphere.

the class AnnotationScanningTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(AnnotationScanningTest.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.suspend();
        }
    });
}
Also used : SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(jakarta.servlet.ServletException) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) IOException(java.io.IOException) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

Action (org.atmosphere.cpr.Action)9 IOException (java.io.IOException)6 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)5 ServletException (jakarta.servlet.ServletException)4 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)4 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)4 AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)3 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)3 Future (java.util.concurrent.Future)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AsyncIOWriter (org.atmosphere.cpr.AsyncIOWriter)2 AtmosphereInterceptorWriter (org.atmosphere.cpr.AtmosphereInterceptorWriter)2 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)2 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)2 AtmosphereResourceEventListenerAdapter (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1