Search in sources :

Example 16 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class TemplateModelWithEncodersTest method createService.

@Override
protected VaadinService createService() {
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    return service;
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 17 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class ThemeUrlResolverTest method init.

@Before
public void init() throws Exception {
    assert VaadinSession.getCurrent() == null : "Required no current vaadin session.";
    assert VaadinRequest.getCurrent() == null : "Required no current vaadin request.";
    MockitoAnnotations.initMocks(this);
    servlet = new VaadinServlet() {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration() throws ServletException {
            return mockDeploymentConfiguration;
        }
    };
    Properties initParameters = new Properties();
    Mockito.when(servletConfig.getServletContext()).thenReturn(servletContext);
    Mockito.when(servletConfig.getInitParameterNames()).thenReturn((Enumeration<String>) initParameters.propertyNames());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn((Enumeration<String>) initParameters.propertyNames());
    Mockito.when(servletContext.getResource(Mockito.anyString())).thenAnswer(i -> new URL("http://localhost" + i.getArguments()[0]));
    servlet.init(servletConfig);
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(uriResolverFactory);
    Mockito.when(uriResolverFactory.getUriResolver(Mockito.any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(Mockito.anyString())).thenAnswer(i -> i.getArguments()[0]);
    VaadinSession.setCurrent(session);
    CurrentInstance.set(VaadinRequest.class, request);
}
Also used : ServletException(javax.servlet.ServletException) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Properties(java.util.Properties) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) URL(java.net.URL) Before(org.junit.Before)

Example 18 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class CustomElementRegistryInitializerTest method createService.

@Override
protected VaadinService createService() {
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    return service;
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 19 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration 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 20 with DeploymentConfiguration

use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.

the class VaadinService method init.

/**
 * Initializes this service. The service should be initialized before it is
 * used.
 *
 * @since 7.1
 * @throws ServiceException
 *             if a problem occurs when creating the service
 */
public void init() throws ServiceException {
    instantiator = createInstantiator();
    List<RequestHandler> handlers = createRequestHandlers();
    ServiceInitEvent event = new ServiceInitEvent(this);
    instantiator.getServiceInitListeners().forEach(listener -> listener.serviceInit(event));
    event.getAddedRequestHandlers().forEach(handlers::add);
    Collections.reverse(handlers);
    requestHandlers = Collections.unmodifiableCollection(handlers);
    dependencyFilters = instantiator.getDependencyFilters(event.getAddedDependencyFilters()).collect(Collectors.toList());
    bootstrapListeners = instantiator.getBootstrapListeners(event.getAddedBootstrapListeners()).collect(Collectors.toList());
    DeploymentConfiguration deploymentConf = getDeploymentConfiguration();
    if (deploymentConf.isUsingNewRouting()) {
        router = new Router(getRouteRegistry());
    } else {
        router = new com.vaadin.flow.router.legacy.Router();
        String routerConfiguratorClassName = deploymentConf.getRouterConfiguratorClassName();
        if (routerConfiguratorClassName != null && !RouterConfigurator.class.getName().equals(routerConfiguratorClassName)) {
            // Configure router if we have a non-default configurator type
            configureRouter(routerConfiguratorClassName);
        }
    }
    initialized = true;
}
Also used : StreamRequestHandler(com.vaadin.flow.server.communication.StreamRequestHandler) UidlRequestHandler(com.vaadin.flow.server.communication.UidlRequestHandler) SessionRequestHandler(com.vaadin.flow.server.communication.SessionRequestHandler) Router(com.vaadin.flow.router.Router) RouterConfigurator(com.vaadin.flow.router.legacy.RouterConfigurator) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Aggregations

DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)25 VaadinService (com.vaadin.flow.server.VaadinService)8 Test (org.junit.Test)7 URL (java.net.URL)6 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 Before (org.junit.Before)4 VaadinServlet (com.vaadin.flow.server.VaadinServlet)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 Properties (java.util.Properties)3 ServletConfig (javax.servlet.ServletConfig)3 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UI (com.vaadin.flow.component.UI)2 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)2 AbstractDeploymentConfiguration (com.vaadin.flow.server.AbstractDeploymentConfiguration)2 ServiceException (com.vaadin.flow.server.ServiceException)2 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)2 Conf (com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf)2