@SuppressWarnings("unchecked")
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
Object msg = event.getMessage();
// Original Value
AtmosphereResourceImpl r = AtmosphereResourceImpl.class.cast(event.getResource());
Boolean resumeOnBroadcast = r.resumeOnBroadcast();
if (!resumeOnBroadcast) {
// For legacy reason, check the attribute as well
Object o = r.getRequest(false).getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
resumeOnBroadcast = Boolean.class.cast(o);
}
}
// Disable resume so cached message can be send in one chunk.
if (resumeOnBroadcast) {
r.resumeOnBroadcast(false);
r.getRequest(false).setAttribute(ApplicationConfig.RESUME_ON_BROADCAST, false);
}
if (event.isCancelled() || event.isClosedByClient()) {
invoke(onDisconnectMethod, event);
} else if (event.isResumedOnTimeout() || event.isResuming()) {
invoke(onTimeoutMethod, event);
} else {
Object o;
if (msg != null) {
if (Managed.class.isAssignableFrom(msg.getClass())) {
Object newMsg = Managed.class.cast(msg).o;
event.setMessage(newMsg);
// No method matched. Give a last chance by trying to decode the proxiedInstance.
// This makes application development more simpler.
// Chaining of encoder is not supported.
// TODO: This could be problematic with String + method
for (MethodInfo m : onRuntimeMethod) {
o = Invoker.encode(encoders.get(m.method), newMsg);
if (o != null) {
event.setMessage(o);
break;
}
}
} else {
logger.trace("BroadcasterFactory has been used, this may produce recursion if encoder/decoder match the broadcasted message");
final MethodInfo.EncoderObject e = message(r, msg);
o = e == null ? null : e.encodedObject;
if (o != null) {
event.setMessage(o);
}
}
}
super.onStateChange(event);
}
if (resumeOnBroadcast && r.isSuspended()) {
r.resume();
}
}