1 package org.openecomp.sdc.translator.utils;
3 import org.apache.commons.io.IOUtils;
4 import org.openecomp.sdc.common.errors.CoreException;
5 import org.openecomp.sdc.common.errors.ErrorCategory;
6 import org.openecomp.sdc.common.errors.ErrorCode;
7 import org.openecomp.sdc.datatypes.error.ErrorLevel;
8 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
9 import org.openecomp.sdc.logging.types.LoggerConstants;
10 import org.openecomp.sdc.logging.types.LoggerErrorCode;
11 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
12 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
14 import java.io.BufferedReader;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
22 import java.net.URISyntaxException;
24 import java.util.Enumeration;
25 import java.util.HashMap;
27 import java.util.function.BiConsumer;
28 import java.util.function.Predicate;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipFile;
36 public class ResourceWalker {
38 public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart)
41 Map<String, String> filesContent = new HashMap<>();
42 traverse(resourceDirectoryToStart, (fileName, stream) -> {
43 try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
44 filesContent.put(fileName, IOUtils.toString(reader));
45 } catch (IOException exception) {
46 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
47 LoggerTragetServiceName.READ_RESOURCE_FILE, ErrorLevel.ERROR.name(),
48 LoggerErrorCode.DATA_ERROR.getErrorCode(),
49 LoggerErrorDescription.RESOURCE_FILE_READ_ERROR
50 + " File name = " + fileName);
51 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
52 .withMessage(LoggerErrorDescription.RESOURCE_FILE_READ_ERROR
53 + " File name = " + fileName)
54 .withId("Resource Read Error").withCategory(ErrorCategory.APPLICATION).build(),
61 private static void traverse(String start, BiConsumer<String, InputStream> handler) throws
64 URL url = ResourceWalker.class.getClassLoader().getResource(start);
66 throw new FileNotFoundException("Resource not found: " + start);
69 switch (url.getProtocol().toLowerCase()) {
72 traverseFile(new File(url.getPath()), handler);
76 String path = url.getPath();
77 int resourcePosition = path.lastIndexOf("!/" + start);
78 traverseArchive(path.substring(0, resourcePosition), start, handler);
81 throw new IllegalArgumentException("Unknown protocol");
85 private static void traverseArchive(String file, String resource, BiConsumer<String, InputStream>
87 throws URISyntaxException, IOException {
89 // There is what looks like a bug in Java:
90 // if "abc" is a directory in an archive,
91 // both "abc" and "abc/" will be found successfully.
92 // However, calling isDirectory() will return "true" for "abc/",
93 // but "false" for "abc".
94 try (ZipFile zip = new ZipFile(new URI(file).getPath())) {
96 Predicate<ZipEntry> predicate = buildPredicate(resource);
97 Enumeration<? extends ZipEntry> entries = zip.entries();
98 while (entries.hasMoreElements()) {
99 handleZipEntry(predicate, zip, entries.nextElement(), handler);
104 private static Predicate<ZipEntry> buildPredicate(String resource) {
106 if (resource.endsWith("/")) {
108 zipEntry.getName().startsWith(resource) && !zipEntry.isDirectory();
111 String name = zipEntry.getName();
112 return (name.equals(resource) || name.startsWith(resource + "/"))
113 && !zipEntry.isDirectory();
118 private static void handleZipEntry(Predicate<ZipEntry> predicate, ZipFile zip, ZipEntry zipEntry,
119 BiConsumer<String, InputStream> handler)
122 if (predicate.test(zipEntry)) {
124 try (InputStream input = zip.getInputStream(zipEntry)) {
125 handler.accept(zipEntry.getName(), input);
130 private static void traverseFile(File file, BiConsumer<String, InputStream> handler) throws
133 if (file.isDirectory()) {
134 for (File sub : file.listFiles()) {
135 traverseFile(sub, handler);
138 try (FileInputStream stream = new FileInputStream(file)) {
139 handler.accept(file.getPath(), stream);