Search in sources :

Example 6 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class DefaultDeploymentConfiguration method getFrontendDirectoryPath.

private Optional<String> getFrontendDirectoryPath() {
    VaadinUriResolver uriResolver = new VaadinUriResolver() {

        @Override
        protected String getContextRootUrl() {
            // ServletContext.getResource expects a leading slash
            return CONTEXT_ROOT_PATH;
        }

        @Override
        protected String getFrontendRootUrl() {
            return getEs6FrontendPrefix();
        }
    };
    String scanBase = uriResolver.resolveVaadinUri(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
    if (!scanBase.startsWith(CONTEXT_ROOT_PATH)) {
        String message = formatDefaultPolyfillMessage(String.format("Cannot automatically find the %s polyfill because the property " + "'%s' value is not absolute (doesn't start with '/')", WEB_COMPONENTS_LOADER_JS_NAME, Constants.FRONTEND_URL_ES6));
        getLogger().warn(message);
        return Optional.empty();
    }
    return Optional.of(scanBase);
}
Also used : VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 7 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class BundleFilterInitializer method serviceInit.

@Override
public void serviceInit(ServiceInitEvent event) {
    DeploymentConfiguration deploymentConfiguration = event.getSource().getDeploymentConfiguration();
    if (deploymentConfiguration.isProductionMode()) {
        VaadinUriResolver es6ContextPathResolver = new VaadinUriResolver() {

            @Override
            protected String getContextRootUrl() {
                return "/";
            }

            @Override
            protected String getFrontendRootUrl() {
                return deploymentConfiguration.getEs6FrontendPrefix();
            }
        };
        Map<String, Set<String>> importsInBundles = readBundleDependencies(event, es6ContextPathResolver);
        if (!importsInBundles.isEmpty()) {
            if (importsInBundles.values().stream().noneMatch(importSet -> importSet.contains(BundleDependencyFilter.MAIN_BUNDLE_URL))) {
                throw new IllegalArgumentException(String.format("Attempted to initialize BundleDependencyFilter with an " + "import to bundle mapping which does not contain the main bundle %s", BundleDependencyFilter.MAIN_BUNDLE_URL));
            }
            event.addDependencyFilter(new BundleDependencyFilter(importsInBundles));
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 8 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class VaadinUriResolverTest method testProtocolChain.

@Test
public void testProtocolChain() {
    browser = Mockito.mock(WebBrowser.class);
    resolver = new VaadinUriResolver() {

        @Override
        protected String getFrontendRootUrl() {
            if (browser.isEs6Supported()) {
                return "context://es6/";
            }
            return "context://es5/";
        }

        @Override
        protected String getContextRootUrl() {
            return "http://someplace/";
        }
    };
    Mockito.when(browser.isEs6Supported()).thenReturn(true);
    assertEquals("http://someplace/es6/my-component.html", resolver.resolveVaadinUri("frontend://my-component.html"));
    Mockito.when(browser.isEs6Supported()).thenReturn(false);
    assertEquals("http://someplace/es5/my-component.html", resolver.resolveVaadinUri("frontend://my-component.html"));
}
Also used : WebBrowser(com.vaadin.flow.server.WebBrowser) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Test(org.junit.Test)

Example 9 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class VaadinUriResolverFactory method toServletContextPath.

/**
 * Resolves the {@code path} to the path which can be used by a
 * {@link javax.servlet.ServletContext} to get a resource content.
 * <p>
 * The factory method {@link #getUriResolver(VaadinRequest)} is used to get
 * URI resolver which resolves the {@code path}. This path works on the
 * client side considering <code>&lt;base href&gt;</code> attribute value
 * (the class {@link VaadinUriResolver} is a shared class between client and
 * server, so it cannot contain any server-side specific logic). But it
 * requires additional logic on the server side to be able to use it with
 * the {@link javax.servlet.ServletContext#getResource(String)} or
 * {@link javax.servlet.ServletContext#getResourceAsStream(String)} methods. This logic is
 * implemented in this method so it can be reused on the server-side in
 * various places.
 *
 * @see #getUriResolver(VaadinRequest)
 *
 * @param request
 *            a request as a context to get a VaadinUriResolver
 * @param path
 *            a resource path to resolve
 * @return resolved path which can be used by ServletContext to get a
 *         content
 */
default String toServletContextPath(VaadinRequest request, String path) {
    VaadinUriResolver uriResolver = getUriResolver(request);
    assert uriResolver != null;
    String uri = getUriResolver(request).resolveVaadinUri(path);
    assert uri != null;
    if (request instanceof VaadinServletRequest) {
        VaadinServletRequest servletRequest = (VaadinServletRequest) request;
        String servletPath = servletRequest.getServletPath();
        assert servletPath != null;
        if (!servletPath.endsWith("/") && !uri.startsWith("/")) {
            servletPath += "/";
        } else if (servletPath.endsWith("/") && uri.startsWith("/")) {
            servletPath = servletPath.substring(0, servletPath.length() - 1);
        }
        // and the pathinfo doesn't matter for the resolved path
        if (uri.contains("../")) {
            return servletPath + uri;
        }
    }
    return uri.startsWith("/") ? uri : "/" + uri;
}
Also used : VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 10 with VaadinUriResolver

use of com.vaadin.flow.shared.VaadinUriResolver in project flow by vaadin.

the class BundleFilterInitializer method readBundleDependencies.

private Map<String, Set<String>> readBundleDependencies(ServiceInitEvent event, VaadinUriResolver es6ContextPathResolver) {
    VaadinServletService servlet = ((VaadinServletService) event.getSource());
    String es6Base = es6ContextPathResolver.resolveVaadinUri(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
    if (!es6Base.endsWith("/")) {
        es6Base += '/';
    }
    String bundleManifestContextPath = es6Base + FLOW_BUNDLE_MANIFEST;
    try (InputStream bundleManifestStream = servlet.getResourceAsStream(bundleManifestContextPath)) {
        if (bundleManifestStream == null) {
            getLogger().info("Bundling disabled: Flow bundle manifest '{}' was not found in servlet context", bundleManifestContextPath);
            return Collections.emptyMap();
        }
        JsonObject bundlesToUrlsContained = Json.parse(IOUtils.toString(bundleManifestStream, StandardCharsets.UTF_8));
        Map<String, Set<String>> importToBundle = new HashMap<>();
        for (String bundlePath : bundlesToUrlsContained.keys()) {
            JsonArray bundledFiles = bundlesToUrlsContained.getArray(bundlePath);
            for (int i = 0; i < bundledFiles.length(); i++) {
                String bundledFile = bundledFiles.getString(i);
                if (servlet.getResource(es6ContextPathResolver.resolveVaadinUri(es6Base + bundlePath)) == null) {
                    throw new IllegalArgumentException(String.format("Failed to find bundle at context path '%s', specified in manifest '%s'. " + "Remove file reference from the manifest to disable bundle usage or add the bundle to the context path specified.", bundlePath, bundleManifestContextPath));
                }
                importToBundle.computeIfAbsent(bundledFile, key -> new HashSet<>()).add(bundlePath);
            }
        }
        return importToBundle;
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Failed to read bundle manifest file at context path '%s'", bundleManifestContextPath), e);
    }
}
Also used : ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Json(elemental.json.Json) Set(java.util.Set) JsonArray(elemental.json.JsonArray) IOException(java.io.IOException) HashMap(java.util.HashMap) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) StandardCharsets(java.nio.charset.StandardCharsets) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) IOUtils(org.apache.commons.io.IOUtils) Map(java.util.Map) JsonObject(elemental.json.JsonObject) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) InputStream(java.io.InputStream) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) InputStream(java.io.InputStream) VaadinServletService(com.vaadin.flow.server.VaadinServletService) JsonObject(elemental.json.JsonObject) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) JsonArray(elemental.json.JsonArray) HashSet(java.util.HashSet)

Aggregations

VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)10 Test (org.junit.Test)4 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Set (java.util.Set)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)2 ApplicationConstants (com.vaadin.flow.shared.ApplicationConstants)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Component (com.vaadin.flow.component.Component)1 Html (com.vaadin.flow.component.Html)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1 UI (com.vaadin.flow.component.UI)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 BodySize (com.vaadin.flow.component.page.BodySize)1