Skip to content

Commit e21d843

Browse files
committed
Apply Nullability for Zip module
Related to: #10083 * Add `@org.jspecify.annotations.NullMarked` into all the `package-info.java` of Zip module packages * Fix reported problems
1 parent c4c9627 commit e21d843

File tree

7 files changed

+28
-19
lines changed

7 files changed

+28
-19
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Provides parser classes to provide Xml namespace support for the Zip components.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.zip.config.xml;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Root package of the Zip Module.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.zip;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Classes to support Splitter pattern for Zip.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.zip.splitter;

spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/AbstractZipTransformer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.File;
2020
import java.nio.charset.Charset;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.integration.transformer.AbstractTransformer;
2325
import org.springframework.messaging.Message;
2426
import org.springframework.util.Assert;
@@ -93,7 +95,7 @@ protected void onInit() {
9395
* @param message the message and its payload must not be null.
9496
*/
9597
@Override
96-
protected Object doTransform(Message<?> message) {
98+
protected @Nullable Object doTransform(Message<?> message) {
9799
return doZipTransform(message);
98100
}
99101

@@ -103,6 +105,6 @@ protected Object doTransform(Message<?> message) {
103105
* @param message The message will never be null.
104106
* @return The result of the Zip transformation.
105107
*/
106-
protected abstract Object doZipTransform(Message<?> message);
108+
protected abstract @Nullable Object doZipTransform(Message<?> message);
107109

108110
}

spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
import java.io.UncheckedIOException;
2626
import java.util.SortedMap;
2727
import java.util.TreeMap;
28+
import java.util.UUID;
2829
import java.util.zip.ZipEntry;
2930

3031
import org.apache.commons.io.IOUtils;
32+
import org.jspecify.annotations.Nullable;
3133
import org.zeroturnaround.zip.ZipEntryCallback;
3234
import org.zeroturnaround.zip.ZipException;
3335
import org.zeroturnaround.zip.ZipUtil;
3436

3537
import org.springframework.messaging.Message;
3638
import org.springframework.messaging.MessagingException;
39+
import org.springframework.util.Assert;
3740

3841
/**
3942
* Transformer implementation that applies an UnZip transformation to the message
@@ -68,7 +71,7 @@ public void setExpectSingleResult(boolean expectSingleResult) {
6871
}
6972

7073
@Override
71-
protected Object doZipTransform(final Message<?> message) {
74+
protected @Nullable Object doZipTransform(final Message<?> message) {
7275
Object payload = message.getPayload();
7376
Object unzippedData;
7477

@@ -100,6 +103,10 @@ else if (payload instanceof byte[] bytes) {
100103

101104
final SortedMap<String, Object> uncompressedData = new TreeMap<>();
102105

106+
UUID messageId = message.getHeaders().getId();
107+
Assert.state(messageId != null,
108+
() -> "The 'MessageHeaders.ID' must be provided in request message: " + message);
109+
103110
ZipUtil.iterate(inputStream, new ZipEntryCallback() {
104111

105112
@Override
@@ -115,7 +122,7 @@ public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws I
115122
zipEntryName, zipEntryTime, zipEntryCompressedSize, type));
116123

117124
if (ZipResultType.FILE.equals(zipResultType)) {
118-
final File destinationFile = checkPath(message, zipEntryName);
125+
final File destinationFile = checkPath(messageId, zipEntryName);
119126

120127
if (zipEntry.isDirectory()) {
121128
destinationFile.mkdirs(); //NOSONAR false positive
@@ -128,7 +135,7 @@ public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws I
128135
}
129136
else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) {
130137
if (!zipEntry.isDirectory()) {
131-
checkPath(message, zipEntryName);
138+
checkPath(messageId, zipEntryName);
132139
byte[] data = IOUtils.toByteArray(zipEntryInputStream);
133140
uncompressedData.put(zipEntryName, data);
134141
}
@@ -138,8 +145,8 @@ else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) {
138145
}
139146
}
140147

141-
public File checkPath(final Message<?> message, final String zipEntryName) throws IOException {
142-
File tempDir = new File(workDirectory, message.getHeaders().getId().toString()); // NOSONAR
148+
public File checkPath(UUID messageId, final String zipEntryName) throws IOException {
149+
File tempDir = new File(workDirectory, messageId.toString());
143150
tempDir.mkdirs(); //NOSONAR false positive
144151
final File destinationFile = new File(tempDir, zipEntryName);
145152

@@ -157,7 +164,7 @@ public File checkPath(final Message<?> message, final String zipEntryName) throw
157164
});
158165

159166
if (uncompressedData.isEmpty()) {
160-
logger.warn(() -> "No data unzipped from payload with message Id " + message.getHeaders().getId());
167+
logger.warn(() -> "No data unzipped from payload with message Id " + messageId);
161168
unzippedData = null;
162169
}
163170
else {
@@ -169,8 +176,7 @@ public File checkPath(final Message<?> message, final String zipEntryName) throw
169176
else {
170177
throw new MessagingException(message,
171178
String.format("The UnZip operation extracted %s "
172-
+ "result objects but expectSingleResult was 'true'.", uncompressedData
173-
.size()));
179+
+ "result objects but expectSingleResult was 'true'.", uncompressedData.size()));
174180
}
175181
}
176182
else {

spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/ZipTransformer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class ZipTransformer extends AbstractZipTransformer {
6060

6161
private boolean useFileAttributes = true;
6262

63+
@SuppressWarnings("NullAway.Init")
6364
private FileNameGenerator fileNameGenerator;
6465

6566
/**
@@ -214,22 +215,18 @@ private ZipEntrySource createZipEntrySource(Object item,
214215
if (item instanceof final File filePayload) {
215216
String fileName = useFileAttributes ? filePayload.getName() : zipEntryName;
216217

217-
if (((File) item).isDirectory()) {
218+
if (filePayload.isDirectory()) {
218219
throw new UnsupportedOperationException("Zipping of directories is not supported.");
219220
}
220221

221222
return new FileSource(fileName, filePayload);
222223

223224
}
224225
else if (item instanceof byte[] || item instanceof String) {
225-
byte[] bytesToCompress;
226-
227-
if (item instanceof String) {
228-
bytesToCompress = ((String) item).getBytes(this.charset);
229-
}
230-
else {
231-
bytesToCompress = (byte[]) item;
232-
}
226+
byte[] bytesToCompress =
227+
item instanceof String stringItem
228+
? stringItem.getBytes(this.charset)
229+
: (byte[]) item;
233230

234231
return new ByteSource(zipEntryName, bytesToCompress, lastModifiedDate.getTime());
235232
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Classes to support Transformer pattern for Zip.
33
*/
4+
@org.jspecify.annotations.NullMarked
45
package org.springframework.integration.zip.transformer;

0 commit comments

Comments
 (0)