Search in sources :

Example 1 with BrowserLiveReload

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;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) Lookup(com.vaadin.flow.di.Lookup) BrowserLiveReloadAccessor(com.vaadin.flow.internal.BrowserLiveReloadAccessor)

Example 2 with BrowserLiveReload

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));
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinService(com.vaadin.flow.server.VaadinService) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 3 with BrowserLiveReload

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());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Example 4 with BrowserLiveReload

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);
    }
}
Also used : Element(org.jsoup.nodes.Element) VaadinService(com.vaadin.flow.server.VaadinService) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload)

Example 5 with BrowserLiveReload

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;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) InvalidUIDLSecurityKeyException(com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException) JsonException(elemental.json.JsonException) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) IOException(java.io.IOException) PushMode(com.vaadin.flow.shared.communication.PushMode)

Aggregations

BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)6 VaadinContext (com.vaadin.flow.server.VaadinContext)3 VaadinService (com.vaadin.flow.server.VaadinService)2 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)2 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 Lookup (com.vaadin.flow.di.Lookup)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 BrowserLiveReloadAccess (com.vaadin.flow.internal.BrowserLiveReloadAccess)1 BrowserLiveReloadAccessor (com.vaadin.flow.internal.BrowserLiveReloadAccessor)1 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)1 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)1 PushMode (com.vaadin.flow.shared.communication.PushMode)1 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)1 JsonException (elemental.json.JsonException)1 IOException (java.io.IOException)1