From 6976fbd18368c3bb7340515876f6ab9609a543df Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 4 Nov 2020 14:56:31 +0300 Subject: [PATCH] refactoring: App classpath resources should be access via ResourceProvider (#9278) Fixes #9269 --- .../polymertemplate/NpmTemplateParser.java | 24 +++++++++-- .../vaadin/flow/server/BootstrapUtils.java | 43 ++++++++++--------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/NpmTemplateParser.java b/flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/NpmTemplateParser.java index ecbe6de6734..fb129d8746f 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/NpmTemplateParser.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/NpmTemplateParser.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -31,6 +32,8 @@ import org.slf4j.LoggerFactory; import com.vaadin.flow.component.dependency.JsModule; +import com.vaadin.flow.di.Lookup; +import com.vaadin.flow.di.ResourceProvider; import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.internal.AnnotationReader; import com.vaadin.flow.internal.Pair; @@ -106,7 +109,7 @@ public TemplateData getTemplateContent( } String url = dependency.getUrl(); - String source = getSourcesFromTemplate(tag, url); + String source = getSourcesFromTemplate(service, tag, url); if (source == null) { try { source = getSourcesFromStats(service, url); @@ -171,6 +174,8 @@ private boolean dependencyHasTagName(Dependency dependency, String tag) { /** * Finds the JavaScript sources for given tag. * + * @param service + * the related Vaadin service * @param tag * the value of the {@link com.vaadin.flow.component.Tag} * annotation, e.g. `my-component` @@ -182,9 +187,20 @@ private boolean dependencyHasTagName(Dependency dependency, String tag) { * @return the .js source which declares given custom element, or null if no * such source can be found. */ - protected String getSourcesFromTemplate(String tag, String url) { - InputStream content = getClass().getClassLoader() - .getResourceAsStream(url); + protected String getSourcesFromTemplate(VaadinService service, String tag, + String url) { + Lookup lookup = service.getContext().getAttribute(Lookup.class); + ResourceProvider resourceProvider = lookup + .lookup(ResourceProvider.class); + InputStream content = null; + try { + URL appResource = resourceProvider.getApplicationResource(service, + url); + content = appResource == null ? null : appResource.openStream(); + } catch (IOException exception) { + getLogger().warn("Coudln't get resource for the template '{}'", url, + exception); + } if (content != null) { getLogger().debug( "Found sources from the tag '{}' in the template '{}'", tag, diff --git a/flow-server/src/main/java/com/vaadin/flow/server/BootstrapUtils.java b/flow-server/src/main/java/com/vaadin/flow/server/BootstrapUtils.java index 9efe5f3a754..3bbf33d901a 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/BootstrapUtils.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/BootstrapUtils.java @@ -29,6 +29,11 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Parser; + import com.vaadin.flow.component.Component; import com.vaadin.flow.component.HasElement; import com.vaadin.flow.component.UI; @@ -58,10 +63,6 @@ import com.vaadin.flow.shared.ui.LoadMode; import com.vaadin.flow.theme.AbstractTheme; import com.vaadin.flow.theme.ThemeDefinition; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Parser; import elemental.json.Json; import elemental.json.JsonObject; @@ -461,26 +462,26 @@ public static Optional> resolvePageConfigurationHolder(UI ui, // found" target return resolveRouteNotFoundNavigationTarget( ui.getSession().getService().getContext()) - .map(errorNavigationTarget -> { - /* - * {@code resolveTopParentLayout} is theoretically the - * correct way to get the parent layout. But in fact it does - * work for non route targets. - */ - List> layouts = RouteUtil - .getParentLayoutsForNonRouteTarget( - errorNavigationTarget); - if (layouts.isEmpty()) { - return errorNavigationTarget; - } else { - return layouts.get(layouts.size() - 1); - } - }); + .map(errorNavigationTarget -> { + /* + * {@code resolveTopParentLayout} is theoretically + * the correct way to get the parent layout. But in + * fact it does work for non route targets. + */ + List> layouts = RouteUtil + .getParentLayoutsForNonRouteTarget( + errorNavigationTarget); + if (layouts.isEmpty()) { + return errorNavigationTarget; + } else { + return layouts.get(layouts.size() - 1); + } + }); } /* - * NOTE: this code doesn't belong in this class, but is just - * c/p from Router to avoid adding new API to 2.1. + * NOTE: this code doesn't belong in this class, but is just c/p from Router + * to avoid adding new API to 2.1. */ private static Optional> resolveRouteNotFoundNavigationTarget( VaadinContext context) {