From 433947b5ab5e28fc29aee447de934de89a707419 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 19 Sep 2019 16:14:01 +0100 Subject: Centralize onboarding package validation Change-Id: I3cc58cf15f62008e83cfc7ddb095d07ab216b82a Issue-ID: SDC-2583 Signed-off-by: andre.schmid --- .../core/utilities/file/FileContentHandler.java | 102 ++++++++------------- 1 file changed, 40 insertions(+), 62 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java index c96ceeb46a..cc13879b96 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java @@ -21,14 +21,12 @@ package org.openecomp.core.utilities.file; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.Set; -import java.util.function.Function; - +import java.util.stream.Collectors; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; public class FileContentHandler { @@ -36,13 +34,12 @@ public class FileContentHandler { private Map files = new HashMap<>(); /** - * Gets file content. + * Gets file content as stream. * * @param fileName the file name - * @return the file content + * @return if the file was found, its content as stream, otherwise {@code null}. */ - public InputStream getFileContent(String fileName) { - + public InputStream getFileContentAsStream(final String fileName) { byte[] content = files.get(fileName); if (content == null || content.length == 0) { return null; @@ -51,89 +48,70 @@ public class FileContentHandler { return new ByteArrayInputStream(content); } - /** - * Applies a business logic to a file's content while taking care of all retrieval logic. - * - * @param fileName name of a file inside this content handler. - * @param processor the business logic to work on the file's input stream, which may not be set - * (check the {@link Optional} if no such file can be found - * @param return type, may be {@link java.lang.Void} - * @return result produced by the processor - */ - public T processFileContent(String fileName, Function, T> processor) { + public byte[] getFileContent(final String fileName) { + return files.get(fileName); + } - // do not throw IOException to mimic the existing uses of getFileContent() - try (InputStream contentInputStream = getFileContent(fileName)) { - return processor.apply(Optional.ofNullable(contentInputStream)); - } catch (IOException e) { - throw new ProcessingException("Failed to process file: " + fileName, e); - } + public boolean isFolder(final String fileName) { + return files.get(fileName) == null; } - public void addFile(String fileName, byte[] content) { - files.put(fileName, content); + public boolean isFile(final String fileName) { + return files.get(fileName) != null; } - public void addFile(String fileName, InputStream is) { + public void addFolder(final String folder) { + files.put(folder, null); + } + + public void addFile(final String fileName, final byte[] content) { + files.put(fileName, content == null ? new byte[0] : content); + } + public void addFile(final String fileName, final InputStream is) { files.put(fileName, FileUtils.toByteArray(is)); } public Map getFiles() { - return files; + return files.entrySet().stream().filter(entry -> entry.getValue() != null) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } - public void setFiles(Map files) { - this.files = files; + public void setFiles(final Map files) { + addAll(files); } - public void setFiles(FileContentHandler extFiles) { - extFiles.getFileList().forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName))); + public Set getFileList() { + return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet()); } - public Set getFileList() { - return files.keySet(); + public Set getFolderList() { + return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet()); } - public void putAll(Map files) { - this.files = files; + public void addAll(final FileContentHandler fileContentHandlerOther) { + if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) { + fileContentHandlerOther.getFolderList().forEach(this::addFolder); + } + addAll(fileContentHandlerOther.getFiles()); } - public void addAll(FileContentHandler other) { - this.files.putAll(other.files); + private void addAll(final Map files) { + if (!MapUtils.isEmpty(files)) { + files.forEach(this::addFile); + } } public boolean isEmpty() { return MapUtils.isEmpty(this.files); } - public void remove(String fileName) { - files.remove(fileName); + public byte[] remove(final String fileName) { + return files.remove(fileName); } - public boolean containsFile(String fileName) { + public boolean containsFile(final String fileName) { return files.containsKey(fileName); } - /** - * An application-specific runtime exception - */ - private static class ProcessingException extends RuntimeException { - - public ProcessingException() { - super(); - } - - public ProcessingException(String message) { - super(message); - } - - public ProcessingException(Throwable cause) { - super(cause); - } - - public ProcessingException(String msg, Throwable cause) { - super(msg, cause); - } - } } -- cgit 1.2.3-korg