use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.
the class UidlWriterTest method initializeUIForDependenciesTest.
private UI initializeUIForDependenciesTest(UI ui) {
ServletContext context = Mockito.mock(ServletContext.class);
VaadinServletService service = new VaadinServletService(new VaadinServlet() {
@Override
public ServletContext getServletContext() {
return context;
}
}, new MockDeploymentConfiguration()) {
RouterInterface router = new com.vaadin.flow.router.legacy.Router();
@Override
public RouterInterface getRouter() {
return router;
}
@Override
public Iterable<DependencyFilter> getDependencyFilters() {
return Collections.emptyList();
}
};
service.getRouter().reconfigure(conf -> {
conf.setRoute("", BaseClass.class);
conf.setParentView(BaseClass.class, ParentClass.class);
conf.setParentView(ParentClass.class, SuperParentClass.class);
});
MockVaadinSession session = new MockVaadinSession(service);
session.lock();
ui.getInternals().setSession(session);
when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
ui.doInit(vaadinRequestMock, 1);
factory = mock(VaadinUriResolverFactory.class);
VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
Mockito.when(factory.getUriResolver(any())).thenReturn(vaadinUriResolver);
Mockito.when(vaadinUriResolver.resolveVaadinUri(anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(any(), anyString());
session.setAttribute(VaadinUriResolverFactory.class, factory);
VaadinSession.setCurrent(session);
return ui;
}
use of com.vaadin.flow.server.DependencyFilter 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.DependencyFilter in project flow by vaadin.
the class DefaultTemplateParserTest method defaultParser_useDependencyFilters_noHtmlDependencies_throws.
@Test(expected = IllegalStateException.class)
public void defaultParser_useDependencyFilters_noHtmlDependencies_throws() {
DependencyFilter filter = (list, context) -> {
list.clear();
list.add(new Dependency(Type.STYLESHEET, "something.css", LoadMode.EAGER));
list.add(new Dependency(Type.JAVASCRIPT, "something.js", LoadMode.EAGER));
return list;
};
Mockito.when(service.getDependencyFilters()).thenReturn(Collections.singletonList(filter));
DefaultTemplateParser.getInstance().getTemplateContent(ImportsInspectTemplate.class, "foo");
}
use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.
the class DefaultTemplateParserTest method defaultParser_useDependencyFilters_noDependencies_throws.
@Test(expected = IllegalStateException.class)
public void defaultParser_useDependencyFilters_noDependencies_throws() {
DependencyFilter filter = (list, context) -> {
list.clear();
return list;
};
Mockito.when(service.getDependencyFilters()).thenReturn(Collections.singletonList(filter));
DefaultTemplateParser.getInstance().getTemplateContent(ImportsInspectTemplate.class, "foo");
}
use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.
the class UidlWriter method populateDependencies.
private static void populateDependencies(JsonObject response, VaadinSession session, DependencyList dependencyList) {
Collection<Dependency> pendingSendToClient = dependencyList.getPendingSendToClient();
FilterContext context = new FilterContext(session);
for (DependencyFilter filter : session.getService().getDependencyFilters()) {
pendingSendToClient = filter.filter(new ArrayList<>(pendingSendToClient), context);
}
if (!pendingSendToClient.isEmpty()) {
groupDependenciesByLoadMode(pendingSendToClient).forEach((loadMode, dependencies) -> response.put(loadMode.name(), dependencies));
}
dependencyList.clearPendingSendToClient();
}
Aggregations