private void discoverJarResources(Path path, Service s, Map<Path, String> pathToURIPath, Path baseUriPath, String prefix) throws URISyntaxException, IOException { for (ResourceEntry entry : FileUtils.findResources(s.getClass(), prefix)) { Path resourcePath = path.resolve(entry.suffix); Path uriPath = baseUriPath.resolve(entry.suffix); Path outputPath = getHost().copyResourceToSandbox(entry.url, resourcePath); if (outputPath == null) { // Failed to copy one resource, disable user interface for this service. s.toggleOption(ServiceOption.HTML_USER_INTERFACE, false); } else { pathToURIPath.put(outputPath, uriPath.toString().replace('\\', '/')); } } }
private void discoverFileResources(Service s, Map<Path, String> pathToURIPath, Path baseUriPath, String prefix) { File rootDir = new File(new File(getHost().getState().resourceSandboxFileReference), prefix); if (!rootDir.exists()) { log(Level.INFO, "Resource directory not found: %s", rootDir.toString()); return; } String basePath = baseUriPath.toString(); String serviceName = s.getClass().getSimpleName(); List<File> resources = FileUtils.findFiles(rootDir.toPath(), new HashSet<String>(), false); for (File f : resources) { String subPath = f.getAbsolutePath(); subPath = subPath.substring(subPath.indexOf(serviceName)); subPath = subPath.replace(serviceName, ""); Path uriPath = Paths.get(basePath, subPath); pathToURIPath.put(f.toPath(), uriPath.toString().replace('\\', '/')); } if (pathToURIPath.isEmpty()) { log(Level.INFO, "No resources found in directory: %s", rootDir.toString()); } }
protected Map<Path, String> discoverUiResources(Path path, Service s) throws Throwable { Map<Path, String> pathToURIPath = new HashMap<>(); Path baseUriPath = Paths.get(getSelfLink()); String prefix = path.toString().replace('\\', '/'); if (getHost().getState().resourceSandboxFileReference != null) { discoverFileResources(s, pathToURIPath, baseUriPath, prefix); } if (pathToURIPath.isEmpty()) { discoverJarResources(path, s, pathToURIPath, baseUriPath, prefix); } return pathToURIPath; }
protected void startUiFileContentServices() throws Throwable { Map<Path, String> pathToURIPath = new HashMap<>(); Path baseResourcePath = Utils.getServiceUiResourcePath(this); try { pathToURIPath = discoverUiResources(baseResourcePath, this); } catch (Throwable e) { log(Level.WARNING, "Error enumerating UI resources for %s: %s", this.getSelfLink(), Utils.toString(e)); } if (pathToURIPath.isEmpty()) { log(Level.WARNING, "No custom UI resources found for %s", this.getClass().getName()); return; } for (Entry<Path, String> e : pathToURIPath.entrySet()) { String value = e.getValue(); if (value.contains("/META-INF/")) { continue; } Operation post = Operation .createPost(UriUtils.buildUri(getHost(), value)); RestrictiveFileContentService fcs = new RestrictiveFileContentService( e.getKey().toFile()); getHost().startService(post, fcs); } }