Search in sources :

Example 1 with ServiceInitEvent

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

the class TestingServiceInitListener method serviceInit.

@Override
public void serviceInit(ServiceInitEvent event) {
    initCount.incrementAndGet();
    event.addRequestHandler((session, request, response) -> {
        requestCount.incrementAndGet();
        return false;
    });
    event.addDependencyFilter((dependencies, context) -> {
        // used by DependencyFilterUI
        if (dependencies.stream().anyMatch(dependency -> dependency.getUrl().startsWith("replaceme://"))) {
            List<Dependency> newList = new ArrayList<>();
            newList.add(new Dependency(Dependency.Type.HTML_IMPORT, "frontend://com/vaadin/flow/uitest/ui/dependencies/filtered.html", LoadMode.EAGER));
            dependencies.stream().filter(dependency -> !dependency.getUrl().startsWith("replaceme://")).forEach(newList::add);
            dependencies = newList;
        } else // used by BundledTemplateInTemplateWithIdView
        if (dependencies.stream().anyMatch(dependency -> dependency.getUrl().startsWith("bundle://"))) {
            List<Dependency> newList = new ArrayList<>();
            newList.add(new Dependency(Dependency.Type.HTML_IMPORT, "frontend://com/vaadin/flow/uitest/ui/template/BundleIdTemplate.html", LoadMode.EAGER));
            dependencies = newList;
        }
        return dependencies;
    });
}
Also used : ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Dependency(com.vaadin.flow.shared.ui.Dependency) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 2 with ServiceInitEvent

use of com.vaadin.flow.server.ServiceInitEvent 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

ServiceInitEvent (com.vaadin.flow.server.ServiceInitEvent)2 VaadinServiceInitListener (com.vaadin.flow.server.VaadinServiceInitListener)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 ApplicationConstants (com.vaadin.flow.shared.ApplicationConstants)1 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)1 Dependency (com.vaadin.flow.shared.ui.Dependency)1 LoadMode (com.vaadin.flow.shared.ui.LoadMode)1 Json (elemental.json.Json)1 JsonArray (elemental.json.JsonArray)1 JsonObject (elemental.json.JsonObject)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1