use of com.vaadin.flow.server.DependencyFilter.FilterContext 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.FilterContext 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