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 java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
28 import java.util.Optional;
30 import java.util.function.Function;
32 import org.apache.commons.collections4.MapUtils;
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}
61 * @return result produced by the processor
63 public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) {
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);
73 public void addFile(String fileName, byte[] content) {
74 files.put(fileName, content);
77 public void addFile(String fileName, InputStream is) {
79 files.put(fileName, FileUtils.toByteArray(is));
82 public Map<String, byte[]> getFiles() {
86 public void setFiles(Map<String, byte[]> files) {
90 public void setFiles(FileContentHandler extFiles) {
91 extFiles.getFileList().forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName)));
94 public Set<String> getFileList() {
95 return files.keySet();
98 public void putAll(Map<String, byte[]> files) {
102 public void addAll(FileContentHandler other) {
103 this.files.putAll(other.files);
106 public boolean isEmpty() {
107 return MapUtils.isEmpty(this.files);
110 public void remove(String fileName) {
111 files.remove(fileName);
114 public boolean containsFile(String fileName) {
115 return files.containsKey(fileName);
119 * An application-specific runtime exception
121 private static class ProcessingException extends RuntimeException {
123 public ProcessingException() {
127 public ProcessingException(String message) {
131 public ProcessingException(Throwable cause) {
135 public ProcessingException(String msg, Throwable cause) {