Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / ZipWriter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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 package org.openecomp.sdc.be.tosca;
21
22 import io.vavr.control.Try;
23 import java.util.function.Function;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipOutputStream;
26
27 /**
28  * ZipWriter abstracts the Zip file writing logic.
29  */
30 public interface ZipWriter {
31
32     /**
33      * Builds a ZipWriter that outputs the data on a {@link java.util.zip.ZipOutputStream}
34      *
35      * @param zos the target {@link java.util.zip.ZipOutputStream}
36      */
37     static ZipWriter live(ZipOutputStream zos) {
38         return (entryName, payload) -> Try.of(() -> {
39             zos.putNextEntry(new ZipEntry(entryName));
40             zos.write(payload);
41             // We can return null as a Void is expected;
42             return null;
43         });
44     }
45
46     /**
47      * Writes an entry provided with its name and its payload
48      *
49      * @param entryName The entry's name to use in the zip file
50      * @param payload   The payload to write for this entry
51      */
52     Try<Void> write(String entryName, byte[] payload);
53
54     /**
55      * Writes an entry provided with its name and its payload
56      *
57      * @param entryName The entry's name to use in the zip file
58      * @param payload   The payload to write for this entry
59      */
60     default Try<Void> write(String entryName, String payload) {
61         return write(entryName, payload.getBytes());
62     }
63
64     /**
65      * Alias for {@link org.openecomp.sdc.be.tosca.ZipWriter}
66      *
67      * @param entryName The entry's name to use in the zip file
68      */
69     default Function<String, Try<Void>> writeString(String entryName) {
70         return payload -> write(entryName, payload.getBytes());
71     }
72
73     default Function<byte[], Try<Void>> write(String entryName) {
74         return payload -> write(entryName, payload);
75     }
76 }