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);
}
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));
}
}
}
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"));
}
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><base href></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;
}
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);
}
}
Aggregations