Search in sources :

Example 1 with VaadinServlet

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

the class ServletHelperTest method setup.

@Before
public void setup() throws ServletException {
    servlet = new VaadinServlet();
    servlet.init(new MockServletConfig());
}
Also used : VaadinServlet(com.vaadin.flow.server.VaadinServlet) Before(org.junit.Before)

Example 2 with VaadinServlet

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

the class VaadinServiceTest method createService.

private static VaadinService createService() {
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    try {
        servlet.init(servletConfig);
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    VaadinService service = servlet.getService();
    return service;
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinService(com.vaadin.flow.server.VaadinService)

Example 3 with VaadinServlet

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

the class WebJarServerTest method init.

@Before
public void init() throws Exception {
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.getDevelopmentFrontendPrefix()).thenReturn(Constants.FRONTEND_URL_DEV_DEFAULT);
    VaadinServletService servletService = Mockito.mock(VaadinServletService.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return servletService;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(servletService.getDeploymentConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.areWebJarsEnabled()).thenReturn(true);
    servlet.init(Mockito.mock(ServletConfig.class));
    Field webJarServerField = VaadinServlet.class.getDeclaredField("webJarServer");
    webJarServerField.setAccessible(true);
    webJarServer = (WebJarServer) webJarServerField.get(servlet);
    // webJarServer = new WebJarServer(configuration);
    // context = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getResource(GRID_WEBJAR)).thenReturn(new URL("http://localhost:8080" + GRID_DEPENDENCY));
}
Also used : ServletException(javax.servlet.ServletException) Field(java.lang.reflect.Field) ServiceException(com.vaadin.flow.server.ServiceException) VaadinServlet(com.vaadin.flow.server.VaadinServlet) ServletConfig(javax.servlet.ServletConfig) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) URL(java.net.URL) Before(org.junit.Before)

Example 4 with VaadinServlet

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()));
}
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 5 with VaadinServlet

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

the class WebpackHandlerTest method vaadinServlet_forDifferentRequests_shouldHaveCorrectResponse.

@Test
public void vaadinServlet_forDifferentRequests_shouldHaveCorrectResponse() throws Exception {
    HttpServletRequest request = prepareRequest("/VAADIN/foo.js");
    HttpServletResponse response = prepareResponse();
    final String globalResponse = "{ \"VAADIN/foo.js\": " + "\"VAADIN/foo.js\" }";
    int port = prepareHttpServer(0, HTTP_OK, globalResponse);
    VaadinServlet servlet = prepareServlet(port);
    servlet.service(request, response);
    assertEquals(HTTP_OK, responseStatus);
    httpServer.stop(0);
    prepareHttpServer(port, HTTP_NOT_MODIFIED, "");
    servlet.service(request, response);
    assertEquals(HTTP_NOT_MODIFIED, responseStatus);
    httpServer.stop(0);
    exception.expect(ConnectException.class);
    servlet.service(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractDevModeTest(com.vaadin.base.devserver.startup.AbstractDevModeTest) Test(org.junit.Test)

Aggregations

VaadinServlet (com.vaadin.flow.server.VaadinServlet)17 Test (org.junit.Test)8 VaadinService (com.vaadin.flow.server.VaadinService)5 VaadinServletService (com.vaadin.flow.server.VaadinServletService)5 ServletException (javax.servlet.ServletException)5 Before (org.junit.Before)5 VaadinSession (com.vaadin.flow.server.VaadinSession)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Properties (java.util.Properties)4 DependencyFilter (com.vaadin.flow.server.DependencyFilter)3 Dependency (com.vaadin.flow.shared.ui.Dependency)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 ServletConfig (javax.servlet.ServletConfig)3 ServletContext (javax.servlet.ServletContext)3