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 AtmosphereEvent.suspend() and later resume via AtmosphereEvent.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 onMessage(org.atmosphere.cpr.AtmosphereEvent). A class implementing AtmosphereHandler must be thread safe For example, a simple ChatAtmosphereHandler will take the form of



    public AtmosphereEvent<HttpServletRequest, HttpServletResponse>
            onEvent(AtmosphereEvent<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")) {
            // for IE
            res.getWriter().write("<!-- Comet is a programming technique that enables web " +
                    "servers to send data to the client without having any need " +
                    "for the client to request it. -->\n");
            res.getWriter().flush();

            //
            // Mark this connection as suspended.
            //
            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();
            }
        }
        return event;
    }

    public AtmosphereEvent<HttpServletRequest, HttpServletResponse>
            onMessage(AtmosphereEvent<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();
        }
        return event;
    }

 

Author:
Jeanfrancois Arcand

Method Summary
 AtmosphereEvent onEvent(AtmosphereEvent<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.
 AtmosphereEvent onMessage(AtmosphereEvent<F,G> event)
          This method is invoked when the Broadcaster execute a broadcast operations.
 

Method Detail

onEvent

AtmosphereEvent onEvent(AtmosphereEvent<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 AtmosphereEvent
Returns:
the modified AtmosphereEvent
Throws:
IOException

onMessage

AtmosphereEvent onMessage(AtmosphereEvent<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 AtmosphereEvent.resume() gets invoked. In that case, AtmosphereEvent.isResuming() will return true.
This method will also be invoked when the AtmosphereEvent#suspend(int) expires. In that case, AtmosphereEvent.isResumedOnTimeout() will return true

Parameters:
event - an AtmosphereEvent
Returns:
the modified AtmosphereEvent
Throws:
IOException


Copyright © 2009 SUN Microsystems. All Rights Reserved.