Skip to content

Understanding AtmosphereInterceptor

jfarcand edited this page Jun 8, 2012 · 18 revisions

The Framework ships with several implementation of AtmosphereInterceptor. Interceptor are called before and after AtmosphereHandler. They allow an application to customize the request/response before they get delivered to the AtmosphereHandler. Atmosphere ships with several implementation:

Reducing the number of code in your application

Without interceptor, an AtmosphereHandler would looks like

@AtmosphereHandlerService(path="/chat")
public class ChatAtmosphereHandler implements AtmosphereHandler {

    @Override
    public void onRequest(AtmosphereResource r) 
           throws IOException {

        AtmosphereRequest req = r.getRequest();
        // First, tell Atmosphere to allow bi-directional communication by suspending.
        if (req.getMethod().equalsIgnoreCase("GET")) {
                r.suspend();
        // Second, broadcast message to all connected users.
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            r.getBroadcaster().broadcast(req.getReader().readLine().trim());
        }
    }

    @Override
    public void onStateChange(AtmosphereResourceEvent event) 
            throws IOException {
        AtmosphereResource r = event.getResource();
        AtmosphereResponse res = r.getResponse();

        if (event.isSuspended()) {
            ...

            switch (r.transport()) {
                case JSONP:
                case AJAX:
                case LONG_POLLING:
                    event.getResource().resume();
                    break;
                default:
                    res.getWriter().flush();
                    break;
            }
        } 
    }

Now let the framework manage the Atmosphereresource dore :

@AtmosphereHandlerService(path = "/chat", interceptors=
{AtmosphereResourceLifecycleInterceptor.class})
public class SocketIOChatAtmosphereHandler implements AtmosphereHandler {

    @Override
    public void onRequest(AtmosphereResource r) 
           throws IOException {
        r.getBroadcaster().broadcast(r.getRequest().getReader().readLine());
    }

    @Override
    public void onStateChange(AtmosphereResourceEvent event) 
           throws IOException {
        AtmosphereResource r = event.getResource();
        AtmosphereResponse res = r.getResponse();

        if (event.isSuspended()) {
            .....
        }
    }

Better, if you don't want to handle the broadcast operation as well, just do:

@AtmosphereHandlerService(path = "/chat", interceptors= 
{AtmosphereResourceLifecycleInterceptor.class,
 BroadcastOnPostAtmosphereInterceptor.class})
public class SocketIOChatAtmosphereHandler implements AtmosphereHandler {

    @Override
    public void onRequest(AtmosphereResource r) 
       throws IOException {
    }

    @Override
    public void onStateChange(AtmosphereResourceEvent event) 
           throws IOException {
        AtmosphereResource r = event.getResource();
        AtmosphereResponse res = r.getResponse();

        if (event.isSuspended()) {
            .....
        }
    }

Clone this wiki locally