use of com.vaadin.flow.server.VaadinServlet in project flow by vaadin.
the class DefaultTemplateParser method getTemplateContent.
@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag) {
VaadinServlet servlet = VaadinServlet.getCurrent();
boolean logEnabled = LOG_CACHE.get(clazz).compareAndSet(false, true);
List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, HtmlImport.class).stream().map(htmlImport -> new Dependency(Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode())).collect(Collectors.toList());
FilterContext filterContext = new FilterContext(VaadinSession.getCurrent());
for (DependencyFilter filter : VaadinService.getCurrent().getDependencyFilters()) {
dependencies = filter.filter(new ArrayList<>(dependencies), filterContext);
}
for (Dependency dependency : dependencies) {
if (dependency.getType() != Type.HTML_IMPORT) {
continue;
}
String url = dependency.getUrl();
String path = servlet.resolveResource(url);
if (logEnabled) {
getLogger().debug("Html import path '{}' is resolved to '{}'", url, path);
}
try (InputStream content = servlet.getResourceAsStream(path)) {
if (content == null) {
throw new IllegalStateException(String.format("Can't find resource '%s' " + "via the servlet context", url));
}
Element templateElement = parseHtmlImport(content, url, tag);
if (logEnabled && templateElement != null) {
getLogger().debug("Found a template file containing template " + "definition for the tag '{}' by the path '{}'", tag, url);
}
if (templateElement != null) {
return new TemplateData(url, templateElement);
}
} catch (IOException exception) {
// ignore exception on close()
if (logEnabled) {
getLogger().warn("Couldn't close template input stream", exception);
}
}
}
throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using @'%s' annotations. " + "Check the availability of the template files in your WAR " + "file or provide alternative implementation of the " + "method getTemplateContent() which should return an element " + "representing the content of the template file", tag, HtmlImport.class.getSimpleName()));
}
use of com.vaadin.flow.server.VaadinServlet in project flow by vaadin.
the class HtmlDependencyParser method parseDependencies.
private void parseDependencies(String path, Set<String> dependencies) {
if (dependencies.contains(path)) {
return;
}
dependencies.add(path);
VaadinSession session = VaadinSession.getCurrent();
VaadinServlet servlet = VaadinServlet.getCurrent();
if (servlet == null || session == null) {
/*
* Cannot happen in runtime.
*
* But not all unit tests set it. Let's just don't proceed further.
*/
return;
}
assert session.hasLock();
HtmlDependenciesCache cache = session.getAttribute(HtmlDependenciesCache.class);
if (cache == null) {
cache = new HtmlDependenciesCache();
session.setAttribute(HtmlDependenciesCache.class, cache);
}
String resolvedPath = servlet.resolveResource(path);
if (cache.hasDependency(resolvedPath)) {
return;
}
cache.addDependency(resolvedPath);
try (InputStream content = servlet.getResourceAsStream(resolvedPath)) {
if (content == null) {
getLogger().trace("Can't find resource '{}' to parse for imports via the servlet context", path);
} else {
parseHtmlImports(content, resolvedPath).map(uri -> resolveUri(uri, path)).forEach(uri -> parseDependencies(uri, dependencies));
}
} catch (IOException exception) {
// ignore exception on close()
getLogger().debug("Couldn't close template input stream", exception);
}
}
use of com.vaadin.flow.server.VaadinServlet 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);
}
use of com.vaadin.flow.server.VaadinServlet in project flow by vaadin.
the class BundleFilterInitializerTest method init.
@Before
public void init() throws MalformedURLException {
event = Mockito.mock(ServiceInitEvent.class);
VaadinServletService service = Mockito.mock(VaadinServletService.class);
VaadinServlet servlet = Mockito.mock(VaadinServlet.class);
Mockito.when(event.getSource()).thenReturn(service);
Mockito.when(service.getServlet()).thenReturn(servlet);
Mockito.when(service.getDeploymentConfiguration()).thenReturn(new DefaultDeploymentConfiguration(BundleFilterInitializerTest.class, new Properties(), (base, consumer) -> {
}) {
@Override
public boolean isProductionMode() {
return true;
}
});
Mockito.doAnswer(invocation -> {
dependencyFilterAddHandler.accept(invocation.getArgumentAt(0, DependencyFilter.class));
return null;
}).when(event).addDependencyFilter(Mockito.any(DependencyFilter.class));
Mockito.doAnswer(invocation -> {
return inputStreamProducer.apply(invocation.getArgumentAt(0, String.class));
}).when(service).getResourceAsStream("/frontend-es6/vaadin-flow-bundle-manifest.json");
Mockito.doAnswer(invocation -> {
return resourceProducer.apply(invocation.getArgumentAt(0, String.class));
}).when(service).getResource(Mockito.anyString());
}
use of com.vaadin.flow.server.VaadinServlet in project flow by vaadin.
the class VaadinServiceTest method serviceContainsStreamRequestHandler.
@Test
public void serviceContainsStreamRequestHandler() throws ServiceException, ServletException {
ServletConfig servletConfig = new MockServletConfig();
VaadinServlet servlet = new VaadinServlet();
servlet.init(servletConfig);
VaadinService service = servlet.getService();
Assert.assertTrue(service.createRequestHandlers().stream().filter(StreamRequestHandler.class::isInstance).findAny().isPresent());
}
Aggregations