use of org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume in project atmosphere by Atmosphere.
the class BlockingIOCometSupport method suspend.
/**
* Suspend the connection by blocking the current {@link Thread}
*
* @param action The {@link Action}
* @param req the {@link AtmosphereRequest}
* @param res the {@link AtmosphereResponse}
*/
protected void suspend(Action action, AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
final CountDownLatch latch = new CountDownLatch(1);
req.setAttribute(LATCH, latch);
boolean ok = true;
AtmosphereResource resource = req.resource();
if (resource != null) {
try {
resource.addEventListener(new OnResume() {
@Override
public void onResume(AtmosphereResourceEvent event) {
latch.countDown();
}
});
if (action.timeout() != -1) {
ok = latch.await(action.timeout(), TimeUnit.MILLISECONDS);
} else {
latch.await();
}
} catch (InterruptedException ex) {
logger.trace("", ex);
} finally {
if (!ok) {
timedout(req, res);
} else {
((AtmosphereResourceImpl) resource).cancel();
}
}
}
}
use of org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnResume 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);
}
}
Aggregations