c96ceeb46a3e6138d7a5620540d090dd0c778cde
[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 java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Set;
30 import java.util.function.Function;
31
32 import org.apache.commons.collections4.MapUtils;
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      * @return result produced by the processor
62      */
63     public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) {
64
65         // do not throw IOException to mimic the existing uses of getFileContent()
66         try (InputStream contentInputStream = getFileContent(fileName)) {
67             return processor.apply(Optional.ofNullable(contentInputStream));
68         } catch (IOException e) {
69             throw new ProcessingException("Failed to process file: " + fileName, e);
70         }
71     }
72
73     public void addFile(String fileName, byte[] content) {
74         files.put(fileName, content);
75     }
76
77     public void addFile(String fileName, InputStream is) {
78
79         files.put(fileName, FileUtils.toByteArray(is));
80     }
81
82     public Map<String, byte[]> getFiles() {
83         return files;
84     }
85
86     public void setFiles(Map<String, byte[]> files) {
87         this.files = files;
88     }
89
90     public void setFiles(FileContentHandler extFiles) {
91         extFiles.getFileList().forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName)));
92     }
93
94     public Set<String> getFileList() {
95         return files.keySet();
96     }
97
98     public void putAll(Map<String, byte[]> files) {
99         this.files = files;
100     }
101
102     public void addAll(FileContentHandler other) {
103         this.files.putAll(other.files);
104     }
105
106     public boolean isEmpty() {
107         return MapUtils.isEmpty(this.files);
108     }
109
110     public void remove(String fileName) {
111         files.remove(fileName);
112     }
113
114     public boolean containsFile(String fileName) {
115         return files.containsKey(fileName);
116     }
117
118     /**
119      * An application-specific runtime exception
120      */
121     private static class ProcessingException extends RuntimeException {
122
123         public ProcessingException() {
124             super();
125         }
126
127         public ProcessingException(String message) {
128             super(message);
129         }
130
131         public ProcessingException(Throwable cause) {
132             super(cause);
133         }
134
135         public ProcessingException(String msg, Throwable cause) {
136             super(msg, cause);
137         }
138     }
139 }