Skip to content

Commit f0d2104

Browse files
committed
Fix new Sonar smells
1 parent f128a3f commit f0d2104

File tree

6 files changed

+158
-151
lines changed

6 files changed

+158
-151
lines changed

spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private boolean sendWithObservation(Message<?> message, long timeout) {
335335
DefaultMessageSenderObservationConvention.INSTANCE,
336336
() -> new MessageSenderContext(messageToSend, getComponentName()),
337337
this.observationRegistry)
338-
.observe(() -> sendInternal(messageToSend, timeout));
338+
.observe(() -> sendInternal(messageToSend, timeout)); // NOSONAR - never null
339339
}
340340

341341
private boolean sendWithMetrics(Message<?> message, long timeout) {

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,13 @@ private void registerNullChannel() {
174174
BeanDefinition nullChannelDefinition = null;
175175
BeanFactory beanFactoryToUse = this.beanFactory;
176176
do {
177-
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory listable) {
178-
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
179-
nullChannelDefinition =
180-
listable.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
181-
}
177+
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory listable &&
178+
listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
179+
180+
nullChannelDefinition = listable.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
182181
}
183-
if (beanFactoryToUse instanceof HierarchicalBeanFactory) {
184-
beanFactoryToUse = ((HierarchicalBeanFactory) beanFactoryToUse).getParentBeanFactory();
182+
if (beanFactoryToUse instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {
183+
beanFactoryToUse = hierarchicalBeanFactory.getParentBeanFactory();
185184
}
186185
}
187186
while (nullChannelDefinition == null);
@@ -455,7 +454,7 @@ private static BeanDefinitionBuilder createMessageHandlerMethodFactoryBeanDefini
455454
return BeanDefinitionBuilder.genericBeanDefinition(IntegrationMessageHandlerMethodFactory.class)
456455
.addConstructorArgValue(listCapable)
457456
.addPropertyReference("messageConverter",
458-
IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
457+
IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
459458
}
460459

461460
}

spring-integration-core/src/main/java/org/springframework/integration/util/CheckedFunction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ default Function<T, R> unchecked() {
3939
return apply(t1);
4040
}
4141
catch (Throwable t) { // NOSONAR
42-
if (t instanceof RuntimeException runtimeException) {
42+
if (t instanceof RuntimeException runtimeException) { // NOSONAR
4343
throw runtimeException;
4444
}
45-
else if (t instanceof Error error) {
45+
else if (t instanceof Error error) { // NOSONAR
4646
throw error;
4747
}
4848
else {

spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,13 @@ private Destination getReplyDestination(jakarta.jms.Message request, Session ses
467467
@Nullable
468468
private Destination resolveReplyTo(jakarta.jms.Message request, Session session) throws JMSException {
469469
Destination replyTo = request.getJMSReplyTo();
470-
if (replyTo == null) {
471-
if (this.replyToExpression != null) {
472-
Object replyToValue = this.replyToExpression.getValue(this.evaluationContext, request);
473-
if (replyToValue instanceof Destination destination) {
474-
return destination;
475-
}
476-
else if (replyToValue instanceof String destinationName) {
477-
return this.destinationResolver.resolveDestinationName(session, destinationName, false);
478-
}
470+
if (replyTo == null && this.replyToExpression != null) {
471+
Object replyToValue = this.replyToExpression.getValue(this.evaluationContext, request);
472+
if (replyToValue instanceof Destination destination) {
473+
return destination;
474+
}
475+
else if (replyToValue instanceof String destinationName) {
476+
return this.destinationResolver.resolveDestinationName(session, destinationName, false);
479477
}
480478
}
481479
return replyTo;

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

+95-98
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
2121
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
2223
import java.io.IOException;
2324
import java.io.InputStream;
25+
import java.io.UncheckedIOException;
2426
import java.util.SortedMap;
2527
import java.util.TreeMap;
2628
import java.util.zip.ZipEntry;
@@ -31,7 +33,6 @@
3133
import org.zeroturnaround.zip.ZipUtil;
3234

3335
import org.springframework.messaging.Message;
34-
import org.springframework.messaging.MessageHandlingException;
3536
import org.springframework.messaging.MessagingException;
3637

3738
/**
@@ -67,138 +68,134 @@ public void setExpectSingleResult(boolean expectSingleResult) {
6768

6869
@Override
6970
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;
7573

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;
8675

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());
9481
}
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());
9885
}
9986

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+
}
10199

102-
ZipUtil.iterate(inputStream, new ZipEntryCallback() {
100+
final SortedMap<String, Object> uncompressedData = new TreeMap<>();
103101

104-
@Override
105-
public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException {
102+
ZipUtil.iterate(inputStream, new ZipEntryCallback() {
106103

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 {
111106

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";
115111

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));
118115

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
134121
}
135122
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);
137133
}
138134
}
135+
else {
136+
throw new IllegalStateException("Unsupported zipResultType: " + zipResultType);
137+
}
138+
}
139139

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);
144144

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)) {
150150

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);
155153
}
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;
161155
}
162-
else {
156+
});
163157

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();
174167
}
175168
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()));
177173
}
178-
179174
}
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;
186177
}
178+
187179
}
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 + "'");
190184
}
191-
return unzippedData;
192185
}
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);
195191
}
192+
return unzippedData;
196193
}
197194

198195
private static void mkDirOfAncestorDirectories(File destinationFile) {
199196
File parentDirectory = destinationFile.getParentFile();
200197
if (parentDirectory != null) {
201-
parentDirectory.mkdirs();
198+
parentDirectory.mkdirs(); // NOSONAR
202199
}
203200
}
204201

0 commit comments

Comments
 (0)