/** * Add a route that serves webjars (http://www.webjars.org/) resources. */ default Route addWebjarsResourceRoute(String urlPath) { return addResourceRoute(new WebjarsResourceHandler(urlPath)); }
@Override public URL getResourceUrl(String resourcePath) { if (!isValidResourcePath(resourceBasePath, resourcePath)) { log.warn("Request for '{}' which is not located in '{}'", resourcePath, resourceBasePath); return null; } String resourceName = getResourceBasePath() + "/" + resourcePath; String artifactPath = resourcePath.substring(0, resourcePath.indexOf('/') + 1); if (pathAliases.containsKey(artifactPath)) { String artifactVersion = pathAliases.get(artifactPath); // Do not replace already fixed-version paths. // i.e. skip replacing first path segment of "/jquery/1.11.1/jquery.min.js" // BUT do replace first path segment of "jquery/jquery.min.js". if (!resourcePath.startsWith(artifactVersion)) { String aliasedPath = artifactVersion + resourcePath.substring(artifactPath.length()); log.trace("Replaced Webjar path {} with {}", resourcePath, aliasedPath); resourceName = getResourceBasePath() + "/" + aliasedPath; } } URL url = this.getClass().getClassLoader().getResource(resourceName); if (url == null) { log.warn("Resource '{}' not found", resourceName); } return url; }
public WebjarsResourceHandler(String urlPath) { super(urlPath, "META-INF/resources/webjars"); this.pathAliases = indexWebjars(); }
WebjarsResourceHandler webjars = new WebjarsResourceHandler(); router.addRoute(new Route(HttpMethod.GET, webjars.getUriPattern(), webjars));
for (String pomProperties : locatePomProperties()) { log.debug("Parsing Webjars metadata {}", pomProperties); URL url = ClasspathUtils.locateOnClasspath(pomProperties);
/** * Locate all Webjars Maven pom.properties files on the classpath. * * @return a list of Maven pom.properties files on the classpath */ private List<String> locatePomProperties() { List<String> propertiesFiles = new ArrayList<>(); List<URL> packageUrls = ClasspathUtils.getResources(getResourceBasePath()); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { // We only care about Webjars jar files log.debug("Scanning {}", packageUrl); try { String jar = packageUrl.toString().substring("jar:".length()).split("!")[0]; File file = new File(new URI(jar)); try (JarInputStream is = new JarInputStream(new FileInputStream(file))) { JarEntry entry = null; while ((entry = is.getNextJarEntry()) != null) { if (!entry.isDirectory() && entry.getName().endsWith("pom.properties")) { propertiesFiles.add(entry.getName()); } } } } catch (URISyntaxException | IOException e) { throw new PippoRuntimeException("Failed to get classes for package '{}'", packageUrl); } } } return propertiesFiles; }
WebjarsResourceHandler webjars = new WebjarsResourceHandler(); router.addRoute(new Route(HttpMethod.GET, webjars.getUriPattern(), webjars));
protected RouteRegistration addWebjarsResourceRoute(String basePath) { resourcePaths.add(StringUtils.removeStart(basePath, "/")); return GET(new WebjarsResourceHandler(basePath)); }
protected RouteRegistration addWebjarsResourceRoute(String basePath) { resourcePaths.add(StringUtils.removeStart(basePath, "/")); return GET(new WebjarsResourceHandler(basePath)); }