Search in sources :

Example 6 with DependencyFilter

use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.

the class DefaultTemplateParserTest method defaultParser_useDependencyFilters_returnBundle.

@Test
public void defaultParser_useDependencyFilters_returnBundle() {
    AtomicInteger calls = new AtomicInteger();
    DependencyFilter filter = (list, context) -> {
        list.clear();
        list.add(new Dependency(Type.HTML_IMPORT, "/bundle.html", LoadMode.EAGER));
        calls.incrementAndGet();
        return list;
    };
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.singletonList(filter));
    TemplateParser parser = DefaultTemplateParser.getInstance();
    Element element = parser.getTemplateContent(ImportsInspectTemplate.class, "foo").getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    element = parser.getTemplateContent(RootImportsInspectTemplate.class, "foo").getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    element = parser.getTemplateContent(OtherImportsInspectTemplate.class, "bar").getTemplateElement();
    Assert.assertTrue(element.getElementById("bar") != null);
    Assert.assertEquals("The DependencyFilter should be called exactly 3 times", 3, calls.get());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 7 with DependencyFilter

use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.

the class NpmTemplateParser method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag, VaadinService service) {
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, JsModule.class).stream().map(jsModule -> new Dependency(Dependency.Type.JS_MODULE, jsModule.value(), // load mode doesn't
    LoadMode.EAGER)).collect(Collectors.toList());
    for (DependencyFilter filter : service.getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), service);
    }
    Pair<Dependency, String> chosenDep = null;
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Dependency.Type.JS_MODULE) {
            continue;
        }
        String url = dependency.getUrl();
        String source = getSourcesFromTemplate(service, tag, url);
        if (source == null) {
            continue;
        }
        if (chosenDep == null) {
            chosenDep = new Pair<>(dependency, source);
        }
        if (dependencyHasTagName(dependency, tag)) {
            chosenDep = new Pair<>(dependency, source);
            break;
        }
    }
    if (chosenDep != null) {
        Element templateElement = BundleParser.parseTemplateElement(chosenDep.getFirst().getUrl(), chosenDep.getSecond());
        if (!JsoupUtils.getDomModule(templateElement, null).isPresent()) {
            // Template needs to be wrapped in an element with id, to look
            // like a P2 template
            Element parent = new Element(tag);
            parent.attr("id", tag);
            templateElement.appendTo(parent);
        }
        return new TemplateData(chosenDep.getFirst().getUrl(), templateElement);
    }
    throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using '@%s' annotations. " + "In a Spring Boot project, please ensure that the template's " + "groupId is added to the vaadin.whitelisted-packages " + "property. Otherwise, please 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, JsModule.class.getSimpleName()));
}
Also used : Logger(org.slf4j.Logger) URL(java.net.URL) Pair(com.vaadin.flow.internal.Pair) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Dependency(com.vaadin.flow.shared.ui.Dependency) Collectors(java.util.stream.Collectors) FrontendUtils(com.vaadin.flow.server.frontend.FrontendUtils) ArrayList(java.util.ArrayList) List(java.util.List) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Lookup(com.vaadin.flow.di.Lookup) Constants(com.vaadin.flow.server.Constants) JsModule(com.vaadin.flow.component.dependency.JsModule) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) JsModule(com.vaadin.flow.component.dependency.JsModule) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 8 with DependencyFilter

use of com.vaadin.flow.server.DependencyFilter in project flow by vaadin.

the class UidlWriter method populateDependencies.

private static void populateDependencies(JsonObject response, DependencyList dependencyList, ResolveContext context) {
    Collection<Dependency> pendingSendToClient = dependencyList.getPendingSendToClient();
    for (DependencyFilter filter : context.getService().getDependencyFilters()) {
        pendingSendToClient = filter.filter(new ArrayList<>(pendingSendToClient), context.getService());
    }
    if (!pendingSendToClient.isEmpty()) {
        groupDependenciesByLoadMode(pendingSendToClient, context).forEach((loadMode, dependencies) -> response.put(loadMode.name(), dependencies));
    }
    dependencyList.clearPendingSendToClient();
}
Also used : ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Aggregations

DependencyFilter (com.vaadin.flow.server.DependencyFilter)8 Dependency (com.vaadin.flow.shared.ui.Dependency)8 VaadinService (com.vaadin.flow.server.VaadinService)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Element (org.jsoup.nodes.Element)6 LoadMode (com.vaadin.flow.shared.ui.LoadMode)5 ArrayList (java.util.ArrayList)5 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)4 VaadinServlet (com.vaadin.flow.server.VaadinServlet)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 Type (com.vaadin.flow.shared.ui.Dependency.Type)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Tag (com.vaadin.flow.component.Tag)3 ModelClass (com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass)3 TemplateData (com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData)3 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)3 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 ServiceException (com.vaadin.flow.server.ServiceException)3 VaadinRequest (com.vaadin.flow.server.VaadinRequest)3