d5cd56e05867ab184d6ad6eb0528808a92ab26eb
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.utilities.file;
22
23 import org.apache.commons.collections4.MapUtils;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.Set;
32 import java.util.function.Function;
33
34 public class FileContentHandler {
35
36   private Map<String, byte[]> files = new HashMap<>();
37
38   /**
39    * Gets file content.
40    *
41    * @param fileName the file name
42    * @return the file content
43    */
44   public InputStream getFileContent(String fileName) {
45
46     byte[] content = files.get(fileName);
47     if (content == null || content.length == 0) {
48       return null;
49     }
50
51     return new ByteArrayInputStream(content);
52   }
53
54   /**
55    * Applies a business logic to a file's content while taking care of all retrieval logic.
56    *
57    * @param fileName name of a file inside this content handler.
58    * @param processor the business logic to work on the file's input stream, which may not be set
59    *                  (check the {@link Optional} if no such file can be found
60    * @param <T> return type, may be {@link java.lang.Void}
61    *
62    * @return result produced by the processor
63    */
64   public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) {
65
66     // do not throw IOException to mimic the existing uses of getFileContent()
67     try (InputStream contentInputStream = getFileContent(fileName)) {
68       return processor.apply(Optional.ofNullable(contentInputStream));
69     } catch (IOException e) {
70       throw new ProcessingException("Failed to process file: " + fileName, e);
71     }
72   }
73
74   public void addFile(String fileName, byte[] content) {
75     files.put(fileName, content);
76   }
77
78   public void addFile(String fileName, InputStream is) {
79
80     files.put(fileName, FileUtils.toByteArray(is));
81   }
82
83   public Map<String, byte[]> getFiles() {
84     return files;
85   }
86
87   public void setFiles(Map<String, byte[]> files) {
88     this.files = files;
89   }
90
91   public void setFiles(FileContentHandler extFiles) {
92     extFiles.getFileList().stream()
93         .forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName)));
94   }
95
96   public Set<String> getFileList() {
97     return files.keySet();
98   }
99
100   public void putAll(Map<String, byte[]> files) {
101     this.files = files;
102   }
103
104   public void addAll(FileContentHandler other) {
105     this.files.putAll(other.files);
106   }
107
108   public boolean isEmpty() {
109     return MapUtils.isEmpty(this.files);
110   }
111
112   public void remove(String fileName) {
113     files.remove(fileName);
114   }
115
116   public boolean containsFile(String fileName) {
117     return files.containsKey(fileName);
118   }
119
120   /**
121    * An application-specific runtime exception
122    */
123   private static class ProcessingException extends RuntimeException {
124
125     public ProcessingException() {
126       super();
127     }
128
129     public ProcessingException(String message) {
130       super(message);
131     }
132
133     public ProcessingException(Throwable cause) {
134       super(cause);
135     }
136
137     public ProcessingException(String msg, Throwable cause) {
138       super(msg, cause);
139     }
140   }
141 }