Skip to content

Navigation Menu

Sign in
Sign up

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. You can define AtmosphereInterceptor that apply to all AtmosphereHandler by annotating them using:

 @AtmosphereInterceptorService

or per Meteor or AtmosphereHandler using

 @MeteorService(interceptors={...})
 @AtmosphereHandlerService(interceptors={...}) 

You can also define them in web/atmosphere.xml

 // application.xml
 <applicationConfig>
 <param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
 <param-value>list of classes</param-value>
 </applicationConfig>
 // web.xml
 <init-param>
 <param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
 <param-value>list of classes</param-value>
 </init-param>

or per AtmosphereHandler in atmosphere.xml

<atmosphere-handler 
 support-session="true"
 context-root="/*" class-name="..."
 broadcaster="..."
 broadcasterCache=""
 broadcastFilterClasses=""
 interceptorClasses=""
 comet-support=""
>

AtmosphereInterceptor allows 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

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