Search in sources :

Example 1 with SpringServlet

use of com.vaadin.flow.spring.SpringServlet in project flow by vaadin.

the class RequestUtilTest method testAnonymousRouteRequest_rootMappedServlet_notAView.

@Test
public void testAnonymousRouteRequest_rootMappedServlet_notAView() {
    Mockito.when(vaadinConfigurationProperties.getUrlMapping()).thenReturn("/*");
    SpringServlet servlet = setupMockServlet();
    addRoute(servlet, PublicRootView.class);
    addRoute(servlet, AnotherPublicView.class);
    MockHttpServletRequest request = createRequest(null);
    request.setServletPath("/bar");
    Assert.assertFalse(requestUtil.isAnonymousRoute(request));
    request = createRequest("other");
    request.setServletPath("/bar");
    Assert.assertFalse(requestUtil.isAnonymousRoute(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SpringServlet(com.vaadin.flow.spring.SpringServlet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with SpringServlet

use of com.vaadin.flow.spring.SpringServlet in project flow by vaadin.

the class RequestUtilTest method testAnonymousRouteRequest_fooMappedServlet_notAView.

@Test
public void testAnonymousRouteRequest_fooMappedServlet_notAView() {
    Mockito.when(vaadinConfigurationProperties.getUrlMapping()).thenReturn("/foo/*");
    SpringServlet servlet = setupMockServlet();
    addRoute(servlet, PublicRootView.class);
    addRoute(servlet, AnotherPublicView.class);
    MockHttpServletRequest request = createRequest(null);
    request.setServletPath("/foo/bar");
    Assert.assertFalse(requestUtil.isAnonymousRoute(request));
    request = createRequest("other");
    request.setServletPath("/foo/bar");
    Assert.assertFalse(requestUtil.isAnonymousRoute(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SpringServlet(com.vaadin.flow.spring.SpringServlet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with SpringServlet

use of com.vaadin.flow.spring.SpringServlet in project flow by vaadin.

the class SpringInstantiatorTest method getService.

public static VaadinService getService(ApplicationContext context, Properties configProperties, boolean rootMapping) throws ServletException {
    SpringServlet servlet = new SpringServlet(context, rootMapping) {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
            if (configProperties != null) {
                configProperties.putAll(initParameters);
                return super.createDeploymentConfiguration(configProperties);
            }
            return super.createDeploymentConfiguration(initParameters);
        }
    };
    ServletConfig config = Mockito.mock(ServletConfig.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servlet.getClass().getClassLoader());
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.getContext()).thenReturn(new VaadinServletContext(servletContext));
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    StaticFileHandlerFactory staticFileHandlerFactory = vaadinService -> new StaticFileServer((VaadinServletService) vaadinService);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(staticFileHandlerFactory);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    Mockito.when(config.getServletContext()).thenReturn(servletContext);
    Mockito.when(config.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    servlet.init(config);
    return servlet.getService();
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) ServletException(javax.servlet.ServletException) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Div(com.vaadin.flow.component.html.Div) StaticFileServer(com.vaadin.flow.server.StaticFileServer) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Scope(org.springframework.context.annotation.Scope) Locale(java.util.Locale) Lookup(com.vaadin.flow.di.Lookup) SpringServlet(com.vaadin.flow.spring.SpringServlet) SpringRunner(org.springframework.test.context.junit4.SpringRunner) I18NProvider(com.vaadin.flow.i18n.I18NProvider) ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) Instantiator(com.vaadin.flow.di.Instantiator) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Set(java.util.Set) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ComponentScan(org.springframework.context.annotation.ComponentScan) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) Configuration(org.springframework.context.annotation.Configuration) Mockito(org.mockito.Mockito) List(java.util.List) Component(org.springframework.stereotype.Component) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) VaadinService(com.vaadin.flow.server.VaadinService) ServletContext(javax.servlet.ServletContext) Assert(org.junit.Assert) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) ServletConfig(javax.servlet.ServletConfig) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Example 4 with SpringServlet

use of com.vaadin.flow.spring.SpringServlet in project flow by vaadin.

the class RequestUtil method isAnonymousRoute.

/**
 * Checks whether the request targets a Flow route that is public, i.e.
 * marked as @{@link AnonymousAllowed}.
 *
 * @param request
 *            the servlet request
 * @return {@code true} if the request is targeting an anonymous route,
 *         {@code false} otherwise
 */
public boolean isAnonymousRoute(HttpServletRequest request) {
    String vaadinMapping = configurationProperties.getUrlMapping();
    String requestedPath = HandlerHelper.getRequestPathInsideContext(request);
    Optional<String> maybePath = HandlerHelper.getPathIfInsideServlet(vaadinMapping, requestedPath);
    if (!maybePath.isPresent()) {
        return false;
    }
    String path = maybePath.get();
    if (path.startsWith("/")) {
        // Requested path includes a beginning "/" but route mapping is done
        // without one
        path = path.substring(1);
    }
    SpringServlet servlet = springServletRegistration.getServlet();
    VaadinService service = servlet.getService();
    Router router = service.getRouter();
    RouteRegistry routeRegistry = router.getRegistry();
    NavigationRouteTarget target = routeRegistry.getNavigationRouteTarget(path);
    if (target == null) {
        return false;
    }
    RouteTarget routeTarget = target.getRouteTarget();
    if (routeTarget == null) {
        return false;
    }
    Class<? extends com.vaadin.flow.component.Component> targetView = routeTarget.getTarget();
    if (targetView == null) {
        return false;
    }
    // Check if a not authenticated user can access the view
    boolean result = accessAnnotationChecker.hasAccess(targetView, null, role -> false);
    if (result) {
        getLogger().debug(path + " refers to a public view");
    }
    return result;
}
Also used : RouteRegistry(com.vaadin.flow.server.RouteRegistry) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) VaadinService(com.vaadin.flow.server.VaadinService) Router(com.vaadin.flow.router.Router) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) RouteTarget(com.vaadin.flow.router.internal.RouteTarget) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Example 5 with SpringServlet

use of com.vaadin.flow.spring.SpringServlet in project flow by vaadin.

the class SpringInstantiatorTest method getService.

public static VaadinServletService getService(ApplicationContext context, Properties configProperties) throws ServletException {
    SpringServlet servlet = new SpringServlet(context) {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
            if (configProperties != null) {
                configProperties.putAll(initParameters);
                return super.createDeploymentConfiguration(configProperties);
            }
            return super.createDeploymentConfiguration(initParameters);
        }
    };
    ServletConfig config = Mockito.mock(ServletConfig.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(config.getServletContext()).thenReturn(servletContext);
    Mockito.when(config.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    servlet.init(config);
    return servlet.getService();
}
Also used : ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Properties(java.util.Properties) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Aggregations

SpringServlet (com.vaadin.flow.spring.SpringServlet)9 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 Router (com.vaadin.flow.router.Router)2 RouteRegistry (com.vaadin.flow.server.RouteRegistry)2 VaadinService (com.vaadin.flow.server.VaadinService)2 Properties (java.util.Properties)2 ServletConfig (javax.servlet.ServletConfig)2 ServletContext (javax.servlet.ServletContext)2 Div (com.vaadin.flow.component.html.Div)1 Instantiator (com.vaadin.flow.di.Instantiator)1 Lookup (com.vaadin.flow.di.Lookup)1 ResourceProvider (com.vaadin.flow.di.ResourceProvider)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 I18NProvider (com.vaadin.flow.i18n.I18NProvider)1 NavigationRouteTarget (com.vaadin.flow.router.internal.NavigationRouteTarget)1 RouteTarget (com.vaadin.flow.router.internal.RouteTarget)1 ServiceInitEvent (com.vaadin.flow.server.ServiceInitEvent)1 StaticFileHandlerFactory (com.vaadin.flow.server.StaticFileHandlerFactory)1