|
14 | 14 | */ |
15 | 15 | package software.amazon.cloudformation.resource; |
16 | 16 |
|
| 17 | +import com.amazonaws.util.IOUtils; |
17 | 18 | import com.fasterxml.jackson.annotation.JsonInclude; |
18 | 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
19 | 20 | import com.fasterxml.jackson.core.type.TypeReference; |
|
22 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
23 | 24 | import com.fasterxml.jackson.databind.SerializationFeature; |
24 | 25 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
25 | 28 | import java.io.IOException; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.zip.GZIPInputStream; |
| 33 | +import java.util.zip.GZIPOutputStream; |
| 34 | +import org.apache.commons.codec.binary.Base64; |
26 | 35 | import software.amazon.cloudformation.proxy.aws.AWSServiceSerdeModule; |
27 | 36 |
|
28 | 37 | public class Serializer { |
29 | 38 |
|
| 39 | + public static final String COMPRESSED = "compressed"; |
30 | 40 | private static final ObjectMapper OBJECT_MAPPER; |
31 | | - |
32 | 41 | private static final ObjectMapper STRICT_OBJECT_MAPPER; |
| 42 | + private static final TypeReference<Map<String, String>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, String>>() { |
| 43 | + }; |
33 | 44 |
|
34 | 45 | /** |
35 | 46 | * Configures the specified ObjectMapper with the (de)serialization behaviours |
@@ -76,10 +87,37 @@ public <T> String serialize(final T modelObject) throws JsonProcessingException |
76 | 87 | return OBJECT_MAPPER.writeValueAsString(modelObject); |
77 | 88 | } |
78 | 89 |
|
| 90 | + public <T> String compress(final String modelInput) throws IOException { |
| 91 | + final Map<String, String> map = new HashMap<>(); |
| 92 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 93 | + GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream); |
| 94 | + gzip.write(modelInput.getBytes(StandardCharsets.UTF_8)); |
| 95 | + gzip.close(); |
| 96 | + map.put(COMPRESSED, Base64.encodeBase64String(byteArrayOutputStream.toByteArray())); |
| 97 | + return OBJECT_MAPPER.writeValueAsString(map); |
| 98 | + } |
| 99 | + |
79 | 100 | public <T> T deserialize(final String s, final TypeReference<T> reference) throws IOException { |
80 | 101 | return OBJECT_MAPPER.readValue(s, reference); |
81 | 102 | } |
82 | 103 |
|
| 104 | + public String decompress(final String s) { |
| 105 | + try { |
| 106 | + final Map<String, String> map = deserialize(s, MAP_TYPE_REFERENCE); |
| 107 | + |
| 108 | + if (!map.containsKey(COMPRESSED)) { |
| 109 | + return s; |
| 110 | + } |
| 111 | + |
| 112 | + final byte[] bytes = Base64.decodeBase64(map.get(COMPRESSED)); |
| 113 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); |
| 114 | + GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream); |
| 115 | + return new String(IOUtils.toByteArray(gzipInputStream), StandardCharsets.UTF_8); |
| 116 | + } catch (IOException e) { |
| 117 | + return s; |
| 118 | + } |
| 119 | + } |
| 120 | + |
83 | 121 | public <T> T deserializeStrict(final String s, final TypeReference<T> reference) throws IOException { |
84 | 122 | return STRICT_OBJECT_MAPPER.readValue(s, reference); |
85 | 123 | } |
|
0 commit comments