use of com.vaadin.flow.internal.BrowserLiveReload in project flow by vaadin.
the class PushHandlerTest method mockBrowserLiveReloadImpl.
public static BrowserLiveReload mockBrowserLiveReloadImpl(VaadinContext context) {
BrowserLiveReload liveReload = Mockito.mock(BrowserLiveReload.class);
Lookup lookup = Lookup.of(new BrowserLiveReloadAccessor() {
@Override
public BrowserLiveReload getLiveReload(VaadinContext context) {
return liveReload;
}
}, BrowserLiveReloadAccessor.class);
context.setAttribute(Lookup.class, lookup);
return liveReload;
}
use of com.vaadin.flow.internal.BrowserLiveReload in project flow by vaadin.
the class BrowserLiveReloadAccessorImplTest method getLiveReload_devMode_contextHasReloadInstance_instanceIsReturned.
@Test
public void getLiveReload_devMode_contextHasReloadInstance_instanceIsReturned() {
VaadinService service = Mockito.mock(VaadinService.class);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
Mockito.when(config.isProductionMode()).thenReturn(false);
Mockito.when(config.isDevModeLiveReloadEnabled()).thenReturn(true);
VaadinContext context = Mockito.mock(VaadinContext.class);
Mockito.when(service.getContext()).thenReturn(context);
BrowserLiveReload reload = Mockito.mock(BrowserLiveReload.class);
Mockito.when(context.getAttribute(Mockito.eq(BrowserLiveReload.class), Mockito.any())).thenReturn(reload);
Assert.assertSame(reload, access.getLiveReload(service));
}
use of com.vaadin.flow.internal.BrowserLiveReload in project flow by vaadin.
the class PushHandlerTest method onConnect_devMode_websocket_refreshConnection_onConnectIsCalled_callWithUIIsNotCalled.
@Test
public void onConnect_devMode_websocket_refreshConnection_onConnectIsCalled_callWithUIIsNotCalled() throws ServiceException {
MockVaadinServletService service = Mockito.spy(MockVaadinServletService.class);
MockDeploymentConfiguration deploymentConfiguration = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
deploymentConfiguration.setProductionMode(false);
deploymentConfiguration.setDevModeLiveReloadEnabled(true);
deploymentConfiguration.setDevModeGizmoEnabled(true);
ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
Mockito.when(applicationConfiguration.isProductionMode()).thenReturn(false);
VaadinContext context = service.getContext();
context.setAttribute(ApplicationConfiguration.class, applicationConfiguration);
BrowserLiveReload liveReload = mockBrowserLiveReloadImpl(context);
AtomicReference<AtmosphereResource> res = new AtomicReference<>();
runTest(service, (handler, resource) -> {
AtmosphereRequest request = resource.getRequest();
Mockito.when(request.getParameter(ApplicationConstants.DEBUG_WINDOW_CONNECTION)).thenReturn("");
Mockito.when(resource.transport()).thenReturn(TRANSPORT.WEBSOCKET);
handler.onConnect(resource);
res.set(resource);
});
Mockito.verify(service, Mockito.times(0)).requestStart(Mockito.any(), Mockito.any());
Mockito.verify(liveReload).onConnect(res.get());
}
use of com.vaadin.flow.internal.BrowserLiveReload in project flow by vaadin.
the class IndexHtmlRequestHandler method addDevmodeGizmo.
private void addDevmodeGizmo(Document indexDocument, DeploymentConfiguration config, VaadinSession session, VaadinRequest request) {
VaadinService service = session.getService();
Optional<BrowserLiveReload> liveReload = BrowserLiveReloadAccessor.getLiveReloadFromService(service);
if (liveReload.isPresent()) {
Element devmodeGizmo = new Element("vaadin-devmode-gizmo");
if (!config.isDevModeLiveReloadEnabled()) {
devmodeGizmo.attr("liveReloadDisabled", "");
}
devmodeGizmo.attr("url", BootstrapHandlerHelper.getPushURL(session, request));
BrowserLiveReload.Backend backend = liveReload.get().getBackend();
if (backend != null) {
devmodeGizmo.attr("backend", backend.toString());
}
devmodeGizmo.attr("springbootlivereloadport", Integer.toString(Constants.SPRING_BOOT_DEFAULT_LIVE_RELOAD_PORT));
indexDocument.body().appendChild(devmodeGizmo);
}
}
use of com.vaadin.flow.internal.BrowserLiveReload 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;
}
Aggregations