use of org.atmosphere.cpr.AtmosphereFramework in project cxf by apache.
the class AtmosphereSseServletDestination method create.
private AtmosphereFramework create() {
final AtmosphereFramework instance = new AtmosphereFramework(true, false);
instance.interceptor(new SseAtmosphereInterceptor());
instance.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true");
instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true");
instance.addInitParameter(ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTOR, "true");
instance.addInitParameter(ApplicationConfig.CLOSE_STREAM_ON_CANCEL, "true");
// Atmosphere does not limit amount of threads and application can crash in no time
// https://github.com/Atmosphere/atmosphere/wiki/Configuring-Atmosphere-for-Performance
instance.addInitParameter(ApplicationConfig.BROADCASTER_MESSAGE_PROCESSING_THREADPOOL_MAXSIZE, "20");
instance.addInitParameter(ApplicationConfig.BROADCASTER_ASYNC_WRITE_THREADPOOL_MAXSIZE, "20");
// workaround for atmosphere's jsr356 initialization requiring servletConfig
instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true");
instance.setBroadcasterCacheClassName(UUIDBroadcasterCache.class.getName());
instance.addAtmosphereHandler("/", new DestinationHandler());
return instance;
}
use of org.atmosphere.cpr.AtmosphereFramework in project cxf by apache.
the class DefaultProtocolInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
LOG.log(Level.FINE, "inspect");
if (AtmosphereResource.TRANSPORT.WEBSOCKET != r.transport() && AtmosphereResource.TRANSPORT.SSE != r.transport() && AtmosphereResource.TRANSPORT.POLLING != r.transport()) {
LOG.fine("Skipping ignorable request");
return Action.CONTINUE;
}
if (AtmosphereResource.TRANSPORT.POLLING == r.transport()) {
final String saruuid = (String) r.getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
final AtmosphereResponse suspendedResponse = suspendedResponses.get(saruuid);
LOG.fine("Attaching a proxy writer to suspended response");
r.getResponse().asyncIOWriter(new AtmosphereInterceptorWriter() {
@Override
public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException {
suspendedResponse.write(data);
suspendedResponse.flushBuffer();
return this;
}
@Override
public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException {
suspendedResponse.write(data);
suspendedResponse.flushBuffer();
return this;
}
@Override
public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException {
suspendedResponse.write(data, offset, length);
suspendedResponse.flushBuffer();
return this;
}
@Override
public void close(AtmosphereResponse response) throws IOException {
}
});
// REVISIT we need to keep this response's asyncwriter alive so that data can be written to the
// suspended response, but investigate if there is a better alternative.
r.getResponse().destroyable(false);
return Action.CONTINUE;
}
r.addEventListener(new AtmosphereResourceEventListenerAdapter() {
@Override
public void onSuspend(AtmosphereResourceEvent event) {
final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
LOG.log(Level.FINE, "Registrering suspended resource: {}", srid);
suspendedResponses.put(srid, event.getResource().getResponse());
AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
if (writer instanceof AtmosphereInterceptorWriter) {
((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
}
}
@Override
public void onDisconnect(AtmosphereResourceEvent event) {
super.onDisconnect(event);
final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
LOG.log(Level.FINE, "Unregistrering suspended resource: {}", srid);
suspendedResponses.remove(srid);
}
});
AtmosphereRequest request = r.getRequest();
if (request.getAttribute(REQUEST_DISPATCHED) == null) {
AtmosphereResponse response = null;
AtmosphereFramework framework = r.getAtmosphereConfig().framework();
try {
byte[] data = WebSocketUtils.readBody(request.getInputStream());
if (data.length == 0) {
if (AtmosphereResource.TRANSPORT.WEBSOCKET == r.transport() || AtmosphereResource.TRANSPORT.SSE == r.transport()) {
r.suspend();
return Action.SUSPEND;
}
return Action.CANCELLED;
}
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "inspecting data {0}", new String(data));
}
try {
AtmosphereRequest ar = createAtmosphereRequest(request, data);
response = new WrappedAtmosphereResponse(r.getResponse(), ar);
ar.localAttributes().put(REQUEST_DISPATCHED, "true");
String refid = ar.getHeader(WebSocketConstants.DEFAULT_REQUEST_ID_KEY);
if (refid != null) {
ar.localAttributes().put(WebSocketConstants.DEFAULT_REQUEST_ID_KEY, refid);
}
// This is a new request, we must clean the Websocket AtmosphereResource.
request.removeAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
response.request(ar);
attachWriter(r);
Action action = framework.doCometSupport(ar, response);
if (action.type() == Action.TYPE.SUSPEND) {
ar.destroyable(false);
response.destroyable(false);
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Error during request dispatching", e);
if (response == null) {
response = new WrappedAtmosphereResponse(r.getResponse(), request);
}
if (e instanceof InvalidPathException) {
response.setIntHeader(WebSocketUtils.SC_KEY, 400);
} else {
response.setIntHeader(WebSocketUtils.SC_KEY, 500);
}
OutputStream out = response.getOutputStream();
out.write(createResponse(response, null, true));
out.close();
}
return Action.CANCELLED;
} catch (IOException e) {
LOG.log(Level.WARNING, "Error during protocol processing", e);
}
} else {
request.destroyable(false);
}
return Action.CONTINUE;
}
use of org.atmosphere.cpr.AtmosphereFramework in project cxf by apache.
the class AtmosphereWebSocketServletDestination method create.
private AtmosphereFramework create(Bus bus) {
final AtmosphereFramework instance = new AtmosphereFramework(false, true);
instance.setUseNativeImplementation(false);
instance.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true");
instance.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true");
instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true");
instance.addInitParameter(ApplicationConfig.WEBSOCKET_PROTOCOL_EXECUTION, "true");
// workaround for atmosphere's jsr356 initialization requiring servletConfig
instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true");
AtmosphereUtils.addInterceptors(instance, bus);
instance.addAtmosphereHandler("/", new DestinationHandler());
return instance;
}
Aggregations