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); } }
@Override public void handleStart(Operation startPost) { try { startUiFileContentServices(); super.handleStart(startPost); } catch (Throwable e) { startPost.fail(e); } }
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()); } }
@Override public void handleGet(Operation get) { URI uri = get.getUri(); String selfLink = getSelfLink(); String requestUri = uri.getPath(); String uiResourcePath = selfLinkWithTrailing + indexFileName; Operation operation = get.clone(); operation.setUri(UriUtils.buildUri(getHost(), uiResourcePath, uri.getQuery())) .setCompletion((o, e) -> { ((getHost().getSecurePort() > 0) ? "; Secure" : "")); getHost().sendRequest(operation);
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('\\', '/')); } } }
@Override public void handleGet(Operation get) { if (isEmbedded == null) { // ConfigurationUtil.getConfigProperty(this, ConfigurationUtil.EMBEDDED_MODE_PROPERTY, // (embedded) -> { // isEmbedded = Boolean.valueOf(embedded); // handleGet(get); // }); // return; // TODO - the code above should be used instead but for some reason the UI components // may get initialized before the configuration service is fully initialized. isEmbedded = ConfigurationUtil.isEmbedded(); } if (!isEmbedded) { // Avoid clickjacking attacks, by ensuring that content is not embedded. get.addResponseHeader(ConfigurationUtil.UI_FRAME_OPTIONS_HEADER, "DENY"); } if (redirectToLoginOrIndex(getHost(), get)) { return; } super.handleGet(get); }
@Override public void handleGet(Operation get) { if (redirectToLoginOrIndex(getHost(), get)) { return;
@Override public void handleStart(Operation startPost) { super.handleStart(startPost); startForwardingService(UriUtils.buildUriPath(SELF_LINK, "assets/i18n/base"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "messages/admiral")); startForwardingService(UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH, "assets/i18n/base"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "messages/admiral")); startForwardingService(UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH, "assets/i18n"), UriUtils.buildUriPath(SELF_LINK, "assets/i18n")); String loginServicePath = UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH); if (!loginServicePath.endsWith(UriUtils.URI_PATH_CHAR)) { loginServicePath += UriUtils.URI_PATH_CHAR; } String uiServicePath = ManagementUriParts.UI_SERVICE; if (!uiServicePath.endsWith(UriUtils.URI_PATH_CHAR)) { uiServicePath += UriUtils.URI_PATH_CHAR; } startForwardingService(loginServicePath, uiServicePath); // TODO: remove after new UI is active in CS startRedirectService(UriUtils.buildUriPath(SELF_LINK, "/index-embedded.html"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "/index-embedded.html")); }