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;
});
}
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);
}
}
Aggregations