summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java
blob: c3c1febf4049c7fbac755b45fed812448697c976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.translator.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCategory;
import org.openecomp.sdc.common.errors.ErrorCode;

public class ResourceWalker {

  private static final String RESOURCE_FILE_READ_ERROR = "Can't read resource file from class path.";

  private ResourceWalker() {
  }

  /**
   * Read resources from directory map.
   *
   * @param resourceDirectoryToStart the resource directory to start
   * @return the map of file where key is file name and value is its data
   * @throws Exception the exception
   */
  public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart)
      throws
      Exception {
    Map<String, String> filesContent = new HashMap<>();
    traverse(resourceDirectoryToStart, (fileName, stream) -> {
      try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
        filesContent.put(fileName, IOUtils.toString(reader));
      } catch (IOException exception) {
        throw new CoreException((new ErrorCode.ErrorCodeBuilder())
            .withMessage(RESOURCE_FILE_READ_ERROR
                + " File name = " + fileName)
            .withId("Resource Read Error").withCategory(ErrorCategory.APPLICATION).build(),
            exception);
      }
    });
    return filesContent;
  }

  private static void traverse(String start, BiConsumer<String, InputStream> handler) throws
      Exception {

    URL url = ResourceWalker.class.getClassLoader().getResource(start);
    if (url == null) {
      throw new FileNotFoundException("Resource not found: " + start);
    }

    switch (url.getProtocol().toLowerCase()) {

      case "file":
        traverseFile(new File(url.getPath()), handler);
        break;
      case "zip":
      case "jar":
        String path = url.getPath();
        int resourcePosition = path.lastIndexOf("!/" + start);
        traverseArchive(path.substring(0, resourcePosition), start, handler);
        break;
      default:
        throw new IllegalArgumentException("Unknown protocol");
    }
  }

  private static void traverseArchive(String file, String resource, BiConsumer<String, InputStream>
      handler)
      throws URISyntaxException, IOException {

    // There is what looks like a bug in Java:
    // if "abc" is a directory in an archive,
    // both "abc" and "abc/" will be found successfully.
    // However, calling isDirectory() will return "true" for "abc/",
    // but "false" for "abc".
    try (ZipFile zip = new ZipFile(new URI(file).getPath())) {

      Predicate<ZipEntry> predicate = buildPredicate(resource);
      Enumeration<? extends ZipEntry> entries = zip.entries();
      while (entries.hasMoreElements()) {
        handleZipEntry(predicate, zip, entries.nextElement(), handler);
      }
    }
  }

  private static Predicate<ZipEntry> buildPredicate(String resource) {

    if (resource.endsWith("/")) {
      return zipEntry ->
          zipEntry.getName().startsWith(resource) && !zipEntry.isDirectory();
    } else {
      return zipEntry -> {
        String name = zipEntry.getName();
        return (name.equals(resource) || name.startsWith(resource + "/"))
            && !zipEntry.isDirectory() && !name.contains("../");
      };
    }
  }

  private static void handleZipEntry(Predicate<ZipEntry> predicate, ZipFile zip, ZipEntry zipEntry,
                                     BiConsumer<String, InputStream> handler)
      throws IOException {

    if (predicate.test(zipEntry)) {

      try (InputStream input = zip.getInputStream(zipEntry)) {
        handler.accept(zipEntry.getName(), input);
      }
    }
  }

  private static void traverseFile(File file, BiConsumer<String, InputStream> handler) throws
      IOException {

    if (file.isDirectory()) {
      File[] files = file.listFiles();
      if (files != null) {
        for (File sub : files) {
          traverseFile(sub, handler);
        }
      }
    } else {
      try (FileInputStream stream = new FileInputStream(file)) {
        handler.accept(file.getPath(), stream);
      }
    }
  }
}