|
19 | 19 | import java.io.ByteArrayInputStream;
|
20 | 20 | import java.io.File;
|
21 | 21 | import java.io.FileInputStream;
|
| 22 | +import java.io.FileNotFoundException; |
22 | 23 | import java.io.IOException;
|
23 | 24 | import java.io.InputStream;
|
| 25 | +import java.io.UncheckedIOException; |
24 | 26 | import java.util.SortedMap;
|
25 | 27 | import java.util.TreeMap;
|
26 | 28 | import java.util.zip.ZipEntry;
|
|
31 | 33 | import org.zeroturnaround.zip.ZipUtil;
|
32 | 34 |
|
33 | 35 | import org.springframework.messaging.Message;
|
34 |
| -import org.springframework.messaging.MessageHandlingException; |
35 | 36 | import org.springframework.messaging.MessagingException;
|
36 | 37 |
|
37 | 38 | /**
|
@@ -67,138 +68,134 @@ public void setExpectSingleResult(boolean expectSingleResult) {
|
67 | 68 |
|
68 | 69 | @Override
|
69 | 70 | protected Object doZipTransform(final Message<?> message) {
|
70 |
| - try { |
71 |
| - Object payload = message.getPayload(); |
72 |
| - Object unzippedData; |
73 |
| - |
74 |
| - InputStream inputStream = null; |
| 71 | + Object payload = message.getPayload(); |
| 72 | + Object unzippedData; |
75 | 73 |
|
76 |
| - try { |
77 |
| - if (payload instanceof final File filePayload) { |
78 |
| - if (filePayload.isDirectory()) { |
79 |
| - throw new UnsupportedOperationException("Cannot unzip a directory: " + |
80 |
| - filePayload.getAbsolutePath()); |
81 |
| - } |
82 |
| - |
83 |
| - if (!SpringZipUtils.isValid(filePayload)) { |
84 |
| - throw new IllegalStateException("Not a zip file: " + filePayload.getAbsolutePath()); |
85 |
| - } |
| 74 | + InputStream inputStream = null; |
86 | 75 |
|
87 |
| - inputStream = new FileInputStream(filePayload); |
88 |
| - } |
89 |
| - else if (payload instanceof InputStream) { |
90 |
| - inputStream = (InputStream) payload; |
91 |
| - } |
92 |
| - else if (payload instanceof byte[]) { |
93 |
| - inputStream = new ByteArrayInputStream((byte[]) payload); |
| 76 | + try { |
| 77 | + if (payload instanceof final File filePayload) { |
| 78 | + if (filePayload.isDirectory()) { |
| 79 | + throw new UnsupportedOperationException("Cannot unzip a directory: " + |
| 80 | + filePayload.getAbsolutePath()); |
94 | 81 | }
|
95 |
| - else { |
96 |
| - throw new IllegalArgumentException("Unsupported payload type '" + payload.getClass().getSimpleName() |
97 |
| - + "'. The only supported payload types are java.io.File, byte[] and java.io.InputStream"); |
| 82 | + |
| 83 | + if (!SpringZipUtils.isValid(filePayload)) { |
| 84 | + throw new IllegalStateException("Not a zip file: " + filePayload.getAbsolutePath()); |
98 | 85 | }
|
99 | 86 |
|
100 |
| - final SortedMap<String, Object> uncompressedData = new TreeMap<>(); |
| 87 | + inputStream = new FileInputStream(filePayload); |
| 88 | + } |
| 89 | + else if (payload instanceof InputStream) { |
| 90 | + inputStream = (InputStream) payload; |
| 91 | + } |
| 92 | + else if (payload instanceof byte[]) { |
| 93 | + inputStream = new ByteArrayInputStream((byte[]) payload); |
| 94 | + } |
| 95 | + else { |
| 96 | + throw new IllegalArgumentException("Unsupported payload type '" + payload.getClass().getSimpleName() |
| 97 | + + "'. The only supported payload types are java.io.File, byte[] and java.io.InputStream"); |
| 98 | + } |
101 | 99 |
|
102 |
| - ZipUtil.iterate(inputStream, new ZipEntryCallback() { |
| 100 | + final SortedMap<String, Object> uncompressedData = new TreeMap<>(); |
103 | 101 |
|
104 |
| - @Override |
105 |
| - public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException { |
| 102 | + ZipUtil.iterate(inputStream, new ZipEntryCallback() { |
106 | 103 |
|
107 |
| - final String zipEntryName = zipEntry.getName(); |
108 |
| - final long zipEntryTime = zipEntry.getTime(); |
109 |
| - final long zipEntryCompressedSize = zipEntry.getCompressedSize(); |
110 |
| - final String type = zipEntry.isDirectory() ? "directory" : "file"; |
| 104 | + @Override |
| 105 | + public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException { |
111 | 106 |
|
112 |
| - logger.info(() -> String.format("Unpacking Zip Entry - Name: '%s',Time: '%s', " + |
113 |
| - "Compressed Size: '%s', Type: '%s'", |
114 |
| - zipEntryName, zipEntryTime, zipEntryCompressedSize, type)); |
| 107 | + final String zipEntryName = zipEntry.getName(); |
| 108 | + final long zipEntryTime = zipEntry.getTime(); |
| 109 | + final long zipEntryCompressedSize = zipEntry.getCompressedSize(); |
| 110 | + final String type = zipEntry.isDirectory() ? "directory" : "file"; |
115 | 111 |
|
116 |
| - if (ZipResultType.FILE.equals(zipResultType)) { |
117 |
| - final File destinationFile = checkPath(message, zipEntryName); |
| 112 | + logger.info(() -> String.format("Unpacking Zip Entry - Name: '%s',Time: '%s', " + |
| 113 | + "Compressed Size: '%s', Type: '%s'", |
| 114 | + zipEntryName, zipEntryTime, zipEntryCompressedSize, type)); |
118 | 115 |
|
119 |
| - if (zipEntry.isDirectory()) { |
120 |
| - destinationFile.mkdirs(); //NOSONAR false positive |
121 |
| - } |
122 |
| - else { |
123 |
| - mkDirOfAncestorDirectories(destinationFile); |
124 |
| - SpringZipUtils.copy(zipEntryInputStream, destinationFile); |
125 |
| - uncompressedData.put(zipEntryName, destinationFile); |
126 |
| - } |
127 |
| - } |
128 |
| - else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) { |
129 |
| - if (!zipEntry.isDirectory()) { |
130 |
| - checkPath(message, zipEntryName); |
131 |
| - byte[] data = IOUtils.toByteArray(zipEntryInputStream); |
132 |
| - uncompressedData.put(zipEntryName, data); |
133 |
| - } |
| 116 | + if (ZipResultType.FILE.equals(zipResultType)) { |
| 117 | + final File destinationFile = checkPath(message, zipEntryName); |
| 118 | + |
| 119 | + if (zipEntry.isDirectory()) { |
| 120 | + destinationFile.mkdirs(); //NOSONAR false positive |
134 | 121 | }
|
135 | 122 | else {
|
136 |
| - throw new IllegalStateException("Unsupported zipResultType: " + zipResultType); |
| 123 | + mkDirOfAncestorDirectories(destinationFile); |
| 124 | + SpringZipUtils.copy(zipEntryInputStream, destinationFile); |
| 125 | + uncompressedData.put(zipEntryName, destinationFile); |
| 126 | + } |
| 127 | + } |
| 128 | + else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) { |
| 129 | + if (!zipEntry.isDirectory()) { |
| 130 | + checkPath(message, zipEntryName); |
| 131 | + byte[] data = IOUtils.toByteArray(zipEntryInputStream); |
| 132 | + uncompressedData.put(zipEntryName, data); |
137 | 133 | }
|
138 | 134 | }
|
| 135 | + else { |
| 136 | + throw new IllegalStateException("Unsupported zipResultType: " + zipResultType); |
| 137 | + } |
| 138 | + } |
139 | 139 |
|
140 |
| - public File checkPath(final Message<?> message, final String zipEntryName) throws IOException { |
141 |
| - final File tempDir = new File(workDirectory, message.getHeaders().getId().toString()); |
142 |
| - tempDir.mkdirs(); //NOSONAR false positive |
143 |
| - final File destinationFile = new File(tempDir, zipEntryName); |
| 140 | + public File checkPath(final Message<?> message, final String zipEntryName) throws IOException { |
| 141 | + File tempDir = new File(workDirectory, message.getHeaders().getId().toString()); // NOSONAR |
| 142 | + tempDir.mkdirs(); //NOSONAR false positive |
| 143 | + final File destinationFile = new File(tempDir, zipEntryName); |
144 | 144 |
|
145 |
| - /* If we see the relative traversal string of ".." we need to make sure |
146 |
| - * that the outputdir + name doesn't leave the outputdir. |
147 |
| - */ |
148 |
| - if (!destinationFile.getCanonicalPath() |
149 |
| - .startsWith(tempDir.getCanonicalPath() + File.separator)) { |
| 145 | + /* If we see the relative traversal string of ".." we need to make sure |
| 146 | + * that the outputdir + name doesn't leave the outputdir. |
| 147 | + */ |
| 148 | + if (!destinationFile.getCanonicalPath() |
| 149 | + .startsWith(tempDir.getCanonicalPath() + File.separator)) { |
150 | 150 |
|
151 |
| - throw new ZipException("The file " + zipEntryName + |
152 |
| - " is trying to leave the target output directory of " + workDirectory); |
153 |
| - } |
154 |
| - return destinationFile; |
| 151 | + throw new ZipException("The file " + zipEntryName + |
| 152 | + " is trying to leave the target output directory of " + workDirectory); |
155 | 153 | }
|
156 |
| - }); |
157 |
| - |
158 |
| - if (uncompressedData.isEmpty()) { |
159 |
| - logger.warn(() -> "No data unzipped from payload with message Id " + message.getHeaders().getId()); |
160 |
| - unzippedData = null; |
| 154 | + return destinationFile; |
161 | 155 | }
|
162 |
| - else { |
| 156 | + }); |
163 | 157 |
|
164 |
| - if (this.expectSingleResult) { |
165 |
| - if (uncompressedData.size() == 1) { |
166 |
| - unzippedData = uncompressedData.values().iterator().next(); |
167 |
| - } |
168 |
| - else { |
169 |
| - throw new MessagingException(message, |
170 |
| - String.format("The UnZip operation extracted %s " |
171 |
| - + "result objects but expectSingleResult was 'true'.", uncompressedData |
172 |
| - .size())); |
173 |
| - } |
| 158 | + if (uncompressedData.isEmpty()) { |
| 159 | + logger.warn(() -> "No data unzipped from payload with message Id " + message.getHeaders().getId()); |
| 160 | + unzippedData = null; |
| 161 | + } |
| 162 | + else { |
| 163 | + |
| 164 | + if (this.expectSingleResult) { |
| 165 | + if (uncompressedData.size() == 1) { |
| 166 | + unzippedData = uncompressedData.values().iterator().next(); |
174 | 167 | }
|
175 | 168 | else {
|
176 |
| - unzippedData = uncompressedData; |
| 169 | + throw new MessagingException(message, |
| 170 | + String.format("The UnZip operation extracted %s " |
| 171 | + + "result objects but expectSingleResult was 'true'.", uncompressedData |
| 172 | + .size())); |
177 | 173 | }
|
178 |
| - |
179 | 174 | }
|
180 |
| - |
181 |
| - IOUtils.closeQuietly(inputStream); |
182 |
| - if (payload instanceof final File filePayload && this.deleteFiles) { |
183 |
| - if (!filePayload.delete() && logger.isWarnEnabled()) { |
184 |
| - logger.warn(() -> "failed to delete File '" + filePayload + "'"); |
185 |
| - } |
| 175 | + else { |
| 176 | + unzippedData = uncompressedData; |
186 | 177 | }
|
| 178 | + |
187 | 179 | }
|
188 |
| - finally { |
189 |
| - IOUtils.closeQuietly(inputStream); |
| 180 | + |
| 181 | + IOUtils.closeQuietly(inputStream); |
| 182 | + if (payload instanceof File filePayload && this.deleteFiles && !filePayload.delete()) { |
| 183 | + logger.warn(() -> "failed to delete File '" + filePayload + "'"); |
190 | 184 | }
|
191 |
| - return unzippedData; |
192 | 185 | }
|
193 |
| - catch (Exception e) { |
194 |
| - throw new MessageHandlingException(message, "Failed to apply Zip transformation.", e); |
| 186 | + catch (FileNotFoundException ex) { |
| 187 | + throw new UncheckedIOException(ex); |
| 188 | + } |
| 189 | + finally { |
| 190 | + IOUtils.closeQuietly(inputStream); |
195 | 191 | }
|
| 192 | + return unzippedData; |
196 | 193 | }
|
197 | 194 |
|
198 | 195 | private static void mkDirOfAncestorDirectories(File destinationFile) {
|
199 | 196 | File parentDirectory = destinationFile.getParentFile();
|
200 | 197 | if (parentDirectory != null) {
|
201 |
| - parentDirectory.mkdirs(); |
| 198 | + parentDirectory.mkdirs(); // NOSONAR |
202 | 199 | }
|
203 | 200 | }
|
204 | 201 |
|
|
0 commit comments