aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/utils/ResourceWalker.java
blob: 7e8ef16631264770287c3d4a95187f8b1750de91 (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
package org.openecomp.sdc.translator.utils;

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;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
import org.openecomp.sdc.logging.types.LoggerConstants;
import org.openecomp.sdc.logging.types.LoggerErrorCode;
import org.openecomp.sdc.logging.types.LoggerErrorDescription;
import org.openecomp.sdc.logging.types.LoggerTragetServiceName;

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;

/**
 * @author EVITALIY.
 * @since 02 Apr 17
 */
public class ResourceWalker {

  public static Map<String, String> readResourcesFromDirectory(String resourceDirectoryToStart)
      throws
      Exception {
    Map<String, String> filesContent = new HashMap<>();
    traverse(resourceDirectoryToStart, (fileName, stream) -> {
      BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
      try {
        filesContent.put(fileName, IOUtils.toString(reader));
      } catch (IOException exception) {
        MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API,
            LoggerTragetServiceName.READ_RESOURCE_FILE, ErrorLevel.ERROR.name(),
            LoggerErrorCode.DATA_ERROR.getErrorCode(),
            LoggerErrorDescription.RESOURCE_FILE_READ_ERROR
                + " File name = " + fileName);
        throw new CoreException((new ErrorCode.ErrorCodeBuilder())
            .withMessage(LoggerErrorDescription.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();
      };
    }
  }

  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()) {
      for (File sub : file.listFiles()) {
        traverseFile(sub, handler);
      }
    } else {
      try (FileInputStream stream = new FileInputStream(file)) {
        handler.accept(file.getName(), stream);
      }
    }
  }
}