Search in sources :

Example 21 with VaadinResponse

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

the class WebComponentBootstrapHandlerTest method getMockResponse.

private VaadinResponse getMockResponse(ByteArrayOutputStream stream) throws IOException {
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    Mockito.when(response.getService()).thenReturn(service);
    Mockito.when(service.getContext()).thenReturn(context);
    Mockito.when(context.getAttribute(eq(WebComponentConfigurationRegistry.class), any())).thenReturn(Mockito.mock(WebComponentConfigurationRegistry.class));
    return response;
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinContext(com.vaadin.flow.server.VaadinContext) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) VaadinService(com.vaadin.flow.server.VaadinService)

Example 22 with VaadinResponse

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noExportChunk.

@Test
public void writeBootstrapPage_noExportChunk() throws IOException, ServiceException {
    TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
    VaadinServletService service = new MockVaadinServletService();
    initLookup(service);
    VaadinSession session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(service.getDeploymentConfiguration());
    MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    config.setApplicationOrSystemProperty(SERVLET_PARAMETER_STATISTICS_JSON, VAADIN_SERVLET_RESOURCES + "config/stats_no_export.json");
    config.setEnableDevServer(false);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(request.getServletPath()).thenReturn("/");
    VaadinResponse response = getMockResponse(null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.synchronizedHandleRequest(session, request, response);
    // no "export" chunk, expect "bundle" in result instead
    String result = stream.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(result.contains("VAADIN/build/vaadin-bundle-1111.cache.js"));
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 23 with VaadinResponse

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

the class WebComponentProvider method synchronizedHandleRequest.

@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String pathInfo = request.getPathInfo();
    final ComponentInfo componentInfo = new ComponentInfo(pathInfo);
    if (!componentInfo.hasExtension()) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request without extension " + "information (.js/.html) with request path {}", pathInfo);
        return false;
    }
    if (componentInfo.getTag() == null) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for non-custom element with request path {}", pathInfo);
        return false;
    }
    if (componentInfo.isHTML()) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for html component in npm" + " mode with request path {}", pathInfo);
        return false;
    }
    WebComponentConfigurationRegistry registry = WebComponentConfigurationRegistry.getInstance(request.getService().getContext());
    Optional<WebComponentConfiguration<? extends Component>> optionalWebComponentConfiguration = registry.getConfiguration(componentInfo.tag);
    if (optionalWebComponentConfiguration.isPresent()) {
        WebComponentConfiguration<? extends Component> webComponentConfiguration = optionalWebComponentConfiguration.get();
        String generated;
        Supplier<String> responder;
        response.setContentType(CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8);
        responder = () -> generateNPMResponse(webComponentConfiguration.getTag(), request, response);
        if (cache == null) {
            generated = responder.get();
        } else {
            generated = cache.computeIfAbsent(componentInfo.tag, moduleTag -> responder.get());
        }
        IOUtils.write(generated, response.getOutputStream(), StandardCharsets.UTF_8);
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "No web component for " + Optional.ofNullable(componentInfo.tag).orElse("<null>"));
    }
    return true;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Component(com.vaadin.flow.component.Component) VaadinResponse(com.vaadin.flow.server.VaadinResponse) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8(com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8) IOException(java.io.IOException) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Supplier(java.util.function.Supplier) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) IOUtils(org.apache.commons.io.IOUtils) Matcher(java.util.regex.Matcher) WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SynchronizedRequestHandler(com.vaadin.flow.server.SynchronizedRequestHandler) WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) Component(com.vaadin.flow.component.Component)

Example 24 with VaadinResponse

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

the class CsrfIndexHtmlRequestListener method ensureCsrfTokenCookieIsSet.

private void ensureCsrfTokenCookieIsSet(VaadinRequest request, VaadinResponse response) {
    if (isSpringCsrfTokenPresent(request)) {
        return;
    }
    final String csrfCookieValue = Optional.ofNullable(request.getCookies()).map(Arrays::stream).orElse(Stream.empty()).filter(cookie -> cookie.getName().equals(ApplicationConstants.CSRF_TOKEN)).findFirst().map(Cookie::getValue).orElse(null);
    if (csrfCookieValue != null && !csrfCookieValue.isEmpty()) {
        return;
    }
    /*
         * Despite section 6 of RFC 4122, this particular use of UUID *is*
         * adequate for security capabilities. Type 4 UUIDs contain 122 bits of
         * random data, and UUID.randomUUID() is defined to use a
         * cryptographically secure random generator.
         */
    final String csrfToken = UUID.randomUUID().toString();
    Cookie csrfCookie = new Cookie(ApplicationConstants.CSRF_TOKEN, csrfToken);
    csrfCookie.setSecure(request.isSecure());
    String path = request.getContextPath();
    if (path == null || path.isEmpty()) {
        path = "/";
    }
    csrfCookie.setPath(path);
    csrfCookie.setHttpOnly(false);
    response.addCookie(csrfCookie);
}
Also used : IndexHtmlRequestListener(com.vaadin.flow.server.communication.IndexHtmlRequestListener) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) VaadinResponse(com.vaadin.flow.server.VaadinResponse) IndexHtmlResponse(com.vaadin.flow.server.communication.IndexHtmlResponse) Optional(java.util.Optional) VaadinRequest(com.vaadin.flow.server.VaadinRequest) UUID(java.util.UUID) Cookie(javax.servlet.http.Cookie) SpringCsrfTokenUtil(com.vaadin.flow.internal.springcsrf.SpringCsrfTokenUtil) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Cookie(javax.servlet.http.Cookie)

Aggregations

VaadinResponse (com.vaadin.flow.server.VaadinResponse)24 Test (org.junit.Test)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 VaadinRequest (com.vaadin.flow.server.VaadinRequest)8 VaadinSession (com.vaadin.flow.server.VaadinSession)8 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)7 Document (org.jsoup.nodes.Document)7 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinService (com.vaadin.flow.server.VaadinService)6 Element (org.jsoup.nodes.Element)6 UI (com.vaadin.flow.component.UI)3 Location (com.vaadin.flow.router.Location)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Optional (java.util.Optional)3 AbstractDevModeTest (com.vaadin.base.devserver.startup.AbstractDevModeTest)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)2