Search in sources :

Example 1 with DependencyFilter

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;
}
Also used : MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Matchers.anyString(org.mockito.Matchers.anyString) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) RouterInterface(com.vaadin.flow.router.RouterInterface) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 2 with DependencyFilter

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()));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Comment(org.jsoup.nodes.Comment) Dependency(com.vaadin.flow.shared.ui.Dependency) ArrayList(java.util.ArrayList) Type(com.vaadin.flow.shared.ui.Dependency.Type) Element(org.jsoup.nodes.Element) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext) DependencyFilter(com.vaadin.flow.server.DependencyFilter) ReflectionCache(com.vaadin.flow.internal.ReflectionCache) VaadinSession(com.vaadin.flow.server.VaadinSession) Logger(org.slf4j.Logger) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) VaadinServlet(com.vaadin.flow.server.VaadinServlet) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) List(java.util.List) Document(org.jsoup.nodes.Document) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Jsoup(org.jsoup.Jsoup) InputStream(java.io.InputStream) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) InputStream(java.io.InputStream) Element(org.jsoup.nodes.Element) VaadinServlet(com.vaadin.flow.server.VaadinServlet) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) IOException(java.io.IOException) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext)

Example 3 with DependencyFilter

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");
}
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) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 4 with DependencyFilter

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");
}
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) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Test(org.junit.Test)

Example 5 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, 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();
}
Also used : ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext)

Aggregations

DependencyFilter (com.vaadin.flow.server.DependencyFilter)6 VaadinServlet (com.vaadin.flow.server.VaadinServlet)5 Dependency (com.vaadin.flow.shared.ui.Dependency)5 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)4 VaadinService (com.vaadin.flow.server.VaadinService)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 VaadinServletService (com.vaadin.flow.server.VaadinServletService)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 VaadinUriResolverFactory (com.vaadin.flow.server.VaadinUriResolverFactory)4 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)4 Type (com.vaadin.flow.shared.ui.Dependency.Type)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StandardCharsets (java.nio.charset.StandardCharsets)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 ServletContext (javax.servlet.ServletContext)4 Comment (org.jsoup.nodes.Comment)4 Element (org.jsoup.nodes.Element)4 Node (org.jsoup.nodes.Node)4 Tag (com.vaadin.flow.component.Tag)3