From af992dbf68c75093d56b12f99ca410ce06fd7ad5 Mon Sep 17 00:00:00 2001 From: Herbert Eiselt Date: Thu, 28 Feb 2019 18:15:10 +0100 Subject: SDN-R helpserver show version info SDN-R helpserver show version info Change-Id: Ifa9d5a8c914f942151594c583c31184eef6f3296 Issue-ID: SDNC-681 Signed-off-by: Herbert Eiselt --- .../features/sdnr/wt/helpserver/HelpServlet.java | 178 +++++++-------------- .../sdnr/wt/helpserver/data/Environment.java | 41 ----- .../wt/helpserver/data/ExtactBundleResource.java | 57 +++---- .../helpserver/data/HelpInfrastructureObject.java | 30 +--- .../src/main/resources/help/sdnr/0.4.0/about.md | 2 +- 5 files changed, 91 insertions(+), 217 deletions(-) delete mode 100644 sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/Environment.java (limited to 'sdnr/wt/helpserver/provider/src/main') diff --git a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/HelpServlet.java b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/HelpServlet.java index 76ac17bb6..35e351563 100644 --- a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/HelpServlet.java +++ b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/HelpServlet.java @@ -22,10 +22,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.net.URISyntaxException; -import java.net.URL; import java.net.URLDecoder; import java.nio.file.Path; import java.util.regex.Matcher; @@ -43,8 +41,6 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { private static Logger LOG = LoggerFactory.getLogger(HelpServlet.class); private static final long serialVersionUID = -4285072760648493461L; - private static final boolean USE_FILESYSTEM = true; - private static final boolean USE_RESSOURCES = !USE_FILESYSTEM; private static final String BASEURI = "/help"; private static final boolean REDIRECT_LINKS = true; @@ -58,21 +54,14 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { } @Override - protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.addHeader("Access-Control-Allow-Origin", "*"); resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE"); resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length"); } - /* - * (non-Javadoc) - * - * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, - * javax.servlet.http.HttpServletResponse) Handle Get Request: if query=?meta=send json - * infrastructure for README.md else if file exist send file - */ @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String query = req.getQueryString(); resp.addHeader("Access-Control-Allow-Origin", "*"); resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, DELETE"); @@ -99,106 +88,70 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { } else { LOG.debug("start walking from path=" + basePath.toAbsolutePath().toString()); HelpInfrastructureObject o = null; - if (USE_FILESYSTEM) { - try { - o = new HelpInfrastructureObject(this.basePath); - } catch (URISyntaxException e) { - LOG.debug("Can not relsolve URI. ", e); - } - } else if (USE_RESSOURCES) { - // o=new HelpInfrastructureObject() + try { + o = new HelpInfrastructureObject(this.basePath); + } catch (URISyntaxException e) { + LOG.debug("Can not relsolve URI. ", e); } resp.getOutputStream().println(o != null ? o.toString() : ""); } resp.setHeader("Content-Type", "application/json"); - } else { + } else { LOG.debug("received get with uri=" + req.getRequestURI()); String uri = URLDecoder.decode(req.getRequestURI().substring(BASEURI.length()), "UTF-8"); if (uri.startsWith("/")) { uri = uri.substring(1); } Path p = basePath.resolve(uri); - if (USE_FILESYSTEM) { - File f = p.toFile(); - if (f.isFile() && f.exists()) { - LOG.debug("found file for request"); - if (this.isTextFile(f)) { - resp.setHeader("Content-Type", "application/text"); - resp.setHeader("charset", "utf-8"); - } else if (this.isImageFile(f)) { - resp.setHeader("Content-Type", "image/*"); - } else if (this.ispdf(f)) { - resp.setHeader("Content-Type", "application/pdf"); - } else { - LOG.debug("file is not allowed to deliver"); - resp.setStatus(404); - return; - } - LOG.debug("delivering file"); - OutputStream out = resp.getOutputStream(); - String version = null; - if (REDIRECT_LINKS) { - version = getVersionFromRequestedUri(uri); - } - if (this.isTextFile(f) && REDIRECT_LINKS && version != null) { - final String regex = - "(!?\\[[^\\]]*?\\])\\(((?:(?!http|www\\.|\\#|\\.com|\\.net|\\.info|\\.org|\\.svg|\\.png|\\.jpg|\\.gif|\\.jpeg|\\.pdf).)*?)\\)"; - final Pattern pattern = Pattern.compile(regex); - Matcher matcher; - String line; - try (BufferedReader br = new BufferedReader(new FileReader(f))) { - line = br.readLine(); - while (line != null) { - // check line for internal link - matcher = pattern.matcher(line); - if (matcher.find()) { - // extend link with specific version - line = line.replace(matcher.group(2), - "../" + matcher.group(2) + version + "/README.md"); - } - out.write((line + "\n").getBytes()); - line = br.readLine(); - + File f = p.toFile(); + if (f.isFile() && f.exists()) { + LOG.debug("found file for request"); + if (this.isTextFile(f)) { + resp.setHeader("Content-Type", "application/text"); + resp.setHeader("charset", "utf-8"); + } else if (this.isImageFile(f)) { + resp.setHeader("Content-Type", "image/*"); + } else if (this.ispdf(f)) { + resp.setHeader("Content-Type", "application/pdf"); + } else { + LOG.debug("file is not allowed to deliver"); + resp.setStatus(404); + return; + } + LOG.debug("delivering file"); + OutputStream out = resp.getOutputStream(); + String version = null; + if (REDIRECT_LINKS) { + version = getVersionFromRequestedUri(uri); + } + if (this.isTextFile(f) && REDIRECT_LINKS && version != null) { + final String regex = + "(!?\\[[^\\]]*?\\])\\(((?:(?!http|www\\.|\\#|\\.com|\\.net|\\.info|\\.org|\\.svg|\\.png|\\.jpg|\\.gif|\\.jpeg|\\.pdf).)*?)\\)"; + final Pattern pattern = Pattern.compile(regex); + Matcher matcher; + String line; + try (BufferedReader br = new BufferedReader(new FileReader(f))) { + line = br.readLine(); + while (line != null) { + // check line for internal link + matcher = pattern.matcher(line); + if (matcher.find()) { + // extend link with specific version + line = line.replace(matcher.group(2), + "../" + matcher.group(2) + version + "/README.md"); } - out.flush(); - out.close(); - br.close(); - } - - } else { - try (FileInputStream in = new FileInputStream(f)) { + out.write((line + "\n").getBytes()); + line = br.readLine(); - byte[] buffer = new byte[1024]; - int len; - while ((len = in.read(buffer)) != -1) { - out.write(buffer, 0, len); - } - in.close(); - out.flush(); - out.close(); } + out.flush(); + out.close(); + br.close(); } + } else { - LOG.debug("found not file for request"); - resp.setStatus(404); - } - } else if (USE_RESSOURCES) { - URL resurl = this.getClass().getResource(p.toString()); - if (resurl != null)// resource file found - { - if (this.isTextFile(resurl)) { - resp.setHeader("Content-Type", "application/text"); - resp.setHeader("charset", "utf-8"); - } else if (this.isImageFile(resurl)) { - resp.setHeader("Content-Type", "image/*"); - } else if (this.ispdf(resurl)) { - resp.setHeader("Content-Type", "application/pdf"); - } else { - resp.setStatus(404); - return; - } - try (InputStream in = this.getClass().getResourceAsStream(p.toString())) { - OutputStream out = resp.getOutputStream(); + try (FileInputStream in = new FileInputStream(f)) { + byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { @@ -208,18 +161,18 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { out.flush(); out.close(); } - - } else // resource file not found - { - resp.setStatus(404); } + } else { + LOG.debug("found not file for request"); + resp.setStatus(404); } } } - /* - * - * uri = "help/folder1/folder2/version/README.md" + /** + * Extract version from URI string + * @param uri = "help/folder1/folder2/version/README.md" + * @return version as a string */ private static String getVersionFromRequestedUri(String uri) { if (uri == null) { @@ -237,18 +190,6 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { } - private boolean isTextFile(URL url) { - return url != null ? this.isTextFile(url.toString()) : false; - } - - private boolean ispdf(URL url) { - return url != null ? this.ispdf(url.toString()) : false; - } - - private boolean isImageFile(URL url) { - return url != null ? this.isImageFile(url.toString()) : false; - } - private boolean ispdf(File f) { return f != null ? this.ispdf(f.getName()) : false; } @@ -283,9 +224,6 @@ public class HelpServlet extends HttpServlet implements AutoCloseable { : false; } - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {} - @Override public void close() throws Exception {} } diff --git a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/Environment.java b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/Environment.java deleted file mode 100644 index bbbccdb10..000000000 --- a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/Environment.java +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START======================================================================== - * ONAP : ccsdk feature sdnr wt - * ================================================================================================= - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. - * ================================================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * ============LICENSE_END========================================================================== - ******************************************************************************/ -package org.onap.ccsdk.features.sdnr.wt.helpserver.data; - -import java.net.Inet4Address; -import java.net.UnknownHostException; -import java.util.Map; - -public class Environment { - - public static String getVar(String v) - { - if(v.equals("$HOSTNAME")) - try { - return Inet4Address.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - - } - Map env = System.getenv(); - for (String envName : env.keySet()) { - if(envName!=null && envName.equals(v)) - return env.get(envName); - } - return null; - } -} diff --git a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/ExtactBundleResource.java b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/ExtactBundleResource.java index ac99849dc..975f8985b 100644 --- a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/ExtactBundleResource.java +++ b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/ExtactBundleResource.java @@ -23,39 +23,43 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; - import org.osgi.framework.Bundle; /** * Extract subtree with resources from Opendaylight/Karaf/OSGi bundle into Karaf directory
* - * Reference: Eclipsezone @see https://www.eclipszone.com - *

- * Example for resource and directory path from karaf log. - * write resource: help/FAQ/0.4.0/README.md - * Create directories for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md - * Open the file: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md - * Problem: Binary, JPG files => do not use buffer related functions + * Reference: Eclipsezone @see + * https://www.eclipszone.com + *
+ *
+ * Example for resource and directory path from karaf log. write resource: help/FAQ/0.4.0/README.md + * Create directories for: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Open + * the file: data/cache/com.highstreet.technologies.help/help/FAQ/0.4.0/README.md Problem: Binary, + * JPG files => do not use buffer related functions * - * Hint: Werify with file manager the content of the bundle.jar file to see the location of the resources. - * There is no need to mark them via the classpath. + * Hint: Verify with file manager the content of the bundle.jar file to see the location of the + * resources. There is no need to mark them via the classpath. */ public class ExtactBundleResource { /** * Extract resources from Karaf/OSGi bundle into karaf directory structure. + * * @param bundle Karaf/OSGi bundle with resources - * @param filePrefix prefix in karaf file system for destination e.g. "data/cache/com.highstreet.technologies." + * @param filePrefix prefix in karaf file system for destination e.g. + * "data/cache/com.highstreet.technologies." * @param ressoureRoot root name of ressources, with leading "/". e.g. "/help" * @throws IOException In case of problems. */ - public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot) throws IOException { - copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot)); + public static void copyBundleResoucesRecursively(Bundle bundle, String filePrefix, String ressoureRoot) + throws IOException { + copyResourceTreeRecurse(bundle, filePrefix, bundle.getEntryPaths(ressoureRoot)); } /** * Delete a file or a directory and its children. + * * @param file The directory to delete. * @throws IOException Exception when problem occurs during deleting the directory. */ @@ -63,18 +67,13 @@ public class ExtactBundleResource { if (file.isDirectory()) { for (File childFile : file.listFiles()) { - if (childFile.isDirectory()) { - deleteRecursively(childFile); - } else { - if (!childFile.delete()) { - throw new IOException(); - } - } + deleteRecursively(childFile); } } - - if (!file.delete()) { - throw new IOException(); + if (file.exists()) { + if (!file.delete()) { + throw new IOException("No file " + file.getName()); + } } } @@ -82,24 +81,26 @@ public class ExtactBundleResource { /** * Recurse function to steps through the resource element tree + * * @param b Bundle index for bundle with resourcs * @param filePrefix * @param resource * @throws IOException */ - private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration resource) throws IOException { + private static void copyResourceTreeRecurse(Bundle b, String filePrefix, Enumeration resource) + throws IOException { while (resource.hasMoreElements()) { String name = resource.nextElement(); Enumeration list = b.getEntryPaths(name); if (list != null) { copyResourceTreeRecurse(b, filePrefix, list); } else { - //Read - File targetFile = new File(filePrefix+name); + // Read + File targetFile = new File(filePrefix + name); targetFile.getParentFile().mkdirs(); - try(InputStream in = b.getEntry(name).openStream(); - OutputStream outStream = new FileOutputStream(targetFile);) { + try (InputStream in = b.getEntry(name).openStream(); + OutputStream outStream = new FileOutputStream(targetFile);) { int theInt; while ((theInt = in.read()) >= 0) { diff --git a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/HelpInfrastructureObject.java b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/HelpInfrastructureObject.java index 11042d13a..2c79645f1 100644 --- a/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/HelpInfrastructureObject.java +++ b/sdnr/wt/helpserver/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/helpserver/data/HelpInfrastructureObject.java @@ -103,8 +103,8 @@ public class HelpInfrastructureObject extends JSONObject { } - public HelpInfrastructureObject(Path proot) throws URISyntaxException { - File root = proot.toFile(); + public HelpInfrastructureObject(Path pRoot) throws URISyntaxException { + File root = pRoot.toFile(); File[] list = root.listFiles(); if (list == null) { return; @@ -113,35 +113,11 @@ public class HelpInfrastructureObject extends JSONObject { if (f.isDirectory()) { ArrayList versions = findReadmeVersionFolders(root.toPath(), f.toPath(), true); if (versions != null && versions.size() > 0) { - NodeObject o = new NodeObject(proot, f, f.getName(), versions); + NodeObject o = new NodeObject(pRoot, f, f.getName(), versions); this.put(o.getString("label").toLowerCase(), o); } } } - - - } - - public static void walk(ArrayList results, String path) { - - File root = new File(path); - File[] list = root.listFiles(); - - if (list == null) { - return; - } - - for (File f : list) { - if (f.isDirectory()) { - walk(results, f.getAbsolutePath()); - // System.out.println( "Dir:" + f.getAbsoluteFile() ); - } else { - // System.out.println( "File:" + f.getAbsoluteFile() ); - if (f.isFile() && f.getName().endsWith(".md")) { - results.add(f); - } - } - } } private static ArrayList findReadmeVersionFolders(Path base, Path root, boolean appendCurrent) { diff --git a/sdnr/wt/helpserver/provider/src/main/resources/help/sdnr/0.4.0/about.md b/sdnr/wt/helpserver/provider/src/main/resources/help/sdnr/0.4.0/about.md index 856d69aa5..31fd5fef8 100644 --- a/sdnr/wt/helpserver/provider/src/main/resources/help/sdnr/0.4.0/about.md +++ b/sdnr/wt/helpserver/provider/src/main/resources/help/sdnr/0.4.0/about.md @@ -1 +1 @@ -Version: @buildtime@ \ No newline at end of file +ONAP SDN-R | ONF Wireless for ${distversion} - Build: ${buildtime} (${buildno}) \ No newline at end of file -- cgit 1.2.3-korg