INT-1482 added default charset value for FtpSendingMessageHandler
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,8 +13,18 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.ftp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -22,18 +32,12 @@ import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageDeliveryException;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.MessageRejectedException;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.SocketException;
|
||||
|
||||
|
||||
/**
|
||||
* A {@link org.springframework.integration.core.MessageHandler} implementation that sends files to an FTP server.
|
||||
*
|
||||
@@ -41,14 +45,21 @@ import java.net.SocketException;
|
||||
* @author Mark Fisher
|
||||
* @author Josh Long
|
||||
*/
|
||||
public class FtpSendingMessageHandler implements MessageHandler,
|
||||
InitializingBean {
|
||||
public class FtpSendingMessageHandler implements MessageHandler, InitializingBean {
|
||||
|
||||
private static final String TEMPORARY_FILE_SUFFIX = ".writing";
|
||||
private FtpClientPool ftpClientPool;
|
||||
private FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
|
||||
private File temporaryBufferFolderFile;
|
||||
private Resource temporaryBufferFolder = new FileSystemResource(SystemUtils.getJavaIoTmpDir());
|
||||
private String charset;
|
||||
|
||||
|
||||
private volatile FtpClientPool ftpClientPool;
|
||||
|
||||
private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
|
||||
|
||||
private volatile File temporaryBufferFolderFile;
|
||||
|
||||
private volatile Resource temporaryBufferFolder = new FileSystemResource(SystemUtils.getJavaIoTmpDir());
|
||||
|
||||
private volatile String charset = Charset.defaultCharset().name();
|
||||
|
||||
|
||||
public FtpSendingMessageHandler() {
|
||||
}
|
||||
@@ -57,10 +68,19 @@ public class FtpSendingMessageHandler implements MessageHandler,
|
||||
this.ftpClientPool = ftpClientPool;
|
||||
}
|
||||
|
||||
|
||||
public void setFtpClientPool(FtpClientPool ftpClientPool) {
|
||||
this.ftpClientPool = ftpClientPool;
|
||||
}
|
||||
|
||||
public void setTemporaryBufferFolder(Resource temporaryBufferFolder) {
|
||||
this.temporaryBufferFolder = temporaryBufferFolder;
|
||||
}
|
||||
|
||||
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
|
||||
this.fileNameGenerator = fileNameGenerator;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(ftpClientPool, "'ftpClientPool' must not be null");
|
||||
Assert.notNull(temporaryBufferFolder,
|
||||
@@ -82,83 +102,56 @@ public class FtpSendingMessageHandler implements MessageHandler,
|
||||
return resultFile;
|
||||
}
|
||||
|
||||
private File handleByteArrayMessage(byte[] bytes, File tempFile,
|
||||
File resultFile) throws IOException {
|
||||
private File handleByteArrayMessage(byte[] bytes, File tempFile, File resultFile) throws IOException {
|
||||
FileCopyUtils.copy(bytes, tempFile);
|
||||
tempFile.renameTo(resultFile);
|
||||
|
||||
return resultFile;
|
||||
}
|
||||
|
||||
private File handleStringMessage(String content, File tempFile,
|
||||
File resultFile, String charset) throws IOException {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(
|
||||
tempFile), charset);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile), charset);
|
||||
FileCopyUtils.copy(content, writer);
|
||||
tempFile.renameTo(resultFile);
|
||||
|
||||
return resultFile;
|
||||
}
|
||||
|
||||
public void setTemporaryBufferFolder(Resource temporaryBufferFolder) {
|
||||
this.temporaryBufferFolder = temporaryBufferFolder;
|
||||
}
|
||||
|
||||
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
|
||||
this.fileNameGenerator = fileNameGenerator;
|
||||
}
|
||||
|
||||
private File redeemForStorableFile(Message<?> msg)
|
||||
throws MessageDeliveryException {
|
||||
private File redeemForStorableFile(Message<?> message) throws MessageDeliveryException {
|
||||
try {
|
||||
Object payload = msg.getPayload();
|
||||
String generateFileName = this.fileNameGenerator.generateFileName(msg);
|
||||
File tempFile = new File(temporaryBufferFolderFile,
|
||||
generateFileName + TEMPORARY_FILE_SUFFIX);
|
||||
File resultFile = new File(temporaryBufferFolderFile,
|
||||
generateFileName);
|
||||
Object payload = message.getPayload();
|
||||
String generateFileName = this.fileNameGenerator.generateFileName(message);
|
||||
File tempFile = new File(temporaryBufferFolderFile, generateFileName + TEMPORARY_FILE_SUFFIX);
|
||||
File resultFile = new File(temporaryBufferFolderFile, generateFileName);
|
||||
File sendableFile;
|
||||
|
||||
if (payload instanceof String) {
|
||||
sendableFile = this.handleStringMessage((String) payload,
|
||||
tempFile, resultFile, this.charset);
|
||||
} else if (payload instanceof File) {
|
||||
sendableFile = this.handleFileMessage((File) payload, tempFile,
|
||||
resultFile);
|
||||
} else if (payload instanceof byte[]) {
|
||||
sendableFile = this.handleByteArrayMessage((byte[]) payload,
|
||||
tempFile, resultFile);
|
||||
} else {
|
||||
sendableFile = this.handleStringMessage((String) payload, tempFile, resultFile, this.charset);
|
||||
}
|
||||
else if (payload instanceof File) {
|
||||
sendableFile = this.handleFileMessage((File) payload, tempFile, resultFile);
|
||||
}
|
||||
else if (payload instanceof byte[]) {
|
||||
sendableFile = this.handleByteArrayMessage((byte[]) payload, tempFile, resultFile);
|
||||
}
|
||||
else {
|
||||
sendableFile = null;
|
||||
}
|
||||
|
||||
return sendableFile;
|
||||
} catch (Throwable th) {
|
||||
throw new MessageDeliveryException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCharset(String charset) {
|
||||
this.charset = charset;
|
||||
catch (Throwable th) {
|
||||
throw new MessageDeliveryException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/* Ugh this needs to be put in a convenient place accessible for all the file:, sftp:, and ftp:* adapters */
|
||||
|
||||
public void handleMessage(Message<?> message)
|
||||
throws MessageRejectedException, MessageHandlingException,
|
||||
MessageDeliveryException {
|
||||
public void handleMessage(Message<?> message) {
|
||||
Assert.notNull(message, "'message' must not be null");
|
||||
|
||||
Object payload = message.getPayload();
|
||||
|
||||
Assert.notNull(payload, "Message payload must not be null");
|
||||
|
||||
File file = this.redeemForStorableFile(message);
|
||||
|
||||
if ((file != null) && file.exists()) {
|
||||
FTPClient client = null;
|
||||
boolean sentSuccesfully;
|
||||
|
||||
try {
|
||||
client = getFtpClient();
|
||||
sentSuccesfully = sendFile(file, client);
|
||||
@@ -167,14 +160,17 @@ public class FtpSendingMessageHandler implements MessageHandler,
|
||||
"File [" + file +
|
||||
"] not found in local working directory; it was moved or deleted unexpectedly",
|
||||
e);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessageDeliveryException(message,
|
||||
"Error transferring file [" + file +
|
||||
"] from local working directory to remote FTP directory", e);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessageDeliveryException(message,
|
||||
"Error handling message for file [" + file + "]", e);
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
if (file.exists()) {
|
||||
try {
|
||||
file.delete();
|
||||
@@ -182,12 +178,10 @@ public class FtpSendingMessageHandler implements MessageHandler,
|
||||
/// noop
|
||||
}
|
||||
}
|
||||
|
||||
if (client != null) {
|
||||
ftpClientPool.releaseClient(client);
|
||||
}
|
||||
}
|
||||
|
||||
if (!sentSuccesfully) {
|
||||
throw new MessageDeliveryException(message,
|
||||
"Failed to store file '" + file + "'");
|
||||
@@ -195,22 +189,19 @@ public class FtpSendingMessageHandler implements MessageHandler,
|
||||
}
|
||||
}
|
||||
|
||||
private boolean sendFile(File file, FTPClient client)
|
||||
throws FileNotFoundException, IOException {
|
||||
private boolean sendFile(File file, FTPClient client) throws FileNotFoundException, IOException {
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
boolean sent = client.storeFile(file.getName(), fileInputStream);
|
||||
fileInputStream.close();
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
private FTPClient getFtpClient() throws SocketException, IOException {
|
||||
FTPClient client;
|
||||
client = this.ftpClientPool.getClient();
|
||||
Assert.state(client != null,
|
||||
FtpClientPool.class.getSimpleName() +
|
||||
" returned 'null' client this most likely a bug in the pool implementation.");
|
||||
|
||||
Assert.state(client != null, FtpClientPool.class.getSimpleName() +
|
||||
" returned 'null' client this most likely a bug in the pool implementation.");
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user