Search in sources :

Example 1 with Attributes

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

the class ComponentUtil method setData.

private static <T, U> void setData(Component component, SerializableTriConsumer<Attributes, T, U> setter, T key, U value) {
    Attributes attributes = component.attributes;
    if (attributes == null) {
        if (value == null) {
            return;
        }
        attributes = new Attributes();
        component.attributes = attributes;
    }
    setter.accept(attributes, key, value);
    if (attributes.isEmpty()) {
        component.attributes = null;
    }
}
Also used : Attributes(com.vaadin.flow.server.Attributes)

Example 2 with Attributes

use of com.vaadin.flow.server.Attributes 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.getLockInstance().lock();
                /*
                     * Read attribute using reflection to bypass
                     * VaadinSesison.getAttribute which would cause an infinite
                     * loop when checking the production mode setting for
                     * determining whether to check that the session is locked.
                     */
                Field attributesField = VaadinSession.class.getDeclaredField("attributes");
                attributesField.setAccessible(true);
                Attributes sessionAttributes = (Attributes) attributesField.get(session);
                configuration = (DeploymentConfiguration) sessionAttributes.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(ApplicationConfiguration.get(getService().getContext()), servlet.getClass(), initParameters);
                    } else {
                        configuration = originalConfiguration;
                    }
                    sessionAttributes.setAttribute(name, configuration);
                }
            } finally {
                session.getLockInstance().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) Attributes(com.vaadin.flow.server.Attributes) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) HttpServletRequest(javax.servlet.http.HttpServletRequest) Field(java.lang.reflect.Field) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration)

Aggregations

Attributes (com.vaadin.flow.server.Attributes)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 Conf (com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf)1 Field (java.lang.reflect.Field)1 Properties (java.util.Properties)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1