Search in sources :

Example 16 with VaadinSession

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

the class SerializationTest method testVaadinSession.

public void testVaadinSession() throws Exception {
    VaadinSession session = new VaadinSession(null);
    session = serializeAndDeserialize(session);
    assertNotNull("Pending access queue was not recreated after deserialization", session.getPendingAccessQueue());
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession)

Example 17 with VaadinSession

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

the class CustomElementRegistryInitializerTest method setup.

@Before
public void setup() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Field customElements = CustomElementRegistry.class.getDeclaredField("customElements");
    customElements.setAccessible(true);
    customElements.set(CustomElementRegistry.getInstance(), new AtomicReference<>());
    customElementRegistryInitializer = new CustomElementRegistryInitializer();
    VaadinSession session = Mockito.mock(VaadinSession.class);
    UI ui = new UI() {

        @Override
        public VaadinSession getSession() {
            return session;
        }
    };
    VaadinService service = Mockito.mock(VaadinService.class);
    when(session.getService()).thenReturn(service);
    DefaultInstantiator instantiator = new DefaultInstantiator(service);
    when(service.getInstantiator()).thenReturn(instantiator);
    UI.setCurrent(ui);
}
Also used : DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) Field(java.lang.reflect.Field) VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) Before(org.junit.Before)

Example 18 with VaadinSession

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

the class ApplicationRunnerServlet method findDeploymentConfiguration.

private DeploymentConfiguration findDeploymentConfiguration(DeploymentConfiguration originalConfiguration) throws Exception {
    // First level of cache
    DeploymentConfiguration configuration = CurrentInstance.get(DeploymentConfiguration.class);
    if (configuration == null) {
        // Not in cache, try to find a VaadinSession to get it from
        VaadinSession session = VaadinSession.getCurrent();
        if (session == null) {
            /*
                 * There's no current session, request or response when serving
                 * static resources, but there's still the current request
                 * maintained by ApplicationRunnerServlet, and there's most
                 * likely also a HttpSession containing a VaadinSession for that
                 * request.
                 */
            HttpServletRequest currentRequest = VaadinServletService.getCurrentServletRequest();
            if (currentRequest != null) {
                HttpSession httpSession = currentRequest.getSession(false);
                if (httpSession != null) {
                    Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance.setCurrent((VaadinSession) null);
                    try {
                        VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
                        session = service.findVaadinSession(new VaadinServletRequest(currentRequest, service));
                    } finally {
                        /*
                             * Clear some state set by findVaadinSession to
                             * avoid accidentally depending on it when coding on
                             * e.g. static request handling.
                             */
                        CurrentInstance.restoreInstances(oldCurrent);
                        currentRequest.removeAttribute(VaadinSession.class.getName());
                    }
                }
            }
        }
        if (session != null) {
            String name = ApplicationRunnerServlet.class.getName() + ".deploymentConfiguration";
            try {
                session.lock();
                configuration = (DeploymentConfiguration) session.getAttribute(name);
                if (configuration == null) {
                    ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet.getCurrent();
                    Class<?> classToRun;
                    try {
                        classToRun = servlet.getClassToRun();
                    } catch (ClassNotFoundException e) {
                        /*
                             * This happens e.g. if the UI class defined in the
                             * URL is not found or if this servlet just serves
                             * static resources while there's some other servlet
                             * that serves the UI (e.g. when using /run-push/).
                             */
                        return originalConfiguration;
                    }
                    CustomDeploymentConfiguration customDeploymentConfiguration = classToRun.getAnnotation(CustomDeploymentConfiguration.class);
                    if (customDeploymentConfiguration != null) {
                        Properties initParameters = new Properties(originalConfiguration.getInitParameters());
                        for (Conf entry : customDeploymentConfiguration.value()) {
                            initParameters.put(entry.name(), entry.value());
                        }
                        initParameters.put(VaadinSession.UI_PARAMETER, getApplicationRunnerApplicationClassName(request.get()));
                        configuration = new DefaultDeploymentConfiguration(servlet.getClass(), initParameters, this::scanForResources);
                    } else {
                        configuration = originalConfiguration;
                    }
                    session.setAttribute(name, configuration);
                }
            } finally {
                session.unlock();
            }
            CurrentInstance.set(DeploymentConfiguration.class, configuration);
        } else {
            configuration = originalConfiguration;
        }
    }
    return configuration;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) HttpServletRequest(javax.servlet.http.HttpServletRequest) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration)

Example 19 with VaadinSession

use of com.vaadin.flow.server.VaadinSession 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.
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async) {
    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);
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = ui.getSession().getService().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();
    JsonObject templates = Json.createObject();
    encodeChanges(ui, stateChanges, templates);
    populateDependencies(response, session, uiInternals.getDependencyList());
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    if (templates.keys().length > 0) {
        response.put("templates", templates);
    }
    List<JavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (ui.getSession().getService().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) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Example 20 with VaadinSession

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

the class CustomUIClassLoaderTest method testWithDefaultClassLoader.

/**
 * Tests that a UI class can be loaded even if no classloader has been
 * provided.
 *
 * @throws Exception
 *             if thrown
 */
public void testWithDefaultClassLoader() throws Exception {
    VaadinSession application = createStubApplication();
    application.setConfiguration(createConfigurationMock());
    Class<? extends UI> uiClass = BootstrapHandler.getUIClass(createRequestMock(getClass().getClassLoader()));
    assertEquals(MyUI.class, uiClass);
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession)

Aggregations

VaadinSession (com.vaadin.flow.server.VaadinSession)26 UI (com.vaadin.flow.component.UI)9 VaadinService (com.vaadin.flow.server.VaadinService)9 Before (org.junit.Before)6 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 SpringVaadinSession (com.vaadin.flow.spring.SpringVaadinSession)4 Test (org.junit.Test)4 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)2 SystemMessages (com.vaadin.flow.server.SystemMessages)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 VaadinServletService (com.vaadin.flow.server.VaadinServletService)2 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)2 PushMode (com.vaadin.flow.shared.communication.PushMode)2 JsonException (elemental.json.JsonException)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Field (java.lang.reflect.Field)2