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.
16 package org.openecomp.sdc.translator.utils;
18 import java.io.BufferedReader;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
26 import java.net.URISyntaxException;
28 import java.util.Enumeration;
29 import java.util.HashMap;
31 import java.util.function.BiConsumer;
32 import java.util.function.Predicate;
33 import java.util.zip.ZipEntry;
34 import java.util.zip.ZipFile;
35 import org.apache.commons.io.IOUtils;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.common.errors.ErrorCategory;
38 import org.openecomp.sdc.common.errors.ErrorCode;
40 public class ResourceWalker {
42 private static final String RESOURCE_FILE_READ_ERROR = "Can't read resource file from class path.";
44 private ResourceWalker() {
48 * Read resources from directory map.
50 * @param resourceDirectoryToStart the resource directory to start
51 * @return the map of file where key is file name and value is its data
52 * @throws Exception the exception
54 public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart) throws Exception {
55 Map<String, String> filesContent = new HashMap<>();
56 traverse(resourceDirectoryToStart, (fileName, stream) -> {
57 try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
58 filesContent.put(fileName, IOUtils.toString(reader));
59 } catch (IOException exception) {
60 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(RESOURCE_FILE_READ_ERROR + " File name = " + fileName)
61 .withId("Resource Read Error").withCategory(ErrorCategory.APPLICATION).build(), exception);
67 private static void traverse(String start, BiConsumer<String, InputStream> handler) throws Exception {
68 URL url = ResourceWalker.class.getClassLoader().getResource(start);
70 throw new FileNotFoundException("Resource not found: " + start);
72 switch (url.getProtocol().toLowerCase()) {
74 traverseFile(new File(url.getPath()), handler);
78 String path = url.getPath();
79 int resourcePosition = path.lastIndexOf("!/" + start);
80 traverseArchive(path.substring(0, resourcePosition), start, handler);
83 throw new IllegalArgumentException("Unknown protocol");
87 private static void traverseArchive(String file, String resource, BiConsumer<String, InputStream> handler)
88 throws URISyntaxException, IOException {
89 // There is what looks like a bug in Java:
91 // if "abc" is a directory in an archive,
93 // both "abc" and "abc/" will be found successfully.
95 // However, calling isDirectory() will return "true" for "abc/",
97 // but "false" for "abc".
98 try (ZipFile zip = new ZipFile(new URI(file).getPath())) {
99 Predicate<ZipEntry> predicate = buildPredicate(resource);
100 Enumeration<? extends ZipEntry> entries = zip.entries();
101 while (entries.hasMoreElements()) {
102 handleZipEntry(predicate, zip, entries.nextElement(), handler);
107 private static Predicate<ZipEntry> buildPredicate(String resource) {
108 if (resource.endsWith("/")) {
109 return zipEntry -> zipEntry.getName().startsWith(resource) && !zipEntry.isDirectory();
112 String name = zipEntry.getName();
113 return (name.equals(resource) || name.startsWith(resource + "/")) && !zipEntry.isDirectory() && !name.contains("../");
118 private static void handleZipEntry(Predicate<ZipEntry> predicate, ZipFile zip, ZipEntry zipEntry, BiConsumer<String, InputStream> handler)
120 if (predicate.test(zipEntry)) {
121 try (InputStream input = zip.getInputStream(zipEntry)) {
122 handler.accept(zipEntry.getName(), input);
127 private static void traverseFile(File file, BiConsumer<String, InputStream> handler) throws IOException {
128 if (file.isDirectory()) {
129 File[] files = file.listFiles();
131 for (File sub : files) {
132 traverseFile(sub, handler);
136 try (FileInputStream stream = new FileInputStream(file)) {
137 handler.accept(file.getPath(), stream);