Skip to content

Understanding AtmosphereInterceptor

jfarcand edited this page Jun 7, 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:

  • SSEAtmosphereInterceptor: Add supports for HTML5 Server Side Events. Installed by default.
  • JSONPAtmosphereInterceptor: Add supports for JSONP/Callback transport. Installed by default.
  • AtmosphereResourceLifecycleInterceptor: Manage the life cycle of an AtmosphereResource. By default, the interceptor always suspend GET requests, and automatically resume/flush/handle the Response's state.
  • MessageLengthInterceptor: An interceptor that add a special String "|" at the end of a broadcasted message, allowing the atmosphere.js javascript client to detect if one or several messages where aggregated in one write operations and delivered as a single message. *BroadcastOnPostAtmosphereInterceptor : An interceptor that broadcast the request's body after it has been delivered to an AtmosphereHandler.

For example, if you don't want to manage the AtmosphereResource lifecycle, you can do:

@AtmosphereHandlerService(path = "/chat", interceptors= {"org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor"})
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= {"org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor",
"org.atmosphere.interceptor.BroadcastOnPostAtmosphereInterceptor"})
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