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));
}
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));
}
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();
}
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;
}
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();
}
Aggregations