Search in sources :

Example 1 with OnResume

use of org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume in project atmosphere by Atmosphere.

the class BlockingIOCometSupport method suspend.

/**
 * Suspend the connection by blocking the current {@link Thread}
 *
 * @param action The {@link Action}
 * @param req    the {@link AtmosphereRequest}
 * @param res    the {@link AtmosphereResponse}
 */
protected void suspend(Action action, AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
    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();
                }
            });
            if (action.timeout() != -1) {
                ok = latch.await(action.timeout(), TimeUnit.MILLISECONDS);
            } else {
                latch.await();
            }
        } catch (InterruptedException ex) {
            logger.trace("", ex);
        } finally {
            if (!ok) {
                timedout(req, res);
            } else {
                ((AtmosphereResourceImpl) resource).cancel();
            }
        }
    }
}
Also used : OnResume(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with OnResume

use of org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume in project atmosphere by Atmosphere.

the class ManagedAtmosphereHandler method onRequest.

@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
    AtmosphereRequest request = resource.getRequest();
    String method = request.getMethod();
    boolean polling = Utils.pollableTransport(resource.transport());
    boolean webSocketMessage = Utils.webSocketMessage(resource);
    if (!webSocketMessage && !polling) {
        if (onReadyMethod != null) {
            resource.addEventListener(new OnSuspend() {

                @Override
                public void onSuspend(AtmosphereResourceEvent event) {
                    processReady(event.getResource());
                    resource.removeEventListener(this);
                }
            });
        }
        if (onResumeMethod != null) {
            resource.addEventListener(new OnResume() {

                @Override
                public void onResume(AtmosphereResourceEvent event) {
                    invoke(onResumeMethod, event);
                    resource.removeEventListener(this);
                }
            });
        }
        resource.addEventListener(new OnClose() {

            @Override
            public void onClose(AtmosphereResourceEvent event) {
                invoke(onDisconnectMethod, event);
            }
        });
    }
    if (method.equalsIgnoreCase("get")) {
        invoke(onGetMethod, resource);
    } else if (method.equalsIgnoreCase("post")) {
        Object body = null;
        if (onPostMethod != null) {
            body = readEntirely(resource);
            if (body != null && String.class.isAssignableFrom(body.getClass())) {
                resource.getRequest().body((String) body);
            } else if (body != null) {
                resource.getRequest().body((byte[]) body);
            }
            invoke(onPostMethod, resource);
        }
        MethodInfo.EncoderObject e = message(resource, body);
        if (e != null && e.encodedObject != null) {
            AtmosphereResource r = resource;
            if (e.methodInfo.deliverTo == DeliverTo.DELIVER_TO.RESOURCE && !resource.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) {
                r = resourcesFactory.find(resource.uuid());
            }
            IOUtils.deliver(new Managed(e.encodedObject), null, e.methodInfo.deliverTo, r);
        }
    } else if (method.equalsIgnoreCase("delete")) {
        invoke(onDeleteMethod, resource);
    } else if (method.equalsIgnoreCase("put")) {
        invoke(onPutMethod, resource);
    }
}
Also used : OnResume(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) OnSuspend(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnSuspend) AtmosphereResourceEvent(org.atmosphere.cpr.AtmosphereResourceEvent) OnClose(org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnClose)

Aggregations

AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)2 AtmosphereResourceEvent (org.atmosphere.cpr.AtmosphereResourceEvent)2 OnResume (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)1 OnClose (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnClose)1 OnSuspend (org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnSuspend)1 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1