App onboarding fixes
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / onap / portalapp / widget / utils / UnzipUtil.java
1 package org.onap.portalapp.widget.utils;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.Stack;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
15
16 import org.onap.portalapp.widget.constant.WidgetConstant;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class UnzipUtil {
21
22         /**
23          * Size of the buffer to read/write data
24          */
25         private static final int BUFFER_SIZE = 4096;
26         private static final Logger logger = LoggerFactory.getLogger(UnzipUtil.class);
27
28         /**
29          * Extracts a zip file specified by the zipFilePath to a directory specified by
30          * destDirectory (will be created if does not exists)
31          * 
32          * @param zipFilePath
33          *            path
34          * @param destDirectory
35          *            directory
36          * @param widgetName
37          *            name
38          * @return Map of contents
39          * @throws IOException
40          *             On error
41          */
42         public Map<String, byte[]> unzip_db(String zipFilePath, String destDirectory, String widgetName)
43                         throws IOException {
44
45                 logger.debug("UnzipUtil.unzip_db: unzip widget file {}", widgetName);
46                 File destDir = new File(destDirectory);
47                 if (!destDir.exists())
48                         destDir.mkdir();
49                 try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)))
50                 {
51                         ZipEntry entry = zipIn.getNextEntry();
52                         Map<String, byte[]> map = new HashMap<>();
53         
54                         String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
55                                         WidgetConstant.WIDGET_STYLE_LOCATION };
56                         for (String k : requiredKeys)
57                                 map.put(k, null);
58         
59                         // iterates over entries in the zip file
60                         Stack<File> stack = new Stack<>();
61                         while (entry != null) {
62                                 String filePath = destDirectory + File.separator + widgetName + File.separator
63                                                 + entry.getName().substring(entry.getName().indexOf("/") + 1);
64                                 final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1);
65                                 logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName);
66                                 if (!entry.isDirectory()) {
67                                         // if the entry is a file, extracts it
68                                         logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath);
69                                         stack.push(new File(filePath));
70                                         extractFile(zipIn, filePath);
71                                 } else {
72                                         // if the entry is a directory, make the directory
73                                         logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath);
74                                         File dir = new File(filePath);
75                                         stack.push(new File(filePath));
76                                         dir.mkdir();
77                                 }
78                                 // Is this one we need?
79                                 if (map.containsKey(entryShortName))
80                                         map.put(entryShortName, Files.readAllBytes(Paths.get(filePath)));
81                                 zipIn.closeEntry();
82                                 entry = zipIn.getNextEntry();
83                         }
84                 
85                         while (!stack.isEmpty())
86                                 stack.pop().delete();
87         
88                         for (String k : requiredKeys)
89                                 if (!map.containsKey(k))
90                                         logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k);
91                         
92                         return map;
93                 }
94         }
95
96         /**
97          * Extracts a zip entry (file entry)
98          * 
99          * @param zipIn
100          * @param filePath
101          * @throws IOException
102          */
103         private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
104                 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))){
105                         byte[] bytesIn = new byte[BUFFER_SIZE];
106                         int read = 0;
107                         while ((read = zipIn.read(bytesIn)) != -1) {
108                                 bos.write(bytesIn, 0, read);
109                         }
110                 }
111         }
112 }