2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.translator.utils;
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;
29 import java.io.BufferedReader;
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;
37 import java.net.URISyntaxException;
39 import java.util.Enumeration;
40 import java.util.HashMap;
42 import java.util.function.BiConsumer;
43 import java.util.function.Predicate;
44 import java.util.zip.ZipEntry;
45 import java.util.zip.ZipFile;
47 public class ResourceWalker {
49 private ResourceWalker() {
53 * Read resources from directory map.
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
59 public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart)
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(),
77 private static void traverse(String start, BiConsumer<String, InputStream> handler) throws
80 URL url = ResourceWalker.class.getClassLoader().getResource(start);
82 throw new FileNotFoundException("Resource not found: " + start);
85 switch (url.getProtocol().toLowerCase()) {
88 traverseFile(new File(url.getPath()), handler);
92 String path = url.getPath();
93 int resourcePosition = path.lastIndexOf("!/" + start);
94 traverseArchive(path.substring(0, resourcePosition), start, handler);
97 throw new IllegalArgumentException("Unknown protocol");
101 private static void traverseArchive(String file, String resource, BiConsumer<String, InputStream>
103 throws URISyntaxException, IOException {
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())) {
112 Predicate<ZipEntry> predicate = buildPredicate(resource);
113 Enumeration<? extends ZipEntry> entries = zip.entries();
114 while (entries.hasMoreElements()) {
115 handleZipEntry(predicate, zip, entries.nextElement(), handler);
120 private static Predicate<ZipEntry> buildPredicate(String resource) {
122 if (resource.endsWith("/")) {
124 zipEntry.getName().startsWith(resource) && !zipEntry.isDirectory();
127 String name = zipEntry.getName();
128 return (name.equals(resource) || name.startsWith(resource + "/"))
129 && !zipEntry.isDirectory();
134 private static void handleZipEntry(Predicate<ZipEntry> predicate, ZipFile zip, ZipEntry zipEntry,
135 BiConsumer<String, InputStream> handler)
138 if (predicate.test(zipEntry)) {
140 try (InputStream input = zip.getInputStream(zipEntry)) {
141 handler.accept(zipEntry.getName(), input);
146 private static void traverseFile(File file, BiConsumer<String, InputStream> handler) throws
149 if (file.isDirectory()) {
150 File[] files = file.listFiles();
152 for (File sub : files) {
153 traverseFile(sub, handler);
157 try (FileInputStream stream = new FileInputStream(file)) {
158 handler.accept(file.getPath(), stream);