use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ExcludeSessionBroadcaster method broadcast.
/**
* a list of sessions will be exclude for this broadcast
*
* @param msg
* @param sessions
* @return
*/
public Future<Object> broadcast(Object msg, List<HttpSession> sessions) {
if (destroyed.get()) {
return futureDone(msg);
}
Set<AtmosphereResource> subset = new HashSet<AtmosphereResource>();
subset.addAll(resources);
for (AtmosphereResource r : resources) {
if (!r.getAtmosphereResourceEvent().isCancelled() && sessions.contains(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 OnDisconnectInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
if (Utils.webSocketMessage(r))
return Action.CONTINUE;
AtmosphereRequest request = AtmosphereResourceImpl.class.cast(r).getRequest(false);
String uuid = r.uuid();
if (closeMessage(request)) {
AtmosphereResource ss = config.resourcesFactory().find(uuid);
if (ss == null) {
logger.debug("No Suspended Connection found for {}. Using the AtmosphereResource associated with the close message", uuid);
ss = r;
}
if (ss == null) {
logger.debug("Was unable to execute onDisconnect on {}", r.uuid());
return Action.CONTINUE;
}
logger.debug("AtmosphereResource {} disconnected", uuid);
// Block websocket closing detection
AtmosphereResourceEventImpl.class.cast(ss.getAtmosphereResourceEvent()).isClosedByClient(true);
p.completeLifecycle(ss, false);
return Action.CANCELLED;
}
return Action.CONTINUE;
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class ManagedAtmosphereHandler method onRequest.
@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
AtmosphereRequest request = resource.getRequest();
String method = request.getMethod();
boolean polling = Utils.pollableTransport(resource.transport());
boolean webSocketMessage = Utils.webSocketMessage(resource);
if (!webSocketMessage && !polling) {
if (onReadyMethod != null) {
resource.addEventListener(new OnSuspend() {
@Override
public void onSuspend(AtmosphereResourceEvent event) {
processReady(event.getResource());
resource.removeEventListener(this);
}
});
}
if (onResumeMethod != null) {
resource.addEventListener(new OnResume() {
@Override
public void onResume(AtmosphereResourceEvent event) {
invoke(onResumeMethod, event);
resource.removeEventListener(this);
}
});
}
resource.addEventListener(new OnClose() {
@Override
public void onClose(AtmosphereResourceEvent event) {
invoke(onDisconnectMethod, event);
}
});
}
if (method.equalsIgnoreCase("get")) {
invoke(onGetMethod, resource);
} else if (method.equalsIgnoreCase("post")) {
Object body = null;
if (onPostMethod != null) {
body = readEntirely(resource);
if (body != null && String.class.isAssignableFrom(body.getClass())) {
resource.getRequest().body((String) body);
} else if (body != null) {
resource.getRequest().body((byte[]) body);
}
invoke(onPostMethod, resource);
}
MethodInfo.EncoderObject e = message(resource, body);
if (e != null && e.encodedObject != null) {
AtmosphereResource r = resource;
if (e.methodInfo.deliverTo == DeliverTo.DELIVER_TO.RESOURCE && !resource.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) {
r = resourcesFactory.find(resource.uuid());
}
IOUtils.deliver(new Managed(e.encodedObject), null, e.methodInfo.deliverTo, r);
}
} else if (method.equalsIgnoreCase("delete")) {
invoke(onDeleteMethod, resource);
} else if (method.equalsIgnoreCase("put")) {
invoke(onPutMethod, resource);
}
}
use of org.atmosphere.runtime.AtmosphereResource in project atmosphere by Atmosphere.
the class AbstractReflectorAtmosphereHandler method onStateChange.
/**
* Write the {@link AtmosphereResourceEvent#getMessage()} back to the client using
* the {@link AtmosphereResponseImpl#getOutputStream()} or {@link AtmosphereResponseImpl#getWriter()}.
* If a {@link org.atmosphere.runtime.Serializer} is defined, it will be invoked and the write operation
* will be delegated to it.
* <p/>
* By default, this method will try to use {@link AtmosphereResponseImpl#getWriter()}.
*
* @param event the {@link AtmosphereResourceEvent#getMessage()}
* @throws java.io.IOException
*/
@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
Object message = event.getMessage();
AtmosphereResource resource = event.getResource();
AtmosphereResponse r = resource.getResponse();
AtmosphereRequest request = resource.getRequest();
boolean writeAsBytes = IOUtils.isBodyBinary(request);
if (message == null) {
logger.trace("Message was null for AtmosphereEvent {}", event);
return;
}
if (resource.getSerializer() != null) {
try {
if (message instanceof List) {
for (Object s : (List<Object>) message) {
resource.getSerializer().write(resource.getResponse().getOutputStream(), s);
}
} else {
resource.getSerializer().write(resource.getResponse().getOutputStream(), message);
}
} catch (Throwable ex) {
logger.warn("Serializer exception: message: {}", message, ex);
throw new IOException(ex);
}
} else {
boolean isUsingStream = true;
Object o = resource.getRequest().getAttribute(PROPERTY_USE_STREAM);
if (o != null) {
isUsingStream = (Boolean) o;
}
if (!isUsingStream) {
try {
r.getWriter();
} catch (IllegalStateException e) {
isUsingStream = true;
}
if (writeAsBytes) {
throw new IllegalStateException("Cannot write bytes using PrintWriter");
}
}
if (message instanceof List) {
Iterator<Object> i = ((List) message).iterator();
try {
Object s;
while (i.hasNext()) {
s = i.next();
if (String.class.isAssignableFrom(s.getClass())) {
if (isUsingStream) {
r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
} else {
r.getWriter().write(s.toString());
}
} else if (byte[].class.isAssignableFrom(s.getClass())) {
if (isUsingStream) {
r.getOutputStream().write((byte[]) s);
} else {
r.getWriter().write(s.toString());
}
} else {
if (isUsingStream) {
r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
} else {
r.getWriter().write(s.toString());
}
}
i.remove();
}
} catch (IOException ex) {
event.setMessage(new ArrayList<String>().addAll((List) message));
throw ex;
}
if (isUsingStream) {
r.getOutputStream().flush();
} else {
r.getWriter().flush();
}
} else {
if (isUsingStream) {
r.getOutputStream().write(writeAsBytes ? (byte[]) message : message.toString().getBytes(r.getCharacterEncoding()));
r.getOutputStream().flush();
} else {
r.getWriter().write(message.toString());
r.getWriter().flush();
}
}
}
postStateChange(event);
}
Aggregations