7ad2cf9c6071983388966644e2741a739caa1403
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.utils;
18
19 import org.apache.commons.io.IOUtils;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.common.errors.ErrorCategory;
22 import org.openecomp.sdc.common.errors.ErrorCode;
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.logging.types.LoggerConstants;
25 import org.openecomp.sdc.logging.types.LoggerErrorCode;
26 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
27 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.net.URL;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.Map;
42 import java.util.function.BiConsumer;
43 import java.util.function.Predicate;
44 import java.util.zip.ZipEntry;
45 import java.util.zip.ZipFile;
46
47 public class ResourceWalker {
48
49   private ResourceWalker() {
50   }
51
52   /**
53    * Read resources from directory map.
54    *
55    * @param resourceDirectoryToStart the resource directory to start
56    * @return the map of file where key is file name and value is its data
57    * @throws Exception the exception
58    */
59   public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart)
60       throws
61       Exception {
62     Map<String, String> filesContent = new HashMap<>();
63     traverse(resourceDirectoryToStart, (fileName, stream) -> {
64       try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
65         filesContent.put(fileName, IOUtils.toString(reader));
66       } catch (IOException exception) {
67         throw new CoreException((new ErrorCode.ErrorCodeBuilder())
68             .withMessage(LoggerErrorDescription.RESOURCE_FILE_READ_ERROR
69                 + " File name = " + fileName)
70             .withId("Resource Read Error").withCategory(ErrorCategory.APPLICATION).build(),
71             exception);
72       }
73     });
74     return filesContent;
75   }
76
77   private static void traverse(String start, BiConsumer<String, InputStream> handler) throws
78       Exception {
79
80     URL url = ResourceWalker.class.getClassLoader().getResource(start);
81     if (url == null) {
82       throw new FileNotFoundException("Resource not found: " + start);
83     }
84
85     switch (url.getProtocol().toLowerCase()) {
86
87       case "file":
88         traverseFile(new File(url.getPath()), handler);
89         break;
90       case "zip":
91       case "jar":
92         String path = url.getPath();
93         int resourcePosition = path.lastIndexOf("!/" + start);
94         traverseArchive(path.substring(0, resourcePosition), start, handler);
95         break;
96       default:
97         throw new IllegalArgumentException("Unknown protocol");
98     }
99   }
100
101   private static void traverseArchive(String file, String resource, BiConsumer<String, InputStream>
102       handler)
103       throws URISyntaxException, IOException {
104
105     // There is what looks like a bug in Java:
106     // if "abc" is a directory in an archive,
107     // both "abc" and "abc/" will be found successfully.
108     // However, calling isDirectory() will return "true" for "abc/",
109     // but "false" for "abc".
110     try (ZipFile zip = new ZipFile(new URI(file).getPath())) {
111
112       Predicate<ZipEntry> predicate = buildPredicate(resource);
113       Enumeration<? extends ZipEntry> entries = zip.entries();
114       while (entries.hasMoreElements()) {
115         handleZipEntry(predicate, zip, entries.nextElement(), handler);
116       }
117     }
118   }
119
120   private static Predicate<ZipEntry> buildPredicate(String resource) {
121
122     if (resource.endsWith("/")) {
123       return zipEntry ->
124           zipEntry.getName().startsWith(resource) && !zipEntry.isDirectory();
125     } else {
126       return zipEntry -> {
127         String name = zipEntry.getName();
128         return (name.equals(resource) || name.startsWith(resource + "/"))
129             && !zipEntry.isDirectory();
130       };
131     }
132   }
133
134   private static void handleZipEntry(Predicate<ZipEntry> predicate, ZipFile zip, ZipEntry zipEntry,
135                                      BiConsumer<String, InputStream> handler)
136       throws IOException {
137
138     if (predicate.test(zipEntry)) {
139
140       try (InputStream input = zip.getInputStream(zipEntry)) {
141         handler.accept(zipEntry.getName(), input);
142       }
143     }
144   }
145
146   private static void traverseFile(File file, BiConsumer<String, InputStream> handler) throws
147       IOException {
148
149     if (file.isDirectory()) {
150       File[] files = file.listFiles();
151       if (files != null) {
152         for (File sub : files) {
153           traverseFile(sub, handler);
154         }
155       }
156     } else {
157       try (FileInputStream stream = new FileInputStream(file)) {
158         handler.accept(file.getPath(), stream);
159       }
160     }
161   }
162 }