use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ProtocolUtil method constructRequest.
protected static AtmosphereRequestImpl.Builder constructRequest(WebSocket webSocket, String pathInfo, String requestURI, String methodType, String contentType, boolean destroyable) {
AtmosphereResource resource = webSocket.resource();
AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false);
Map<String, Object> m = attributes(webSocket, request);
// We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection.
AtmosphereRequestImpl.Builder b = (new AtmosphereRequestImpl.Builder().request(request).method(methodType).contentType(contentType == null ? request.getContentType() : contentType).attributes(m).pathInfo(pathInfo).contextPath(request.getContextPath()).servletPath(request.getServletPath()).requestURI(requestURI).requestURL(request.requestURL()).destroyable(destroyable).headers(request.headersMap()).session(resource.session()));
return b;
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class AnnotationScanningTest method create.
@BeforeMethod
public void create() throws Throwable {
framework = new AtmosphereFramework();
framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
framework.addAnnotationPackage(AnnotationScanningTest.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().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {
@Override
public void onRequest(AtmosphereResource resource) throws IOException {
resource.suspend();
}
});
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ExcludeSessionBroadcaster method broadcast.
/**
* the AtmosphereResource r will be exclude for this broadcast
*
* @param msg
* @param r
* @return
*/
@Override
public Future<Object> broadcast(Object msg, AtmosphereResource r) {
if (destroyed.get()) {
throw new IllegalStateException("This Broadcaster has been destroyed and cannot be used");
}
Set<AtmosphereResource> sub = new HashSet<AtmosphereResource>();
sub.addAll(resources);
sub.remove(r);
start();
Object newMsg = filter(msg);
if (newMsg == null) {
return null;
}
BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, sub.size());
dispatchMessages(new Deliver(newMsg, sub, f, msg));
return f;
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ExcludeSessionBroadcaster method broadcast.
/**
* session will be exclude for this broadcast
*
* @param msg
* @param s
* @return
*/
public Future<Object> broadcast(Object msg, HttpSession s) {
if (destroyed.get()) {
return futureDone(msg);
}
Set<AtmosphereResource> subset = new HashSet<AtmosphereResource>();
subset.addAll(resources);
for (AtmosphereResource r : resources) {
if (!r.getAtmosphereResourceEvent().isCancelled() && s.equals(r.getRequest().getSession())) {
subset.remove(r);
}
}
start();
Object newMsg = filter(msg);
if (newMsg == null) {
return futureDone(msg);
}
BroadcasterFuture<Object> f = new BroadcasterFuture<Object>(newMsg, subset.size());
dispatchMessages(new Deliver(newMsg, subset, f, msg));
return f;
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ManagedAtmosphereHandlerTest method testHeartbeat.
@Test
public void testHeartbeat() throws IOException, ServletException {
// Open connection
AtmosphereRequest request = new AtmosphereRequestImpl.Builder().pathInfo("/heartbeat").method("GET").build();
request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
// Check suspend
final AtmosphereResource res = r.get();
assertNotNull(res);
// Send heartbeat
request = new AtmosphereRequestImpl.Builder().pathInfo("/heartbeat").method("POST").body(Heartbeat.paddingData).build();
request.header(X_ATMOSPHERE_TRANSPORT, WEBSOCKET_TRANSPORT);
request.setAttribute(HeartbeatInterceptor.INTERCEPTOR_ADDED, "");
res.initialize(res.getAtmosphereConfig(), res.getBroadcaster(), request, AtmosphereResponseImpl.newInstance(), framework.getAsyncSupport(), res.getAtmosphereHandler());
request.setAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE, res);
framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
assertNotNull(message.get());
assertEquals(message.get(), Heartbeat.paddingData);
}
Aggregations