Skip to content

Commit

Permalink
refactoring: App classpath resources should be access via ResourcePro…
Browse files Browse the repository at this point in the history
…vider (#9278)

Fixes #9269
  • Loading branch information
Denis authored Nov 4, 2020
1 parent d6cf4ea commit 19e71d8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,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;
Expand Down Expand Up @@ -66,8 +69,9 @@ public class NpmTemplateParser implements TemplateParser {
private JsonObject jsonStats;

/**
* The default constructor. Protected in order to prevent direct instantiation,
* but not private in order to allow mocking/overrides for testing purposes.
* The default constructor. Protected in order to prevent direct
* instantiation, but not private in order to allow mocking/overrides for
* testing purposes.
*/
protected NpmTemplateParser() {
}
Expand Down Expand Up @@ -104,7 +108,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);
Expand Down Expand Up @@ -162,16 +166,33 @@ private boolean dependencyHasTagName(Dependency dependency, String tag) {
/**
* Finds the JavaScript sources for given tag.
*
* @param tag the value of the {@link com.vaadin.flow.component.Tag} annotation,
* e.g. `my-component`
* @param url the URL resolved according to the {@link com.vaadin.flow.component.dependency.JsModule}
* spec, for example {@code ./view/my-view.js} or {@code @vaadin/vaadin-button.js}.
* @param service
* the related Vaadin service
* @param tag
* the value of the {@link com.vaadin.flow.component.Tag}
* annotation, e.g. `my-component`
* @param url
* the URL resolved according to the
* {@link com.vaadin.flow.component.dependency.JsModule} spec,
* for example {@code ./view/my-view.js} or
* {@code @vaadin/vaadin-button.js}.
* @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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
Expand All @@ -29,6 +30,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;
Expand All @@ -37,6 +43,8 @@
import com.vaadin.flow.component.page.Inline;
import com.vaadin.flow.component.page.Meta;
import com.vaadin.flow.component.page.Viewport;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.internal.ReflectTools;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.Location;
Expand All @@ -58,10 +66,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;
Expand Down Expand Up @@ -301,13 +305,24 @@ static String getDependencyContents(VaadinRequest request, String file) {

private static InputStream getInlineResourceStream(VaadinRequest request,
String file) {
InputStream stream = request.getService().getClassLoader()
.getResourceAsStream(file);
VaadinService service = request.getService();
ResourceProvider resourceProvider = service.getContext()
.getAttribute(Lookup.class).lookup(ResourceProvider.class);
URL appResource = resourceProvider.getApplicationResource(service,
file);

InputStream stream = null;
try {
stream = appResource == null ? null : appResource.openStream();
} catch (IOException e) {
throw new IllegalStateException(String.format(
"Couldn't open application resource '%s' for inline resource",
file), e);
}

if (stream == null) {
throw new IllegalStateException(String.format(
"File '%s' for inline resource is not available through "
+ "the servlet context class loader.",
"Application resource '%s' for inline resource is not available",
file));
}
return stream;
Expand Down Expand Up @@ -462,26 +477,26 @@ public static Optional<Class<?>> 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<Class<? extends RouterLayout>> 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<Class<? extends RouterLayout>> 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<Class<? extends Component>> resolveRouteNotFoundNavigationTarget(
VaadinContext context) {
Expand Down

0 comments on commit 19e71d8

Please sign in to comment.