2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.core.utilities.file;
23 import org.apache.commons.collections4.MapUtils;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.HashMap;
30 import java.util.Optional;
32 import java.util.function.Function;
34 public class FileContentHandler {
36 private Map<String, byte[]> files = new HashMap<>();
41 * @param fileName the file name
42 * @return the file content
44 public InputStream getFileContent(String fileName) {
46 byte[] content = files.get(fileName);
47 if (content == null || content.length == 0) {
51 return new ByteArrayInputStream(content);
55 * Applies a business logic to a file's content while taking care of all retrieval logic.
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}
62 * @return result produced by the processor
64 public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) {
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);
74 public void addFile(String fileName, byte[] content) {
75 files.put(fileName, content);
78 public void addFile(String fileName, InputStream is) {
80 files.put(fileName, FileUtils.toByteArray(is));
83 public Map<String, byte[]> getFiles() {
87 public void setFiles(Map<String, byte[]> files) {
91 public void setFiles(FileContentHandler extFiles) {
92 extFiles.getFileList().stream()
93 .forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName)));
96 public Set<String> getFileList() {
97 return files.keySet();
100 public void putAll(Map<String, byte[]> files) {
104 public void addAll(FileContentHandler other) {
105 this.files.putAll(other.files);
108 public boolean isEmpty() {
109 return MapUtils.isEmpty(this.files);
112 public void remove(String fileName) {
113 files.remove(fileName);
116 public boolean containsFile(String fileName) {
117 return files.containsKey(fileName);
121 * An application-specific runtime exception
123 private static class ProcessingException extends RuntimeException {
125 public ProcessingException() {
129 public ProcessingException(String message) {
133 public ProcessingException(Throwable cause) {
137 public ProcessingException(String msg, Throwable cause) {