private void callWithUi(final AtmosphereResource resource,
final PushEventCallback callback) {
AtmosphereRequest req = resource.getRequest();
VaadinServletRequest vaadinRequest = new VaadinServletRequest(req,
service);
VaadinSession session = null;
service.requestStart(vaadinRequest, null);
try {
try {
session = service.findVaadinSession(vaadinRequest);
} catch (ServiceException e) {
getLogger().log(Level.SEVERE,
"Could not get session. This should never happen", e);
return;
} catch (SessionExpiredException e) {
SystemMessages msg = service.getSystemMessages(
ServletPortletHelper.findLocale(null, null,
vaadinRequest), vaadinRequest);
sendNotificationAndDisconnect(
resource,
VaadinService.createCriticalNotificationJSON(
msg.getSessionExpiredCaption(),
msg.getSessionExpiredMessage(), null,
msg.getSessionExpiredURL()));
return;
}
UI ui = null;
session.lock();
try {
VaadinSession.setCurrent(session);
// Sets UI.currentInstance
ui = service.findUI(vaadinRequest);
if (ui == null) {
sendNotificationAndDisconnect(resource,
UidlRequestHandler.getUINotFoundErrorJSON(service,
vaadinRequest));
} else {
callback.run(resource, ui);
}
} catch (final IOException e) {
callErrorHandler(session, e);
} catch (final Exception e) {
SystemMessages msg = service.getSystemMessages(
ServletPortletHelper.findLocale(null, null,
vaadinRequest), vaadinRequest);
AtmosphereResource errorResource = resource;
if (ui != null && ui.getPushConnection() != null) {
// We MUST use the opened push connection if there is one.
// Otherwise we will write the response to the wrong request
// when using streaming (the client -> server request
// instead of the opened push channel)
errorResource = ((AtmospherePushConnection) ui
.getPushConnection()).getResource();
}
sendNotificationAndDisconnect(
errorResource,
VaadinService.createCriticalNotificationJSON(
msg.getInternalErrorCaption(),
msg.getInternalErrorMessage(), null,
msg.getInternalErrorURL()));
callErrorHandler(session, e);
} finally {
try {
session.unlock();
} catch (Exception e) {
getLogger().log(Level.WARNING,
"Error while unlocking session", e);
// can't call ErrorHandler, we (hopefully) don't have a lock
}