From 627badaf69987c01811c477219fd943757a635f5 Mon Sep 17 00:00:00 2001 From: "Christopher Lott (Christopher) (cl778h)" Date: Mon, 12 Jun 2017 09:49:00 -0400 Subject: [PORTAL-16 PORTAL-18] Widget ms; staging Remove staging repositories from poms. Add widget microservice code base. Add portal unit tests. Repair defects. Normalize line endings. Change-Id: Ia5e48da2a3141b352439ecd548cddf918f4df585 Signed-off-by: Christopher Lott (cl778h) --- .../portalapp/widget/utils/AuthorizationUtil.java | 19 ++++ .../portalapp/widget/utils/UnzipUtil.java | 102 +++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/AuthorizationUtil.java create mode 100644 ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/UnzipUtil.java (limited to 'ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils') diff --git a/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/AuthorizationUtil.java b/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/AuthorizationUtil.java new file mode 100644 index 00000000..f25f0db6 --- /dev/null +++ b/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/AuthorizationUtil.java @@ -0,0 +1,19 @@ +package org.openecomp.portalapp.widget.utils; + +import java.nio.charset.Charset; +import java.util.Base64; + +public class AuthorizationUtil { + + public boolean authorization(String auth, String security_user, String security_pass){ + if (auth != null && auth.startsWith("Basic")) { + String base64Credentials = auth.substring("Basic".length()).trim(); + String credentials = new String(Base64.getDecoder().decode(base64Credentials), + Charset.forName("UTF-8")); + final String[] values = credentials.split(":",2); + if(security_user.equals(values[0]) && security_pass.equals(values[1])) + return true; + } + return false; + } +} diff --git a/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/UnzipUtil.java b/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/UnzipUtil.java new file mode 100644 index 00000000..efbfe437 --- /dev/null +++ b/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/utils/UnzipUtil.java @@ -0,0 +1,102 @@ +package org.openecomp.portalapp.widget.utils; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.openecomp.portalapp.widget.constant.WidgetConstant; +import org.openecomp.portalapp.widget.service.impl.StorageServiceImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +public class UnzipUtil { + + /** + * Size of the buffer to read/write data + */ + private static final int BUFFER_SIZE = 4096; + private static final Logger logger = LoggerFactory.getLogger(UnzipUtil.class); + /** + * Extracts a zip file specified by the zipFilePath to a directory specified by + * destDirectory (will be created if does not exists) + * @param zipFilePath + * @param destDirectory + * @throws IOException + */ + + public Map unzip_db(String zipFilePath, String destDirectory, String widgetName) throws IOException { + + logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName); + File destDir = new File(destDirectory); + if (!destDir.exists()) { + destDir.mkdir(); + } + ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); + ZipEntry entry = zipIn.getNextEntry(); + Map map = new HashMap<>(); + + map.put(WidgetConstant.WIDGET_CONTROLLER_LOCATION, null); + map.put(WidgetConstant.WIDGET_MARKUP_LOCATION, null); + map.put(WidgetConstant.WIDGET_STYLE_LOCATION, null); + + + Stack stack = new Stack<>(); + + // iterates over entries in the zip file + while (entry != null) { + String filePath = destDirectory + File.separator + widgetName + File.separator + + entry.getName().substring(entry.getName().indexOf("/")+1); + logger.debug("UnzipUtil.unzip_db: file path " + filePath); + if (!entry.isDirectory()) { + // if the entry is a file, extracts it + logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath); + stack.push(new File(filePath)); + extractFile(zipIn, filePath); + } else { + // if the entry is a directory, make the directory + logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath); + File dir = new File(filePath); + stack.push(new File(filePath)); + + dir.mkdir(); + + } + if(map.containsKey(entry.getName().substring(entry.getName().indexOf("/")+1))){ + map.put(entry.getName().substring(entry.getName().indexOf("/")+1), Files.readAllBytes(Paths.get(filePath))); + } + zipIn.closeEntry(); + entry = zipIn.getNextEntry(); + } + zipIn.close(); + while(!stack.isEmpty()) + stack.pop().delete(); + return map; + } + + + + + /** + * Extracts a zip entry (file entry) + * @param zipIn + * @param filePath + * @throws IOException + */ + private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read = 0; + while ((read = zipIn.read(bytesIn)) != -1) { + bos.write(bytesIn, 0, read); + } + bos.close(); + } +} \ No newline at end of file -- cgit 1.2.3-korg