X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-widget-ms%2Fwidget-ms%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fportalapp%2Fwidget%2Fservice%2Fimpl%2FStorageServiceImpl.java;fp=ecomp-portal-widget-ms%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fportalapp%2Fwidget%2Fservice%2Fimpl%2FStorageServiceImpl.java;h=f459ffc4172a0a054013a9daefc0e62ccc30c393;hb=978dbcf0a196acbafad72fe1e2478ec0e384f02f;hp=046b5ac47c80ccd602f9320d609f45279ade9342;hpb=bc7350dce5b7b1dcd1c472a3922b42c4ea99809d;p=portal.git diff --git a/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java similarity index 81% rename from ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java rename to ecomp-portal-widget-ms/widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java index 046b5ac4..f459ffc4 100644 --- a/ecomp-portal-widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java +++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/openecomp/portalapp/widget/service/impl/StorageServiceImpl.java @@ -1,14 +1,11 @@ package org.openecomp.portalapp.widget.service.impl; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -24,7 +21,6 @@ import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import org.openecomp.portalapp.widget.constant.WidgetConstant; -import org.openecomp.portalapp.widget.controller.DatabaseFileUploadController; import org.openecomp.portalapp.widget.domain.ValidationRespond; import org.openecomp.portalapp.widget.domain.WidgetCatalog; import org.openecomp.portalapp.widget.domain.WidgetFile; @@ -43,20 +39,21 @@ import org.springframework.web.multipart.MultipartFile; public class StorageServiceImpl implements StorageService { private static final Logger logger = LoggerFactory.getLogger(StorageServiceImpl.class); - + @Autowired private SessionFactory sessionFactory; @Autowired WidgetCatalogService widgetCatalogService; - + @Override @Transactional public void deleteWidgetFile(long widgetId) { WidgetFile widgetFile = getWidgetFile(widgetId); logger.debug("StorageServiceImpl.deleteWidgetFile: deleting widget file {}", widgetId); - if (widgetFile == null){ - logger.debug("StorageServiceImpl.deleteWidgetFile: No widget file found in database while performing StorageServiceImpl.deleteWidgetFile."); + if (widgetFile == null) { + logger.debug( + "StorageServiceImpl.deleteWidgetFile: No widget file found in database while performing StorageServiceImpl.deleteWidgetFile."); return; } Session session = sessionFactory.getCurrentSession(); @@ -64,7 +61,7 @@ public class StorageServiceImpl implements StorageService { session.delete(widgetFile); tx.commit(); } - + @Override @SuppressWarnings("unchecked") @Transactional @@ -81,9 +78,9 @@ public class StorageServiceImpl implements StorageService { widgetFile = widgetFiles.get(0); return widgetFile; } - + @Override - public ValidationRespond checkZipFile(MultipartFile file){ + public ValidationRespond checkZipFile(MultipartFile file) { StringBuilder error_msg = new StringBuilder(); UnzipUtil unzipper = new UnzipUtil(); Map map; @@ -96,8 +93,10 @@ public class StorageServiceImpl implements StorageService { } try { if (file.isEmpty()) { - logger.error("StorageServiceImpl.checkZipFile: Failed to store empty file " + file.getOriginalFilename()); - throw new StorageException("StorageServiceImpl.checkZipFile: Failed to store empty file " + file.getOriginalFilename()); + logger.error( + "StorageServiceImpl.checkZipFile: Failed to store empty file " + file.getOriginalFilename()); + throw new StorageException( + "StorageServiceImpl.checkZipFile: Failed to store empty file " + file.getOriginalFilename()); } String fileLocation = file.getOriginalFilename(); logger.debug("StorageServiceImpl.checkZipFile: store the widget to:" + fileLocation); @@ -109,23 +108,24 @@ public class StorageServiceImpl implements StorageService { convFile.delete(); } catch (IOException e) { logger.error("StorageServiceImpl.checkZipFile: Failed to store file " + file.getOriginalFilename(), e); - throw new StorageException("torageServiceImpl.checkZipFile: Failed to store file " + file.getOriginalFilename(), e); + throw new StorageException( + "torageServiceImpl.checkZipFile: Failed to store file " + file.getOriginalFilename(), e); } - - for(byte[] b: map.values()){ - if(isValid && b == null){ + + for (byte[] b : map.values()) { + if (isValid && b == null) { isValid = false; error_msg.append(WidgetConstant.VALIDATION_MESSAGE_FILES); break; } } - return new ValidationRespond(isValid, error_msg.toString()); + return new ValidationRespond(isValid, error_msg.toString()); } - + @Override @Transactional public void save(MultipartFile file, WidgetCatalog newWidget, long widgetId) { - + UnzipUtil unzipper = new UnzipUtil(); Map map; File convFile; @@ -148,15 +148,14 @@ public class StorageServiceImpl implements StorageService { } saveHelper(newWidget, widgetId, map); } - - + @Override @Transactional public void initSave(File file, WidgetCatalog newWidget, long widgetId) { - + UnzipUtil unzipper = new UnzipUtil(); Map map; - + try { String fileLocation = file.getPath(); logger.debug("StorageServiceImpl.save: store the widget to:" + fileLocation); @@ -165,27 +164,27 @@ public class StorageServiceImpl implements StorageService { logger.error("StorageServiceImpl.save: Failed to store file " + file.getName(), e); throw new StorageException("Failed to store file " + file.getName(), e); } + saveHelper(newWidget, widgetId, map); } - - + /** - * Helper method for saving widget files (controller.js, framework.js, markup.html and style.css) - * to ep_widget_catalog_files table in database + * Helper method for saving widget files (controller.js, framework.js, + * markup.html and style.css) to ep_widget_catalog_files table in database * * @param newWidget * @param widgetId * @param map */ - private void saveHelper(WidgetCatalog newWidget, long widgetId, Map map){ - + private void saveHelper(WidgetCatalog newWidget, long widgetId, Map map) { + + logger.debug("Going to save widget " + newWidget); WidgetFile widgetFile = new WidgetFile(); widgetFile.setName(newWidget.getName()); widgetFile.setWidgetId(widgetId); - - + InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js"); - + String sb = null; try { byte[] bytes = new byte[fileInputStream.available()]; @@ -200,65 +199,71 @@ public class StorageServiceImpl implements StorageService { 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("ARUGMENT1", namespace) - .replaceAll("ARUGMENT2", controllerName) - .replaceAll("ARUGMENT3", cssName) - .replaceAll("CSS_ARG1", colorArg1) + String framework = sb.replaceAll("ARUGMENT1", namespace).replaceAll("ARUGMENT2", controllerName) + .replaceAll("ARUGMENT3", 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)); - String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")")+1); + + final byte[] controllerLoc = map.get(WidgetConstant.WIDGET_CONTROLLER_LOCATION); + if (controllerLoc == null || controllerLoc.length == 0) + throw new IllegalArgumentException( + "Map is missing required key " + WidgetConstant.WIDGET_CONTROLLER_LOCATION); + String javascript = new String(controllerLoc); + String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")") + 1); String functionName = functionHeader.substring(functionHeader.indexOf(" "), functionHeader.indexOf("(")).trim(); javascript = javascript.replaceFirst(functionName, controllerName); - String functionParam = functionHeader.substring(functionHeader.indexOf("(")+1, functionHeader.indexOf(")")); + String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")")); List paramList = Arrays.asList(functionParam.split(",")); - + int left_bracket_index = javascript.indexOf("{") + 1; String widgetData = namespace + "=" + namespace + "||{};" + "var res = " + namespace + ".widgetData;"; - javascript = javascript.substring(0, left_bracket_index) + widgetData + javascript.substring(left_bracket_index); - + javascript = javascript.substring(0, left_bracket_index) + widgetData + + javascript.substring(left_bracket_index); + StringBuilder injectStr = new StringBuilder().append("["); - for(int i = 0; i < paramList.size(); i++){ - if(i == paramList.size()-1) + 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 = namespace + ".controller = " + javascript + ";" + namespace + ".controller.$inject = " + injectStr.toString(); - - String html = new String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName, controllerName);; - + javascript = namespace + ".controller = " + javascript + ";" + namespace + ".controller.$inject = " + + injectStr.toString(); + + String html = new String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName, + controllerName); + ; + Pattern cssPattern = Pattern.compile("#.*-css-ready"); Matcher cssMatcher = cssPattern.matcher(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION))); - if (cssMatcher.find( )) { - widgetFile.setCss(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)).replace(cssMatcher.group(0), "#" + cssName).getBytes()); + if (cssMatcher.find()) { + widgetFile.setCss(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)) + .replace(cssMatcher.group(0), "#" + cssName).getBytes()); } - + widgetFile.setMarkup(html.getBytes()); widgetFile.setController(javascript.getBytes()); Session session = sessionFactory.openSession(); session.save(widgetFile); session.flush(); session.close(); - //sessionFactory.getCurrentSession().save(widgetFile); - logger.debug("StorageServiceImpl.save: saved fraemwork.js controller.js, markup.html and style.css files to the database for widget {}", widgetId); - + // sessionFactory.getCurrentSession().save(widgetFile); + logger.debug( + "StorageServiceImpl.save: saved fraemwork.js controller.js, markup.html and style.css files to the database for widget {}", + widgetId); + } - + @Override public void update(MultipartFile file, WidgetCatalog newWidget, long widgetId) { UnzipUtil unzipper = new UnzipUtil(); @@ -279,10 +284,11 @@ public class StorageServiceImpl implements StorageService { convFile.delete(); } catch (IOException e) { logger.error("StorageServiceImpl.update: Failed to store file " + file.getOriginalFilename(), e); - throw new StorageException("StorageServiceImpl.update: Failed to store file " + file.getOriginalFilename(), e); + throw new StorageException("StorageServiceImpl.update: Failed to store file " + file.getOriginalFilename(), + e); } WidgetFile widgetFile = getWidgetFile(widgetId); - + InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream("framework-template.js"); String sb = null; try { @@ -302,61 +308,64 @@ public class StorageServiceImpl implements StorageService { } } } - + String namespace = "Portal" + widgetId + "Widget"; String controllerName = "Portal" + widgetId + "Ctrl"; String cssName = "portal" + widgetId + "-css-ready"; String colorArg1 = "color: #fff"; - String framework = sb - .replaceAll("ARUGMENT1", namespace) - .replaceAll("ARUGMENT2", controllerName) - .replaceAll("ARUGMENT3", cssName) - .replaceAll("CSS_ARG1", colorArg1) + String framework = sb.replaceAll("ARUGMENT1", namespace).replaceAll("ARUGMENT2", controllerName) + .replaceAll("ARUGMENT3", 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)); - String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")")+1); + String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")") + 1); String functionName = functionHeader.substring(functionHeader.indexOf(" "), functionHeader.indexOf("(")).trim(); javascript = javascript.replaceFirst(functionName, controllerName); - String functionParam = functionHeader.substring(functionHeader.indexOf("(")+1, functionHeader.indexOf(")")); + String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")")); List paramList = Arrays.asList(functionParam.split(",")); - + int left_bracket_index = javascript.indexOf("{") + 1; String widgetData = namespace + "=" + namespace + "||{};" + "var res = " + namespace + ".widgetData;"; - javascript = javascript.substring(0, left_bracket_index) + widgetData + javascript.substring(left_bracket_index); - + javascript = javascript.substring(0, left_bracket_index) + widgetData + + javascript.substring(left_bracket_index); + StringBuilder injectStr = new StringBuilder().append("["); - for(int i = 0; i < paramList.size(); i++){ - if(i == paramList.size()-1) + 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 = namespace + ".controller = " + javascript + ";" + namespace + ".controller.$inject = " + injectStr.toString(); - - String html = new String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName, controllerName);; - + javascript = namespace + ".controller = " + javascript + ";" + namespace + ".controller.$inject = " + + injectStr.toString(); + + String html = new String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName, + controllerName); + ; + Pattern cssPattern = Pattern.compile("#.*-css-ready"); Matcher cssMatcher = cssPattern.matcher(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION))); - if (cssMatcher.find( )) { - widgetFile.setCss(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)).replace(cssMatcher.group(0), "#" + cssName).getBytes()); + if (cssMatcher.find()) { + widgetFile.setCss(new String(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)) + .replace(cssMatcher.group(0), "#" + cssName).getBytes()); } - + widgetFile.setMarkup(html.getBytes()); widgetFile.setController(javascript.getBytes()); - //widgetFile.setCss(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)); + // widgetFile.setCss(map.get(WidgetConstant.WIDGET_STYLE_LOCATION)); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.update(widgetFile); tx.commit(); session.flush(); session.close(); - logger.debug("StorageServiceImpl.save: updated fraemwork.js controller.js, markup.html and style.css files to the database for widget {}", widgetId); + logger.debug( + "StorageServiceImpl.save: updated fraemwork.js controller.js, markup.html and style.css files to the database for widget {}", + widgetId); } - @Override @SuppressWarnings("unchecked") @Transactional @@ -367,9 +376,9 @@ public class StorageServiceImpl implements StorageService { criteria.add(Restrictions.eq("widgetId", widgetId)); List widgetFile = criteria.list(); logger.debug("StorageServiceImpl.getWidgetMarkup: getting widget markup result={}", widgetFile); - - if(widgetFile.size() > 0 ) - markup = new String(widgetFile.get(0).getMarkup(), "UTF-8"); + + if (widgetFile.size() > 0) + markup = new String(widgetFile.get(0).getMarkup(), "UTF-8"); return markup; } @@ -383,8 +392,8 @@ public class StorageServiceImpl implements StorageService { criteria.add(Restrictions.eq("widgetId", widgetId)); List widgetFile = criteria.list(); logger.debug("StorageServiceImpl.getWidgetController: getting widget controller result={}", widgetFile); - - if(widgetFile.size() > 0 ) + + if (widgetFile.size() > 0) controller = new String(widgetFile.get(0).getController(), "UTF-8"); return controller; } @@ -399,8 +408,8 @@ public class StorageServiceImpl implements StorageService { criteria.add(Restrictions.eq("widgetId", widgetId)); List widgetFile = criteria.list(); logger.debug("StorageServiceImpl.getWidgetFramework: getting widget framework result={}", widgetFile); - - if(widgetFile.size() > 0 ) + + if (widgetFile.size() > 0) framework = new String(widgetFile.get(0).getFramework(), "UTF-8"); return framework; } @@ -415,24 +424,21 @@ public class StorageServiceImpl implements StorageService { criteria.add(Restrictions.eq("widgetId", widgetId)); List widgetFile = criteria.list(); logger.debug("StorageServiceImpl.getWidgetCSS: getting widget css result={}", widgetFile); - - if(widgetFile.size() > 0 ) + + if (widgetFile.size() > 0) css = new String(widgetFile.get(0).getCss(), "UTF-8"); return css; } - - + @Override @Transactional - public byte[] getWidgetCatalogContent(long widgetId) throws Exception{ - - + public byte[] getWidgetCatalogContent(long widgetId) throws Exception { + WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId); String namespace = "Portal" + widgetId + "Widget"; String controllerName = "Portal" + widgetId + "Ctrl"; String cssName = "portal" + widgetId + "-css-ready"; - - + String styles = getWidgetCSS(widgetId).replaceAll(cssName, widget.getName() + "-css-ready"); File f = File.createTempFile("temp", ".zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); @@ -442,36 +448,36 @@ public class StorageServiceImpl implements StorageService { 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); + 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(")")); + String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")")); StringBuilder injectStr = new StringBuilder().append("["); List paramList = Arrays.asList(functionParam.split(",")); - for(int i = 0; i < paramList.size(); i++){ - if(i == paramList.size()-1) + 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);; - - + + // 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);