Skip to content

Navigation Menu

Sign in
Sign up

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.

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=
{"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

AltStyle によって変換されたページ (->オリジナル) /