Search in sources :

Example 66 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class BrowserLiveReloadAccessorImplTest method getLiveReload_liveReloadDisabled_instanceIsCreated.

@Test
public void getLiveReload_liveReloadDisabled_instanceIsCreated() {
    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(false);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(context.getAttribute(Mockito.eq(BrowserLiveReload.class), Mockito.any())).thenReturn(Mockito.mock(BrowserLiveReload.class));
    Mockito.when(service.getContext()).thenReturn(context);
    Assert.assertNotNull(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 67 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class BrowserLiveReloadAccessorImplTest method getLiveReload_productionMode_nullIsReturned.

@Test
public void getLiveReload_productionMode_nullIsReturned() {
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
    Mockito.when(config.isProductionMode()).thenReturn(true);
    Assert.assertNull(access.getLiveReload(service));
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 68 with VaadinService

use of com.vaadin.flow.server.VaadinService 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 69 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class UidlWriter method createUidl.

/**
 * Creates a JSON object containing all pending changes to the given UI.
 *
 * @param ui
 *            The {@link UI} whose changes to write
 * @param async
 *            True if this message is sent by the server asynchronously,
 *            false if it is a response to a client message
 * @param resync
 *            True iff the client should be asked to resynchronize
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async, boolean resync) {
    JsonObject response = Json.createObject();
    UIInternals uiInternals = ui.getInternals();
    VaadinSession session = ui.getSession();
    VaadinService service = session.getService();
    // Purge pending access calls as they might produce additional changes
    // to write out
    service.runPendingAccessTasks(session);
    // Paints components
    getLogger().debug("* Creating response to client");
    int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
    response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
    if (resync) {
        response.put(ApplicationConstants.RESYNCHRONIZE_ID, true);
    }
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = service.getSystemMessages(ui.getLocale(), null);
    JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
    if (meta.keys().length > 0) {
        response.put("meta", meta);
    }
    JsonArray stateChanges = Json.createArray();
    encodeChanges(ui, stateChanges);
    populateDependencies(response, uiInternals.getDependencyList(), new ResolveContext(service, session.getBrowser()));
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    List<PendingJavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (service.getDeploymentConfiguration().isRequestTiming()) {
        response.put("timings", createPerformanceData(ui));
    }
    uiInternals.incrementServerId();
    return response;
}
Also used : JsonArray(elemental.json.JsonArray) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) SystemMessages(com.vaadin.flow.server.SystemMessages) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation)

Example 70 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class UidlWriter method getInlineResourceStream.

private static InputStream getInlineResourceStream(String url, ResolveContext context) {
    VaadinService service = context.getService();
    InputStream stream = service.getResourceAsStream(url);
    if (stream == null) {
        String resolvedPath = service.resolveResource(url);
        getLogger().warn("The path '{}' for inline resource " + "has been resolved to '{}'. " + "But resource is not available via the servlet context. " + "Trying to load '{}' as a URL", url, resolvedPath, url);
        try {
            stream = new URL(url).openConnection().getInputStream();
        } catch (MalformedURLException exception) {
            throw new IllegalStateException(String.format("The path '%s' is not a valid URL. " + "Unable to fetch a resource addressed by it.", url), exception);
        } catch (IOException e) {
            throw new IllegalStateException(String.format(COULD_NOT_READ_URL_CONTENTS_ERROR_MESSAGE, url), e);
        }
    } else if (getLogger().isDebugEnabled()) {
        String resolvedPath = service.resolveResource(url);
        getLogger().debug("The path '{}' for inline resource has been successfully " + "resolved to resource URL '{}'", url, resolvedPath);
    }
    return stream;
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) VaadinService(com.vaadin.flow.server.VaadinService) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

VaadinService (com.vaadin.flow.server.VaadinService)86 Test (org.junit.Test)39 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)19 VaadinSession (com.vaadin.flow.server.VaadinSession)18 Properties (java.util.Properties)15 VaadinContext (com.vaadin.flow.server.VaadinContext)12 Before (org.junit.Before)11 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)10 UI (com.vaadin.flow.component.UI)9 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)8 Lookup (com.vaadin.flow.di.Lookup)7 VaadinResponse (com.vaadin.flow.server.VaadinResponse)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ServletContext (javax.servlet.ServletContext)6 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 Instantiator (com.vaadin.flow.di.Instantiator)4 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)4 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4