summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-widget-ms
diff options
context:
space:
mode:
authorParshad Patel <pars.patel@samsung.com>2018-08-13 13:30:18 +0900
committerParshad Patel <pars.patel@samsung.com>2018-08-13 13:31:48 +0900
commitbcfcf7438c3dbe3209c95b51d325f90db05a65f0 (patch)
tree45c036d5185f9110d9a4f0427492c7d2793d6fa8 /ecomp-portal-widget-ms
parent8d5039c674ef6fd0e4f2ebef9edfac27f0c18900 (diff)
Fix blocker,critical sonar issue
Fix NPE, use try-with-resources issues Issue-ID: PORTAL-342 Change-Id: I9aaf805c012076bad5d803319cb5e3577471f635 Signed-off-by: Parshad Patel <pars.patel@samsung.com>
Diffstat (limited to 'ecomp-portal-widget-ms')
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java169
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java96
2 files changed, 127 insertions, 138 deletions
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
index c2633948..18611728 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
@@ -101,9 +101,9 @@ public class StorageServiceImpl implements StorageService {
String fileLocation = file.getOriginalFilename();
logger.debug("StorageServiceImpl.checkZipFile: store the widget to:" + fileLocation);
convFile = new File(fileLocation);
- FileOutputStream fos = new FileOutputStream(convFile);
- fos.write(file.getBytes());
- fos.close();
+ try(FileOutputStream fos = new FileOutputStream(convFile)){
+ fos.write(file.getBytes());
+ }
map = unzipper.unzip_db(fileLocation, ".", "tempWidgets");
convFile.delete();
} catch (IOException e) {
@@ -137,9 +137,9 @@ public class StorageServiceImpl implements StorageService {
String fileLocation = file.getOriginalFilename();
logger.debug("StorageServiceImpl.save: store the widget to:" + fileLocation);
convFile = new File(fileLocation);
- FileOutputStream fos = new FileOutputStream(convFile);
- fos.write(file.getBytes());
- fos.close();
+ try(FileOutputStream fos = new FileOutputStream(convFile)){
+ fos.write(file.getBytes());
+ }
map = unzipper.unzip_db(fileLocation, ".", "tempWidgets");
convFile.delete();
} catch (IOException e) {
@@ -183,35 +183,29 @@ public class StorageServiceImpl implements StorageService {
widgetFile.setName(newWidget.getName());
widgetFile.setWidgetId(widgetId);
- InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js");
+
String sb = null;
- try {
+ try(InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js")) {
byte[] bytes = new byte[fileInputStream.available()];
- fileInputStream.read(bytes);
- sb = new String(bytes, "UTF-8");
+ if(fileInputStream.read(bytes) > 0) {
+ sb = new String(bytes, "UTF-8");
+ }
} catch (IOException e) {
logger.error("StorageServiceImpl.save: Failed to load framework-template.js file ", e);
- e.printStackTrace();
- } finally {
- if (fileInputStream != null) {
- try {
- fileInputStream.close();
- } catch (IOException e) {
- logger.error("StorageServiceImpl.update: Failed to close the fileInputStream ", e);
- }
- }
}
String namespace = "Portal" + widgetId + "Widget";
String controllerName = "Portal" + widgetId + "Ctrl";
String cssName = "portal" + widgetId + "-css-ready";
String colorArg1 = "color: #fff";
- String framework = sb.replaceAll("ARGUMENT1", namespace).replaceAll("ARGUMENT2", controllerName)
- .replaceAll("ARGUMENT3", cssName).replaceAll("CSS_ARG1", colorArg1)
- .replaceAll("MICROSERVICE_ID", newWidget.getServiceId().toString())
- .replaceAll("WIDGET_ID", Long.toString(widgetId));
-
+ String framework="";
+ if(sb!=null) {
+ framework = sb.replaceAll("ARGUMENT1", namespace).replaceAll("ARGUMENT2", controllerName)
+ .replaceAll("ARGUMENT3", cssName).replaceAll("CSS_ARG1", colorArg1)
+ .replaceAll("MICROSERVICE_ID", newWidget.getServiceId().toString())
+ .replaceAll("WIDGET_ID", Long.toString(widgetId));
+ }
widgetFile.setFramework(framework.getBytes());
final byte[] controllerLoc = map.get(WidgetConstant.WIDGET_CONTROLLER_LOCATION);
@@ -277,9 +271,9 @@ public class StorageServiceImpl implements StorageService {
String fileLocation = file.getOriginalFilename();
logger.debug("StorageServiceImpl.update: store the widget to:" + fileLocation);
convFile = new File(fileLocation);
- FileOutputStream fos = new FileOutputStream(convFile);
- fos.write(file.getBytes());
- fos.close();
+ try(FileOutputStream fos = new FileOutputStream(convFile)){
+ fos.write(file.getBytes());
+ }
map = unzipper.unzip_db(fileLocation, ".", "tempWidgets");
convFile.delete();
} catch (IOException e) {
@@ -289,34 +283,27 @@ public class StorageServiceImpl implements StorageService {
}
WidgetFile widgetFile = getWidgetFile(widgetId);
- InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js");
String sb = null;
- try {
+ try(InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js")){
byte[] bytes = new byte[fileInputStream.available()];
- fileInputStream.read(bytes);
- sb = new String(bytes, "UTF-8");
+ if(fileInputStream.read(bytes) > 0) {
+ sb = new String(bytes, "UTF-8");
+ }
} catch (IOException e) {
logger.error("StorageServiceImpl.save: Failed to load framework-template.js file ", e);
- e.printStackTrace();
- } finally {
- if (fileInputStream != null) {
- try {
- fileInputStream.close();
- } catch (IOException e) {
- logger.error("StorageServiceImpl.update: Failed to close the fileInputStream ", e);
- e.printStackTrace();
- }
- }
}
String namespace = "Portal" + widgetId + "Widget";
String controllerName = "Portal" + widgetId + "Ctrl";
String cssName = "portal" + widgetId + "-css-ready";
String colorArg1 = "color: #fff";
- String framework = sb.replaceAll("ARGUMENT1", namespace).replaceAll("ARGUMENT2", controllerName)
- .replaceAll("ARGUMENT3", cssName).replaceAll("CSS_ARG1", colorArg1)
- .replaceAll("MICROSERVICE_ID", newWidget.getServiceId().toString())
- .replaceAll("WIDGET_ID", Long.toString(widgetId));
+ String framework="";
+ if(sb!=null) {
+ framework = sb.replaceAll("ARGUMENT1", namespace).replaceAll("ARGUMENT2", controllerName)
+ .replaceAll("ARGUMENT3", cssName).replaceAll("CSS_ARG1", colorArg1)
+ .replaceAll("MICROSERVICE_ID", newWidget.getServiceId().toString())
+ .replaceAll("WIDGET_ID", Long.toString(widgetId));
+ }
widgetFile.setFramework(framework.getBytes());
String javascript = new String(map.get(WidgetConstant.WIDGET_CONTROLLER_LOCATION));
@@ -441,53 +428,53 @@ public class StorageServiceImpl implements StorageService {
String styles = getWidgetCSS(widgetId).replaceAll(cssName, widget.getName() + "-css-ready");
File f = File.createTempFile("temp", ".zip");
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
- ZipEntry e = new ZipEntry(widget.getName() + "/styles/styles.css");
- out.putNextEntry(new ZipEntry(widget.getName() + "/"));
- out.putNextEntry(new ZipEntry(widget.getName() + "/styles/"));
- out.putNextEntry(e);
- byte[] data = styles.getBytes();
- out.write(data, 0, data.length);
-
- String widgetData = namespace + "=" + namespace + "||{};" + "var res = " + namespace + ".widgetData;";
- String javascript = getWidgetController(widgetId).replace(widgetData, "").replace(namespace + ".controller =",
- "");
-
- String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")") + 1);
- javascript = javascript.replaceFirst(controllerName, widget.getName() + "Ctrl");
- String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")"));
- StringBuilder injectStr = new StringBuilder().append("[");
- List<String> paramList = Arrays.asList(functionParam.split(","));
- for (int i = 0; i < paramList.size(); i++) {
- if (i == paramList.size() - 1)
- injectStr.append("'" + paramList.get(i).trim() + "'];");
- else
- injectStr.append("'" + paramList.get(i).trim() + "',");
+ try(ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))){
+ ZipEntry e = new ZipEntry(widget.getName() + "/styles/styles.css");
+ out.putNextEntry(new ZipEntry(widget.getName() + "/"));
+ out.putNextEntry(new ZipEntry(widget.getName() + "/styles/"));
+ out.putNextEntry(e);
+ byte[] data = styles.getBytes();
+ out.write(data, 0, data.length);
+
+ String widgetData = namespace + "=" + namespace + "||{};" + "var res = " + namespace + ".widgetData;";
+ String javascript = getWidgetController(widgetId).replace(widgetData, "").replace(namespace + ".controller =",
+ "");
+
+ String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")") + 1);
+ javascript = javascript.replaceFirst(controllerName, widget.getName() + "Ctrl");
+ String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")"));
+ StringBuilder injectStr = new StringBuilder().append("[");
+ List<String> paramList = Arrays.asList(functionParam.split(","));
+ for (int i = 0; i < paramList.size(); i++) {
+ if (i == paramList.size() - 1)
+ injectStr.append("'" + paramList.get(i).trim() + "'];");
+ else
+ injectStr.append("'" + paramList.get(i).trim() + "',");
+ }
+ javascript = javascript.replace(";" + namespace + ".controller.$inject = " + injectStr.toString(), "");
+
+ e = new ZipEntry(widget.getName() + "/js/controller.js");
+ out.putNextEntry(new ZipEntry(widget.getName() + "/js/"));
+ out.putNextEntry(e);
+ data = javascript.getBytes();
+ out.write(data, 0, data.length);
+
+ String html = getWidgetMarkup(widgetId).replaceFirst(controllerName, widget.getName() + "Ctrl");
+
+ // new
+ // String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName,
+ // controllerName);;
+
+ e = new ZipEntry(widget.getName() + "/markup/markup.html");
+ out.putNextEntry(new ZipEntry(widget.getName() + "/markup/"));
+ out.putNextEntry(e);
+ data = html.getBytes();
+ out.write(data, 0, data.length);
+ out.closeEntry();
+ byte[] result = Files.readAllBytes(Paths.get(f.getPath()));
+ f.delete();
+ return result;
}
- javascript = javascript.replace(";" + namespace + ".controller.$inject = " + injectStr.toString(), "");
-
- e = new ZipEntry(widget.getName() + "/js/controller.js");
- out.putNextEntry(new ZipEntry(widget.getName() + "/js/"));
- out.putNextEntry(e);
- data = javascript.getBytes();
- out.write(data, 0, data.length);
-
- String html = getWidgetMarkup(widgetId).replaceFirst(controllerName, widget.getName() + "Ctrl");
-
- // new
- // String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName,
- // controllerName);;
-
- e = new ZipEntry(widget.getName() + "/markup/markup.html");
- out.putNextEntry(new ZipEntry(widget.getName() + "/markup/"));
- out.putNextEntry(e);
- data = html.getBytes();
- out.write(data, 0, data.length);
- out.closeEntry();
- out.close();
- byte[] result = Files.readAllBytes(Paths.get(f.getPath()));
- f.delete();
- return result;
}
}
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
index d699e5da..f20ed1b5 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
@@ -46,49 +46,51 @@ public class UnzipUtil {
File destDir = new File(destDirectory);
if (!destDir.exists())
destDir.mkdir();
- ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
- ZipEntry entry = zipIn.getNextEntry();
- Map<String, byte[]> map = new HashMap<>();
-
- String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
- WidgetConstant.WIDGET_STYLE_LOCATION };
- for (String k : requiredKeys)
- map.put(k, null);
-
- // iterates over entries in the zip file
- Stack<File> stack = new Stack<>();
- while (entry != null) {
- String filePath = destDirectory + File.separator + widgetName + File.separator
- + entry.getName().substring(entry.getName().indexOf("/") + 1);
- final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1);
- logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName);
- 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();
+ try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)))
+ {
+ ZipEntry entry = zipIn.getNextEntry();
+ Map<String, byte[]> map = new HashMap<>();
+
+ String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
+ WidgetConstant.WIDGET_STYLE_LOCATION };
+ for (String k : requiredKeys)
+ map.put(k, null);
+
+ // iterates over entries in the zip file
+ Stack<File> stack = new Stack<>();
+ while (entry != null) {
+ String filePath = destDirectory + File.separator + widgetName + File.separator
+ + entry.getName().substring(entry.getName().indexOf("/") + 1);
+ final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1);
+ logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName);
+ 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();
+ }
+ // Is this one we need?
+ if (map.containsKey(entryShortName))
+ map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
+ zipIn.closeEntry();
+ entry = zipIn.getNextEntry();
}
- // Is this one we need?
- if (map.containsKey(entryShortName))
- map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
- zipIn.closeEntry();
- entry = zipIn.getNextEntry();
+
+ while (!stack.isEmpty())
+ stack.pop().delete();
+
+ for (String k : requiredKeys)
+ if (!map.containsKey(k))
+ logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k);
+
+ return map;
}
- zipIn.close();
- while (!stack.isEmpty())
- stack.pop().delete();
-
- for (String k : requiredKeys)
- if (!map.containsKey(k))
- logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k);
-
- return map;
}
/**
@@ -99,12 +101,12 @@ public class UnzipUtil {
* @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);
+ try(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