-
-
Notifications
You must be signed in to change notification settings - Fork 751
Using AtmosphereInterceptor to customize Atmosphere Framework Dispatch Mechanism
The AtmosphereInterceptor interface allows application or framework integrator to intercept the AtmosphereResource before it gets dispatched to an AtmosphereHandler. As an example, The ReflectorServletProcessor handler' role is to dispatch AtmosphereResource to Servlet based application or framework. An application/framework can defines one or multiple AtmosphereInterceptor in order to intercept AtmosphereResource and manipulate its associated AtmosphereRequest and AtmosphereResponse. You define them in web.xml or application.xml In web.xml
<init-param>
<param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
<param-value>org.foo.BarAtmosphereInterceptor, org.bar.FooAtmosphereInterceptor</param-value>
</init-param>
or in atmosphere.xml
<applicationConfig>
<param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
<param-value>org.foo.BarAtmosphereInterceptor, org.bar.FooAtmosphereInterceptor</param-value>
</applicationConfig>
The AtmosphereInterceptor API looks like
public interface AtmosphereInterceptor {
Action inspect(AtmosphereResource r);
}
An AtmosphereInterceptor can suspend, resume, timeout, cancel or just continue the AtmosphereResource processing by returning the appropriate Action . As an example, the following AtmosphereInterceptor will cancel the processing based on some security token
@Override
public Action inspect(AtmosphereResource r) {
AtmosphereRequest req = r.getRequest();
if (req.getHeader("SomeSecurityToken") == null) {
return Action.CANCELLED;
} else {
return Action.CONTINUE;
}
}
You can also suspend the AtmosphereResource without delivering the request to an application/framework. This is useful when you just want to suspend and not dispatch anything.
@Override
public Action inspect(AtmosphereResource r) {
r.suspend();
return Action.SUSPEND;
}
You can also manipulate the AtmosphereResponse. As an example, below is an SSEAtmosphereInterceptor, which add Server Side Events support to Atmosphere
@Override
public Action inspect(final AtmosphereResource r) {
final AtmosphereResponse response = r.getResponse();
if (r.transport().equals(AtmosphereResource.TRANSPORT.SSE)) {
response.asyncIOWriter(new AsyncIOWriter() {
@Override
public void redirect(String location) throws IOException {
response.sendRedirect(location);
}
@Override
public void writeError(int errorCode, String message) throws IOException {
response.sendError(errorCode);
}
@Override
public void write(String data) throws IOException {
AtmosphereResourceImpl.class.cast(r).writeSSE(false);
response.write("data:" + data + "\n\n");
}
// TODO: Performance: execute a single write
@Override
public void write(byte[] data) throws IOException {
AtmosphereResourceImpl.class.cast(r).writeSSE(false);
response.write("data:").write(data).write("\n\n");
}
@Override
public void write(byte[] data, int offset, int length) throws IOException {
AtmosphereResourceImpl.class.cast(r).writeSSE(false);
response.write("data:").write(data, offset, length).write("\n\n");
}
@Override
public void close() throws IOException {
response.closeStreamOrWriter();
}
@Override
public void flush() throws IOException {
response.flushBuffer();
}
});
}
return Action.CONTINUE;
}
- Understanding Atmosphere
- Understanding @ManagedService
- Using javax.inject.Inject and javax.inject.PostConstruct annotation
- Understanding Atmosphere's Annotation
- Understanding AtmosphereResource
- Understanding AtmosphereHandler
- Understanding WebSocketHandler
- Understanding Broadcaster
- Understanding BroadcasterCache
- Understanding Meteor
- Understanding BroadcastFilter
- Understanding Atmosphere's Events Listeners
- Understanding AtmosphereInterceptor
- Configuring Atmosphere for Performance
- Understanding JavaScript functions
- Understanding AtmosphereResourceSession
- Improving Performance by using the PoolableBroadcasterFactory
- Using Atmosphere Jersey API
- Using Meteor API
- Using AtmosphereHandler API
- Using Socket.IO
- Using GWT
- Writing HTML5 Server-Sent Events
- Using STOMP protocol
- Streaming WebSocket messages
- Configuring Atmosphere's Classes Creation and Injection
- Using AtmosphereInterceptor to customize Atmosphere Framework
- Writing WebSocket sub protocol
- Configuring Atmosphere for the Cloud
- Injecting Atmosphere's Components in Jersey
- Sharing connection between Browser's windows and tabs
- Understanding AtmosphereResourceSession
- Manage installed services
- Server Side: javadoc API
- Server Side: atmosphere.xml and web.xml configuration
- Client Side: atmosphere.js API