org.atmosphere.cpr
Interface AtmosphereHandler<F,G>

All Known Subinterfaces:
AtmosphereServletProcessor
All Known Implementing Classes:
AbstractReflectorAtmosphereHandler, ReflectorServletProcessor

public interface AtmosphereHandler<F,G>

Implementation of AtmosphereHandler allows creation of event-driven web applications which are hosted in the browser. An AtmosphereHandler allows a web application to suspend and resume an http response. An http response can be suspended AtmosphereResource.suspend() and later resume via AtmosphereResource.resume(). Messages can also be shared between suspended response and their associated AtmosphereHandler using a Broadcaster. Any invocation of Broadcaster.broadcast(java.lang.Object) will allow a suspended response to write the content of the message onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent). A class implementing AtmosphereHandler must be thread safe For example, a simple Chat based AtmosphereHandler will take the form of



    public AtmosphereResource<HttpServletRequest, HttpServletResponse>
            onRequest(AtmosphereResource<HttpServletRequest, HttpServletResponse> event) throws IOException {
        HttpServletRequest req = event.getRequest();
        HttpServletResponse res = event.getResponse();

        res.setContentType("text/html");
        res.addHeader("Cache-Control", "private");
        res.addHeader("Pragma", "no-cache");
        if (req.getMethod().equalsIgnoreCase("GET")) {
            event.suspend();
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            res.setCharacterEncoding("UTF-8");
            String action = req.getParameterValues("action")[0];
            String name = req.getParameterValues("name")[0];

            if ("login".equals(action)) {
                event.getBroadcaster().broadcast(
                        BEGIN_SCRIPT_TAG + toJsonp("System Message from "
                        + event.getWebServerName(), name + " has joined.") + END_SCRIPT_TAG);
                res.getWriter().write("success");
                res.getWriter().flush();
            } else if ("post".equals(action)) {
                String message = req.getParameterValues("message")[0];
                event.getBroadcaster().broadcast(BEGIN_SCRIPT_TAG + toJsonp(name, message) + END_SCRIPT_TAG);
                res.getWriter().write("success");
                res.getWriter().flush();
            } else {
                res.setStatus(422);

                res.getWriter().write("success");
                res.getWriter().flush();
            }
        }
    }

    public AtmosphereResource<HttpServletRequest, HttpServletResponse>
            onStateChange(AtmosphereResource<HttpServletRequest, HttpServletResponse> event) throws IOException {
        HttpServletRequest req = event.getRequest();
        HttpServletResponse res = event.getResponse();

        if (event.isResuming() || event.isResumedOnTimedout()) {
            String script = BEGIN_SCRIPT_TAG + "window.parent.app.listen();\n" + END_SCRIPT_TAG;

            res.getWriter().write(script);
            res.getWriter().flush();
        } else {
            res.getWriter().write(event.getMessage().toString());
            res.getWriter().flush();
        }
    }

 

Author:
Jeanfrancois Arcand

Method Summary
 void onRequest(AtmosphereResource<F,G> event)
          When a client send a request to its associated AtmosphereHandler, it can decide if the underlying connection can be suspended (creating a Continuation) or handle the connection synchronously.
 void onStateChange(AtmosphereResourceEvent<F,G> event)
          This method is invoked when the Broadcaster execute a broadcast operations.
 

Method Detail

onRequest

void onRequest(AtmosphereResource<F,G> event)
               throws IOException
When a client send a request to its associated AtmosphereHandler, it can decide if the underlying connection can be suspended (creating a Continuation) or handle the connection synchronously. It is recommended to only suspend request for which HTTP method is a GET and use the POST method to send data to the server, without marking the connection as asynchronous.

Parameters:
event - an AtmosphereResource
Throws:
IOException

onStateChange

void onStateChange(AtmosphereResourceEvent<F,G> event)
                   throws IOException
This method is invoked when the Broadcaster execute a broadcast operations. When this method is invoked its associated Broadcaster, any suspended connection will be allowed to write the data back to its associated clients.
This method will also be invoked when a response get resumed, e.g. when AtmosphereResource.resume() gets invoked. In that case, AtmosphereResourceEvent.isResuming() will return true.
This method will also be invoked when the AtmosphereResource.suspend(long) expires. In that case, AtmosphereResourceEvent.isResumedOnTimeout() will return true

Parameters:
event - an AtmosphereResourceEvent
Throws:
IOException


Copyright © 2010 SUN Microsystems. All Rights Reserved.