use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.
the class DebugWindowConnectionTest method reload_resourceIsNotSet_reloadCommandIsNotSent.
@Test
public void reload_resourceIsNotSet_reloadCommandIsNotSent() {
AtmosphereResource resource = Mockito.mock(AtmosphereResource.class);
Broadcaster broadcaster = Mockito.mock(Broadcaster.class);
Mockito.when(resource.getBroadcaster()).thenReturn(broadcaster);
Assert.assertFalse(reload.isLiveReload(resource));
reload.reload();
Mockito.verifyNoInteractions(broadcaster);
}
use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.
the class DebugWindowConnectionTest method onConnect_suspend_sayHello.
@Test
public void onConnect_suspend_sayHello() {
AtmosphereResource resource = Mockito.mock(AtmosphereResource.class);
Broadcaster broadcaster = Mockito.mock(Broadcaster.class);
Mockito.when(resource.getBroadcaster()).thenReturn(broadcaster);
reload.onConnect(resource);
Assert.assertTrue(reload.isLiveReload(resource));
Mockito.verify(resource).suspend(-1);
Mockito.verify(broadcaster).broadcast("{\"command\": \"hello\"}", resource);
}
use of org.atmosphere.cpr.AtmosphereResource in project flow by vaadin.
the class PushHandler method handleConnectionLost.
private VaadinSession handleConnectionLost(AtmosphereResourceEvent event) {
if (event == null) {
getLogger().error("Could not get event. This should never happen.");
return null;
}
// We don't want to use callWithUi here, as it assumes there's a client
// request active and does requestStart and requestEnd among other
// things.
AtmosphereResource resource = event.getResource();
if (resource == null) {
return null;
}
// In development mode we may have a live-reload push channel
// that should be closed.
Optional<BrowserLiveReload> liveReload = BrowserLiveReloadAccessor.getLiveReloadFromService(service);
if (isDebugWindowConnection(resource) && liveReload.isPresent() && liveReload.get().isLiveReload(resource)) {
liveReload.get().onDisconnect(resource);
return null;
}
VaadinServletRequest vaadinRequest = new VaadinServletRequest(resource.getRequest(), service);
VaadinSession session = null;
try {
session = service.findVaadinSession(vaadinRequest);
} catch (SessionExpiredException e) {
// This happens at least if the server is restarted without
// preserving the session. After restart the client reconnects, gets
// a session expired notification and then closes the connection and
// ends up here
getLogger().debug("Session expired before push disconnect event was received", e);
return session;
}
UI ui;
session.lock();
try {
VaadinSession.setCurrent(session);
// Sets UI.currentInstance
ui = service.findUI(vaadinRequest);
if (ui == null) {
/*
* UI not found, could be because FF has asynchronously closed
* the websocket connection and Atmosphere has already done
* cleanup of the request attributes.
*
* In that case, we still have a chance of finding the right UI
* by iterating through the UIs in the session looking for one
* using the same AtmosphereResource.
*/
ui = findUiUsingResource(resource, session.getUIs());
if (ui == null) {
getLogger().debug("Could not get UI. This should never happen," + " except when reloading in Firefox and Chrome -" + " see http://dev.vaadin.com/ticket/14251.");
return session;
} else {
getLogger().info("No UI was found based on data in the request," + " but a slower lookup based on the AtmosphereResource succeeded." + " See http://dev.vaadin.com/ticket/14251 for more details.");
}
}
PushMode pushMode = ui.getPushConfiguration().getPushMode();
AtmospherePushConnection pushConnection = getConnectionForUI(ui);
String id = resource.uuid();
if (pushConnection == null) {
getLogger().warn("Could not find push connection to close: {} with transport {}", id, resource.transport());
} else {
if (!pushMode.isEnabled()) {
/*
* The client is expected to close the connection after push
* mode has been set to disabled.
*/
getLogger().debug("Connection closed for resource {}", id);
} else {
/*
* Unexpected cancel, e.g. if the user closes the browser
* tab.
*/
getLogger().debug("Connection unexpectedly closed for resource {} with transport {}", id, resource.transport());
}
pushConnection.connectionLost();
}
} catch (final Exception e) {
callErrorHandler(session, e);
} finally {
try {
session.unlock();
} catch (Exception e) {
getLogger().warn("Error while unlocking session", e);
// can't call ErrorHandler, we (hopefully) don't have a lock
}
}
return session;
}
use of org.atmosphere.cpr.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.cpr.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) r).getRequest(false);
String uuid = r.uuid();
if (closeMessage(request)) {
if (config.resourcesFactory() == null || uuid == null) {
logger.debug("Illegal state for uuid {} and AtmosphereResourceFactory {}", r.uuid(), config.resourcesFactory());
return Action.CANCELLED;
}
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;
}
logger.debug("AtmosphereResource {} disconnected", uuid);
// Block websocket closing detection
((AtmosphereResourceEventImpl) ss.getAtmosphereResourceEvent()).isClosedByClient(true);
if (AsynchronousProcessor.class.isAssignableFrom(config.framework().getAsyncSupport().getClass())) {
((AsynchronousProcessor) config.framework().getAsyncSupport()).completeLifecycle(ss, false);
}
return Action.CANCELLED;
}
return Action.CONTINUE;
}
Aggregations