Fix new Sonar smells
This commit is contained in:
@@ -335,7 +335,7 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport
|
||||
DefaultMessageSenderObservationConvention.INSTANCE,
|
||||
() -> new MessageSenderContext(messageToSend, getComponentName()),
|
||||
this.observationRegistry)
|
||||
.observe(() -> sendInternal(messageToSend, timeout));
|
||||
.observe(() -> sendInternal(messageToSend, timeout)); // NOSONAR - never null
|
||||
}
|
||||
|
||||
private boolean sendWithMetrics(Message<?> message, long timeout) {
|
||||
|
||||
@@ -174,14 +174,13 @@ public class DefaultConfiguringBeanFactoryPostProcessor
|
||||
BeanDefinition nullChannelDefinition = null;
|
||||
BeanFactory beanFactoryToUse = this.beanFactory;
|
||||
do {
|
||||
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory listable) {
|
||||
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
|
||||
nullChannelDefinition =
|
||||
listable.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
|
||||
}
|
||||
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory listable &&
|
||||
listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
|
||||
|
||||
nullChannelDefinition = listable.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
|
||||
}
|
||||
if (beanFactoryToUse instanceof HierarchicalBeanFactory) {
|
||||
beanFactoryToUse = ((HierarchicalBeanFactory) beanFactoryToUse).getParentBeanFactory();
|
||||
if (beanFactoryToUse instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {
|
||||
beanFactoryToUse = hierarchicalBeanFactory.getParentBeanFactory();
|
||||
}
|
||||
}
|
||||
while (nullChannelDefinition == null);
|
||||
@@ -455,7 +454,7 @@ public class DefaultConfiguringBeanFactoryPostProcessor
|
||||
return BeanDefinitionBuilder.genericBeanDefinition(IntegrationMessageHandlerMethodFactory.class)
|
||||
.addConstructorArgValue(listCapable)
|
||||
.addPropertyReference("messageConverter",
|
||||
IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
|
||||
IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,10 +39,10 @@ public interface CheckedFunction<T, R> {
|
||||
return apply(t1);
|
||||
}
|
||||
catch (Throwable t) { // NOSONAR
|
||||
if (t instanceof RuntimeException runtimeException) {
|
||||
if (t instanceof RuntimeException runtimeException) { // NOSONAR
|
||||
throw runtimeException;
|
||||
}
|
||||
else if (t instanceof Error error) {
|
||||
else if (t instanceof Error error) { // NOSONAR
|
||||
throw error;
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -467,15 +467,13 @@ public class ChannelPublishingJmsMessageListener
|
||||
@Nullable
|
||||
private Destination resolveReplyTo(jakarta.jms.Message request, Session session) throws JMSException {
|
||||
Destination replyTo = request.getJMSReplyTo();
|
||||
if (replyTo == null) {
|
||||
if (this.replyToExpression != null) {
|
||||
Object replyToValue = this.replyToExpression.getValue(this.evaluationContext, request);
|
||||
if (replyToValue instanceof Destination destination) {
|
||||
return destination;
|
||||
}
|
||||
else if (replyToValue instanceof String destinationName) {
|
||||
return this.destinationResolver.resolveDestinationName(session, destinationName, false);
|
||||
}
|
||||
if (replyTo == null && this.replyToExpression != null) {
|
||||
Object replyToValue = this.replyToExpression.getValue(this.evaluationContext, request);
|
||||
if (replyToValue instanceof Destination destination) {
|
||||
return destination;
|
||||
}
|
||||
else if (replyToValue instanceof String destinationName) {
|
||||
return this.destinationResolver.resolveDestinationName(session, destinationName, false);
|
||||
}
|
||||
}
|
||||
return replyTo;
|
||||
|
||||
@@ -19,8 +19,10 @@ package org.springframework.integration.zip.transformer;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.zip.ZipEntry;
|
||||
@@ -31,7 +33,6 @@ import org.zeroturnaround.zip.ZipException;
|
||||
import org.zeroturnaround.zip.ZipUtil;
|
||||
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
|
||||
/**
|
||||
@@ -67,138 +68,134 @@ public class UnZipTransformer extends AbstractZipTransformer {
|
||||
|
||||
@Override
|
||||
protected Object doZipTransform(final Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
Object unzippedData;
|
||||
|
||||
InputStream inputStream = null;
|
||||
|
||||
try {
|
||||
Object payload = message.getPayload();
|
||||
Object unzippedData;
|
||||
|
||||
InputStream inputStream = null;
|
||||
|
||||
try {
|
||||
if (payload instanceof final File filePayload) {
|
||||
if (filePayload.isDirectory()) {
|
||||
throw new UnsupportedOperationException("Cannot unzip a directory: " +
|
||||
filePayload.getAbsolutePath());
|
||||
}
|
||||
|
||||
if (!SpringZipUtils.isValid(filePayload)) {
|
||||
throw new IllegalStateException("Not a zip file: " + filePayload.getAbsolutePath());
|
||||
}
|
||||
|
||||
inputStream = new FileInputStream(filePayload);
|
||||
}
|
||||
else if (payload instanceof InputStream) {
|
||||
inputStream = (InputStream) payload;
|
||||
}
|
||||
else if (payload instanceof byte[]) {
|
||||
inputStream = new ByteArrayInputStream((byte[]) payload);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unsupported payload type '" + payload.getClass().getSimpleName()
|
||||
+ "'. The only supported payload types are java.io.File, byte[] and java.io.InputStream");
|
||||
if (payload instanceof final File filePayload) {
|
||||
if (filePayload.isDirectory()) {
|
||||
throw new UnsupportedOperationException("Cannot unzip a directory: " +
|
||||
filePayload.getAbsolutePath());
|
||||
}
|
||||
|
||||
final SortedMap<String, Object> uncompressedData = new TreeMap<>();
|
||||
if (!SpringZipUtils.isValid(filePayload)) {
|
||||
throw new IllegalStateException("Not a zip file: " + filePayload.getAbsolutePath());
|
||||
}
|
||||
|
||||
ZipUtil.iterate(inputStream, new ZipEntryCallback() {
|
||||
inputStream = new FileInputStream(filePayload);
|
||||
}
|
||||
else if (payload instanceof InputStream) {
|
||||
inputStream = (InputStream) payload;
|
||||
}
|
||||
else if (payload instanceof byte[]) {
|
||||
inputStream = new ByteArrayInputStream((byte[]) payload);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unsupported payload type '" + payload.getClass().getSimpleName()
|
||||
+ "'. The only supported payload types are java.io.File, byte[] and java.io.InputStream");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException {
|
||||
final SortedMap<String, Object> uncompressedData = new TreeMap<>();
|
||||
|
||||
final String zipEntryName = zipEntry.getName();
|
||||
final long zipEntryTime = zipEntry.getTime();
|
||||
final long zipEntryCompressedSize = zipEntry.getCompressedSize();
|
||||
final String type = zipEntry.isDirectory() ? "directory" : "file";
|
||||
ZipUtil.iterate(inputStream, new ZipEntryCallback() {
|
||||
|
||||
logger.info(() -> String.format("Unpacking Zip Entry - Name: '%s',Time: '%s', " +
|
||||
"Compressed Size: '%s', Type: '%s'",
|
||||
zipEntryName, zipEntryTime, zipEntryCompressedSize, type));
|
||||
@Override
|
||||
public void process(InputStream zipEntryInputStream, ZipEntry zipEntry) throws IOException {
|
||||
|
||||
if (ZipResultType.FILE.equals(zipResultType)) {
|
||||
final File destinationFile = checkPath(message, zipEntryName);
|
||||
final String zipEntryName = zipEntry.getName();
|
||||
final long zipEntryTime = zipEntry.getTime();
|
||||
final long zipEntryCompressedSize = zipEntry.getCompressedSize();
|
||||
final String type = zipEntry.isDirectory() ? "directory" : "file";
|
||||
|
||||
if (zipEntry.isDirectory()) {
|
||||
destinationFile.mkdirs(); //NOSONAR false positive
|
||||
}
|
||||
else {
|
||||
mkDirOfAncestorDirectories(destinationFile);
|
||||
SpringZipUtils.copy(zipEntryInputStream, destinationFile);
|
||||
uncompressedData.put(zipEntryName, destinationFile);
|
||||
}
|
||||
}
|
||||
else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) {
|
||||
if (!zipEntry.isDirectory()) {
|
||||
checkPath(message, zipEntryName);
|
||||
byte[] data = IOUtils.toByteArray(zipEntryInputStream);
|
||||
uncompressedData.put(zipEntryName, data);
|
||||
}
|
||||
logger.info(() -> String.format("Unpacking Zip Entry - Name: '%s',Time: '%s', " +
|
||||
"Compressed Size: '%s', Type: '%s'",
|
||||
zipEntryName, zipEntryTime, zipEntryCompressedSize, type));
|
||||
|
||||
if (ZipResultType.FILE.equals(zipResultType)) {
|
||||
final File destinationFile = checkPath(message, zipEntryName);
|
||||
|
||||
if (zipEntry.isDirectory()) {
|
||||
destinationFile.mkdirs(); //NOSONAR false positive
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Unsupported zipResultType: " + zipResultType);
|
||||
mkDirOfAncestorDirectories(destinationFile);
|
||||
SpringZipUtils.copy(zipEntryInputStream, destinationFile);
|
||||
uncompressedData.put(zipEntryName, destinationFile);
|
||||
}
|
||||
}
|
||||
|
||||
public File checkPath(final Message<?> message, final String zipEntryName) throws IOException {
|
||||
final File tempDir = new File(workDirectory, message.getHeaders().getId().toString());
|
||||
tempDir.mkdirs(); //NOSONAR false positive
|
||||
final File destinationFile = new File(tempDir, zipEntryName);
|
||||
|
||||
/* If we see the relative traversal string of ".." we need to make sure
|
||||
* that the outputdir + name doesn't leave the outputdir.
|
||||
*/
|
||||
if (!destinationFile.getCanonicalPath()
|
||||
.startsWith(tempDir.getCanonicalPath() + File.separator)) {
|
||||
|
||||
throw new ZipException("The file " + zipEntryName +
|
||||
" is trying to leave the target output directory of " + workDirectory);
|
||||
}
|
||||
return destinationFile;
|
||||
}
|
||||
});
|
||||
|
||||
if (uncompressedData.isEmpty()) {
|
||||
logger.warn(() -> "No data unzipped from payload with message Id " + message.getHeaders().getId());
|
||||
unzippedData = null;
|
||||
}
|
||||
else {
|
||||
|
||||
if (this.expectSingleResult) {
|
||||
if (uncompressedData.size() == 1) {
|
||||
unzippedData = uncompressedData.values().iterator().next();
|
||||
}
|
||||
else {
|
||||
throw new MessagingException(message,
|
||||
String.format("The UnZip operation extracted %s "
|
||||
+ "result objects but expectSingleResult was 'true'.", uncompressedData
|
||||
.size()));
|
||||
else if (ZipResultType.BYTE_ARRAY.equals(zipResultType)) {
|
||||
if (!zipEntry.isDirectory()) {
|
||||
checkPath(message, zipEntryName);
|
||||
byte[] data = IOUtils.toByteArray(zipEntryInputStream);
|
||||
uncompressedData.put(zipEntryName, data);
|
||||
}
|
||||
}
|
||||
else {
|
||||
unzippedData = uncompressedData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
if (payload instanceof final File filePayload && this.deleteFiles) {
|
||||
if (!filePayload.delete() && logger.isWarnEnabled()) {
|
||||
logger.warn(() -> "failed to delete File '" + filePayload + "'");
|
||||
throw new IllegalStateException("Unsupported zipResultType: " + zipResultType);
|
||||
}
|
||||
}
|
||||
|
||||
public File checkPath(final Message<?> message, final String zipEntryName) throws IOException {
|
||||
File tempDir = new File(workDirectory, message.getHeaders().getId().toString()); // NOSONAR
|
||||
tempDir.mkdirs(); //NOSONAR false positive
|
||||
final File destinationFile = new File(tempDir, zipEntryName);
|
||||
|
||||
/* If we see the relative traversal string of ".." we need to make sure
|
||||
* that the outputdir + name doesn't leave the outputdir.
|
||||
*/
|
||||
if (!destinationFile.getCanonicalPath()
|
||||
.startsWith(tempDir.getCanonicalPath() + File.separator)) {
|
||||
|
||||
throw new ZipException("The file " + zipEntryName +
|
||||
" is trying to leave the target output directory of " + workDirectory);
|
||||
}
|
||||
return destinationFile;
|
||||
}
|
||||
});
|
||||
|
||||
if (uncompressedData.isEmpty()) {
|
||||
logger.warn(() -> "No data unzipped from payload with message Id " + message.getHeaders().getId());
|
||||
unzippedData = null;
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
else {
|
||||
|
||||
if (this.expectSingleResult) {
|
||||
if (uncompressedData.size() == 1) {
|
||||
unzippedData = uncompressedData.values().iterator().next();
|
||||
}
|
||||
else {
|
||||
throw new MessagingException(message,
|
||||
String.format("The UnZip operation extracted %s "
|
||||
+ "result objects but expectSingleResult was 'true'.", uncompressedData
|
||||
.size()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
unzippedData = uncompressedData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
if (payload instanceof File filePayload && this.deleteFiles && !filePayload.delete()) {
|
||||
logger.warn(() -> "failed to delete File '" + filePayload + "'");
|
||||
}
|
||||
return unzippedData;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessageHandlingException(message, "Failed to apply Zip transformation.", e);
|
||||
catch (FileNotFoundException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
}
|
||||
return unzippedData;
|
||||
}
|
||||
|
||||
private static void mkDirOfAncestorDirectories(File destinationFile) {
|
||||
File parentDirectory = destinationFile.getParentFile();
|
||||
if (parentDirectory != null) {
|
||||
parentDirectory.mkdirs();
|
||||
parentDirectory.mkdirs(); // NOSONAR
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
@@ -89,12 +90,12 @@ public class ZipTransformer extends AbstractZipTransformer {
|
||||
*/
|
||||
@Override
|
||||
protected Object doZipTransform(Message<?> message) {
|
||||
final Object payload = message.getPayload();
|
||||
final Object zippedData;
|
||||
final String baseFileName = this.fileNameGenerator.generateFileName(message);
|
||||
Object payload = message.getPayload();
|
||||
Object zippedData;
|
||||
String baseFileName = this.fileNameGenerator.generateFileName(message);
|
||||
|
||||
final String zipEntryName;
|
||||
final String zipFileName;
|
||||
String zipEntryName;
|
||||
String zipFileName;
|
||||
|
||||
if (message.getHeaders().containsKey(ZipHeaders.ZIP_ENTRY_FILE_NAME)) {
|
||||
zipEntryName = (String) message.getHeaders().get(ZipHeaders.ZIP_ENTRY_FILE_NAME);
|
||||
@@ -110,43 +111,18 @@ public class ZipTransformer extends AbstractZipTransformer {
|
||||
zipFileName = baseFileName + ZIP_EXTENSION;
|
||||
}
|
||||
|
||||
final Date lastModifiedDate;
|
||||
Date lastModifiedDate;
|
||||
|
||||
if (message.getHeaders().containsKey(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE)) {
|
||||
lastModifiedDate = (Date) message.getHeaders().get(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE);
|
||||
lastModifiedDate = message.getHeaders().get(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE, Date.class);
|
||||
}
|
||||
else {
|
||||
lastModifiedDate = new Date();
|
||||
}
|
||||
|
||||
java.util.List<ZipEntrySource> entries = new ArrayList<>();
|
||||
List<ZipEntrySource> entries = createZipEntries(payload, zipEntryName, lastModifiedDate);
|
||||
|
||||
if (payload instanceof Iterable<?>) {
|
||||
int counter = 1;
|
||||
|
||||
String baseName = FilenameUtils.getBaseName(zipEntryName);
|
||||
String fileExtension = FilenameUtils.getExtension(zipEntryName);
|
||||
|
||||
if (StringUtils.hasText(fileExtension)) {
|
||||
fileExtension = FilenameUtils.EXTENSION_SEPARATOR_STR + fileExtension;
|
||||
}
|
||||
|
||||
for (Object item : (Iterable<?>) payload) {
|
||||
|
||||
final ZipEntrySource zipEntrySource = createZipEntrySource(item, lastModifiedDate, baseName + "_"
|
||||
+ counter + fileExtension, this.useFileAttributes);
|
||||
logger.debug(() -> "ZipEntrySource path: '" + zipEntrySource.getPath() + "'");
|
||||
entries.add(zipEntrySource);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
final ZipEntrySource zipEntrySource =
|
||||
createZipEntrySource(payload, lastModifiedDate, zipEntryName, this.useFileAttributes);
|
||||
entries.add(zipEntrySource);
|
||||
}
|
||||
|
||||
final byte[] zippedBytes = SpringZipUtils.pack(entries, this.compressionLevel);
|
||||
byte[] zippedBytes = SpringZipUtils.pack(entries, this.compressionLevel);
|
||||
|
||||
if (ZipResultType.FILE.equals(this.zipResultType)) {
|
||||
final File zippedFile = new File(this.workDirectory, zipFileName);
|
||||
@@ -165,6 +141,48 @@ public class ZipTransformer extends AbstractZipTransformer {
|
||||
throw new IllegalStateException("Unsupported zipResultType " + this.zipResultType);
|
||||
}
|
||||
|
||||
deleteFilesIfAny(payload);
|
||||
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(zippedData)
|
||||
.copyHeaders(message.getHeaders())
|
||||
.setHeader(FileHeaders.FILENAME, zipFileName)
|
||||
.build();
|
||||
}
|
||||
|
||||
private List<ZipEntrySource> createZipEntries(Object payload, String zipEntryName, Date lastModifiedDate) {
|
||||
List<ZipEntrySource> entries = new ArrayList<>();
|
||||
|
||||
if (payload instanceof Iterable<?>) {
|
||||
int counter = 1;
|
||||
|
||||
String baseName = FilenameUtils.getBaseName(zipEntryName);
|
||||
String fileExtension = FilenameUtils.getExtension(zipEntryName);
|
||||
|
||||
if (StringUtils.hasText(fileExtension)) {
|
||||
fileExtension = FilenameUtils.EXTENSION_SEPARATOR_STR + fileExtension;
|
||||
}
|
||||
|
||||
for (Object item : (Iterable<?>) payload) {
|
||||
|
||||
final ZipEntrySource zipEntrySource =
|
||||
createZipEntrySource(item, lastModifiedDate, baseName + "_" + counter + fileExtension,
|
||||
this.useFileAttributes);
|
||||
logger.debug(() -> "ZipEntrySource path: '" + zipEntrySource.getPath() + "'");
|
||||
entries.add(zipEntrySource);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
final ZipEntrySource zipEntrySource =
|
||||
createZipEntrySource(payload, lastModifiedDate, zipEntryName, this.useFileAttributes);
|
||||
entries.add(zipEntrySource);
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
private void deleteFilesIfAny(Object payload) {
|
||||
if (this.deleteFiles) {
|
||||
if (payload instanceof Iterable<?>) {
|
||||
for (Object item : (Iterable<?>) payload) {
|
||||
@@ -175,11 +193,6 @@ public class ZipTransformer extends AbstractZipTransformer {
|
||||
deleteFile(payload);
|
||||
}
|
||||
}
|
||||
return getMessageBuilderFactory()
|
||||
.withPayload(zippedData)
|
||||
.copyHeaders(message.getHeaders())
|
||||
.setHeader(FileHeaders.FILENAME, zipFileName)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void deleteFile(Object fileToDelete) {
|
||||
|
||||
Reference in New Issue
Block a user