use of org.atmosphere.runtime.AtmosphereResponse in project atmosphere by Atmosphere.
the class ManagedAtmosphereHandlerTest method create.
@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
framework.addAnnotationPackage(ManagedGet.class);
framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {
@Override
public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
return suspended(req, res);
}
public void action(AtmosphereResourceImpl r) {
try {
resumed(r.getRequest(), r.getResponse());
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}).init(new ServletConfig() {
@Override
public String getServletName() {
return "void";
}
@Override
public ServletContext getServletContext() {
return mock(ServletContext.class);
}
@Override
public String getInitParameter(String name) {
return ApplicationConfig.CLIENT_HEARTBEAT_INTERVAL_IN_SECONDS.equals(name) ? "10" : null;
}
@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
});
}
use of org.atmosphere.runtime.AtmosphereResponse in project atmosphere by Atmosphere.
the class WebSocketTest method testTransformWithConcurrency.
@Test
public void testTransformWithConcurrency() throws Exception {
WebSocket ws = new WebSocket(framework.getAtmosphereConfig()) {
@Override
public boolean isOpen() {
return false;
}
@Override
public WebSocket write(String s) throws IOException {
return null;
}
@Override
public WebSocket write(byte[] b, int offset, int length) throws IOException {
return null;
}
@Override
public void close() {
}
};
ws.interceptor(new DummyInterceptor(500));
ws.interceptor(new DummyInterceptor(10));
AtmosphereResponse response1 = AtmosphereResponseImpl.newInstance();
AtmosphereResponse response2 = AtmosphereResponseImpl.newInstance();
Worker worker1 = new Worker(ws, response1);
Worker worker2 = new Worker(ws, response2);
Thread t1 = new Thread(worker1);
Thread t2 = new Thread(worker2);
t1.start();
t2.start();
t1.join(2000);
t2.join(2000);
assertFalse(worker1.isCorrupted(), "corrupted");
assertFalse(worker2.isCorrupted(), "corrupted");
}
use of org.atmosphere.runtime.AtmosphereResponse in project atmosphere by Atmosphere.
the class CorsInterceptor method inspect.
@Override
public Action inspect(AtmosphereResource r) {
if (Utils.webSocketMessage(r))
return Action.CONTINUE;
if (!enableAccessControl)
return Action.CONTINUE;
AtmosphereRequest req = r.getRequest();
AtmosphereResponse res = r.getResponse();
if (req.getHeader("Origin") != null && res.getHeader("Access-Control-Allow-Origin") == null) {
res.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
res.addHeader("Access-Control-Expose-Headers", EXPOSE_HEADERS);
res.setHeader("Access-Control-Allow-Credentials", "true");
}
if ("OPTIONS".equals(req.getMethod())) {
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
res.setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, AuthToken, X-Atmosphere-Framework, X-Requested-With, " + EXPOSE_HEADERS + ", X-Atmosphere-Transport, X-Atmosphere-TrackMessageSize, X-atmo-protocol");
res.setHeader("Access-Control-Max-Age", "-1");
return Action.SKIP_ATMOSPHEREHANDLER;
}
return Action.CONTINUE;
}
use of org.atmosphere.runtime.AtmosphereResponse in project atmosphere by Atmosphere.
the class JSONPAtmosphereInterceptor method inspect.
@Override
public Action inspect(AtmosphereResource r) {
if (Utils.webSocketMessage(r))
return Action.CONTINUE;
final AtmosphereRequest request = r.getRequest();
final AtmosphereResponse response = r.getResponse();
// Shield from Broken server
String uri = request.getRequestURI() == null ? "" : request.getRequestURI();
if (r.transport().equals(AtmosphereResource.TRANSPORT.JSONP) || uri.indexOf("jsonp") != -1) {
super.inspect(r);
if (uri.indexOf("jsonp") != -1) {
startChunk = "(\"";
endChunk = "\");\r\n\r\n";
}
AsyncIOWriter writer = response.getAsyncIOWriter();
if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
AtmosphereInterceptorWriter.class.cast(writer).interceptor(new AsyncIOInterceptorAdapter() {
String callbackName() {
String callback = request.getParameter(HeaderConfig.JSONP_CALLBACK_NAME);
if (callback == null) {
// Look for extension
String jsonp = (String) config.properties().get(HeaderConfig.JSONP_CALLBACK_NAME);
if (jsonp != null) {
callback = request.getParameter(jsonp);
}
}
return callback;
}
@Override
public void prePayload(AtmosphereResponse response, byte[] data, int offset, int length) {
String callbackName = callbackName();
response.write(callbackName + startChunk);
}
@Override
public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException {
String charEncoding = response.getCharacterEncoding() == null ? "UTF-8" : response.getCharacterEncoding();
return escapeForJavaScript(new String(responseDraft, charEncoding)).getBytes(charEncoding);
}
@Override
public void postPayload(AtmosphereResponse response, byte[] data, int offset, int length) {
response.write(endChunk, true);
}
});
} else {
logger.warn("Unable to apply {}. Your AsyncIOWriter must implement {}", getClass().getName(), AtmosphereInterceptorWriter.class.getName());
}
}
return Action.CONTINUE;
}
use of org.atmosphere.runtime.AtmosphereResponse in project atmosphere by Atmosphere.
the class JavaScriptProtocol method inspect.
@Override
public Action inspect(final AtmosphereResource ar) {
if (Utils.webSocketMessage(ar))
return Action.CONTINUE;
final AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(ar);
final AtmosphereRequest request = r.getRequest(false);
final AtmosphereResponse response = r.getResponse(false);
String uuid = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID);
String handshakeUUID = request.getHeader(HeaderConfig.X_ATMO_PROTOCOL);
if (uuid != null && uuid.equals("0") && handshakeUUID != null) {
if (enforceAtmosphereVersion) {
String javascriptVersion = request.getHeader(HeaderConfig.X_ATMOSPHERE_FRAMEWORK);
int version = 0;
if (javascriptVersion != null) {
version = parseVersion(javascriptVersion.split("-")[0]);
}
if (version < 221) {
logger.error("Invalid Atmosphere Version {}", javascriptVersion);
response.setStatus(501);
response.addHeader(X_ATMOSPHERE_ERROR, "Atmosphere Protocol version not supported.");
try {
response.flushBuffer();
} catch (IOException e) {
}
return Action.CANCELLED;
}
}
request.header(HeaderConfig.X_ATMO_PROTOCOL, null);
// Extract heartbeat data
int heartbeatInterval = 0;
String heartbeatData = "";
for (final AtmosphereInterceptor interceptor : framework.interceptors()) {
if (HeartbeatInterceptor.class.isAssignableFrom(interceptor.getClass())) {
final HeartbeatInterceptor heartbeatInterceptor = HeartbeatInterceptor.class.cast(interceptor);
heartbeatInterval = heartbeatInterceptor.clientHeartbeatFrequencyInSeconds() * 1000;
heartbeatData = new String(heartbeatInterceptor.getPaddingBytes());
break;
}
}
String message;
if (enforceAtmosphereVersion) {
// UUID since 1.0.10
message = new StringBuilder(r.uuid()).append(wsDelimiter).append(heartbeatInterval).append(wsDelimiter).append(heartbeatData).append(wsDelimiter).toString();
} else {
// UUID since 1.0.10
message = r.uuid();
}
// https://github.com/Atmosphere/atmosphere/issues/993
final AtomicReference<String> protocolMessage = new AtomicReference<String>(message);
if (r.getBroadcaster().getBroadcasterConfig().hasFilters()) {
for (BroadcastFilter bf : r.getBroadcaster().getBroadcasterConfig().filters()) {
if (TrackMessageSizeFilter.class.isAssignableFrom(bf.getClass())) {
protocolMessage.set((String) f.filter(r.getBroadcaster().getID(), r, protocolMessage.get(), protocolMessage.get()).message());
break;
}
}
}
if (!Utils.resumableTransport(r.transport())) {
OnSuspend a = new OnSuspend() {
@Override
public void onSuspend(AtmosphereResourceEvent event) {
response.write(protocolMessage.get());
try {
response.flushBuffer();
} catch (IOException e) {
logger.trace("", e);
}
r.removeEventListener(this);
}
};
// Pass the information to Servlet Based Framework
request.setAttribute(CALLBACK_JAVASCRIPT_PROTOCOL, a);
r.addEventListener(a);
} else {
response.write(protocolMessage.get());
}
// We don't need to reconnect here
if (r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET || r.transport() == AtmosphereResource.TRANSPORT.STREAMING || r.transport() == AtmosphereResource.TRANSPORT.SSE) {
return Action.CONTINUE;
} else {
return Action.CANCELLED;
}
}
return Action.CONTINUE;
}
Aggregations