Search in sources :

Example 1 with OnClose

use of org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnClose 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.runtime.AtmosphereResourceEventListenerAdapter.OnResume) AtmosphereRequest(org.atmosphere.runtime.AtmosphereRequest) AtmosphereResource(org.atmosphere.runtime.AtmosphereResource) OnSuspend(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnSuspend) AtmosphereResourceEvent(org.atmosphere.runtime.AtmosphereResourceEvent) OnClose(org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnClose)

Aggregations

AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)1 AtmosphereResource (org.atmosphere.runtime.AtmosphereResource)1 AtmosphereResourceEvent (org.atmosphere.runtime.AtmosphereResourceEvent)1 OnClose (org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnClose)1 OnResume (org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnResume)1 OnSuspend (org.atmosphere.runtime.AtmosphereResourceEventListenerAdapter.OnSuspend)1