diff --git a/activemq-core/src/main/java/org/apache/activemq/blob/FTPBlobUploadStrategy.java b/activemq-core/src/main/java/org/apache/activemq/blob/FTPBlobUploadStrategy.java index 1490d12d77..f9f55bb792 100644 --- a/activemq-core/src/main/java/org/apache/activemq/blob/FTPBlobUploadStrategy.java +++ b/activemq-core/src/main/java/org/apache/activemq/blob/FTPBlobUploadStrategy.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.net.ConnectException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Arrays; import javax.jms.JMSException; @@ -79,10 +80,11 @@ public class FTPBlobUploadStrategy implements BlobUploadStrategy { url = this.url.toString(); } - ftp.storeFile(filename, in); + if (!ftp.storeFile(filename, in)) { + throw new JMSException("FTP store failed: " + ftp.getReplyString()); + } ftp.quit(); ftp.disconnect(); - return new URL(url + filename); } diff --git a/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java b/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java index ea1d4fa278..e4b1deb027 100644 --- a/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobDownloadStrategyTest.java @@ -24,59 +24,17 @@ import java.net.URL; import javax.jms.JMSException; import junit.framework.Assert; -import junit.framework.TestCase; import org.apache.activemq.command.ActiveMQBlobMessage; -import org.apache.ftpserver.FtpServer; -import org.apache.ftpserver.FtpServerFactory; -import org.apache.ftpserver.ftplet.UserManager; -import org.apache.ftpserver.listener.ListenerFactory; -import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; -import org.apache.ftpserver.usermanager.impl.BaseUser; -import org.jmock.Mockery; -public class FTPBlobDownloadStrategyTest extends TestCase { - - private static final String ftpServerListenerName = "default"; - private FtpServer server; - final static String userNamePass = "activemq"; - - Mockery context = null; - int ftpPort; - String ftpUrl; +public class FTPBlobDownloadStrategyTest extends FTPTestSupport { final int FILE_SIZE = Short.MAX_VALUE * 10; - protected void setUp() throws Exception { - final File ftpHomeDirFile = new File("target/FTPBlobTest/ftptest"); - ftpHomeDirFile.mkdirs(); - ftpHomeDirFile.getParentFile().deleteOnExit(); - - FtpServerFactory serverFactory = new FtpServerFactory(); - ListenerFactory factory = new ListenerFactory(); - - PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); - UserManager userManager = userManagerFactory.createUserManager(); - - BaseUser user = new BaseUser(); - user.setName("activemq"); - user.setPassword("activemq"); - user.setHomeDirectory(ftpHomeDirFile.getParent()); - - userManager.save(user); - - serverFactory.setUserManager(userManager); - factory.setPort(0); - serverFactory.addListener(ftpServerListenerName, factory - .createListener()); - server = serverFactory.createServer(); - server.start(); - ftpPort = serverFactory.getListener(ftpServerListenerName) - .getPort(); - - ftpUrl = "ftp://" + userNamePass + ":" + userNamePass + "@localhost:" - + ftpPort + "/ftptest/"; - + public void testDownload() throws Exception { + setConnection(); + + // create file File uploadFile = new File(ftpHomeDirFile, "test.txt"); FileWriter wrt = new FileWriter(uploadFile); @@ -87,10 +45,7 @@ public class FTPBlobDownloadStrategyTest extends TestCase { } wrt.close(); - - } - - public void testDownload() { + ActiveMQBlobMessage message = new ActiveMQBlobMessage(); BlobDownloadStrategy strategy = new FTPBlobDownloadStrategy(); InputStream stream; diff --git a/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java b/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java index f058c9e32b..f8214fc6de 100644 --- a/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/blob/FTPBlobUploadStrategyTest.java @@ -19,9 +19,7 @@ package org.apache.activemq.blob; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; -import java.net.URL; -import javax.jms.Connection; import javax.jms.JMSException; import javax.jms.Session; @@ -29,95 +27,14 @@ import junit.framework.Assert; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQSession; -import org.apache.activemq.EmbeddedBrokerTestSupport; import org.apache.activemq.command.ActiveMQBlobMessage; import org.apache.activemq.command.MessageId; -import org.apache.commons.net.ftp.FTPClient; -import org.apache.ftpserver.FtpServer; -import org.apache.ftpserver.FtpServerFactory; -import org.apache.ftpserver.ftplet.AuthorizationRequest; -import org.apache.ftpserver.ftplet.User; -import org.apache.ftpserver.ftplet.UserManager; -import org.apache.ftpserver.listener.ListenerFactory; -import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; -import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication; -import org.apache.ftpserver.usermanager.impl.BaseUser; -import org.jmock.Expectations; -import org.jmock.Mockery; -import org.jmock.api.Invocation; -import org.jmock.lib.action.CustomAction; -public class FTPBlobUploadStrategyTest extends EmbeddedBrokerTestSupport { - - private static final String ftpServerListenerName = "default"; - private Connection connection; - private FtpServer server; - final static String userNamePass = "activemq"; +public class FTPBlobUploadStrategyTest extends FTPTestSupport { - Mockery context = null; - String ftpUrl; - - protected void setUp() throws Exception { - - final File ftpHomeDirFile = new File("target/FTPBlobTest/ftptest"); - ftpHomeDirFile.mkdirs(); - ftpHomeDirFile.getParentFile().deleteOnExit(); - - FtpServerFactory serverFactory = new FtpServerFactory(); - ListenerFactory factory = new ListenerFactory(); - - PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); - UserManager userManager = userManagerFactory.createUserManager(); - - BaseUser user = new BaseUser(); - user.setName("activemq"); - user.setPassword("activemq"); - user.setHomeDirectory(ftpHomeDirFile.getParent()); - - userManager.save(user); - - serverFactory.setUserManager(userManager); - factory.setPort(0); - serverFactory.addListener(ftpServerListenerName, factory - .createListener()); - server = serverFactory.createServer(); - server.start(); - int ftpPort = serverFactory.getListener(ftpServerListenerName) - .getPort(); - - ftpUrl = "ftp://" - + userNamePass - + ":" - + userNamePass - + "@localhost:" - + ftpPort - + "/ftptest/"; - bindAddress = "vm://localhost?jms.blobTransferPolicy.defaultUploadUrl=" + ftpUrl; - super.setUp(); - - connection = createConnection(); - connection.start(); - - // check if file exist and delete it - URL url = new URL(ftpUrl); - String connectUrl = url.getHost(); - int port = url.getPort() < 1 ? 21 : url.getPort(); - - FTPClient ftp = new FTPClient(); - ftp.connect(connectUrl, port); - if(!ftp.login("activemq", "activemq")) { - ftp.quit(); - ftp.disconnect(); - throw new JMSException("Cant Authentificate to FTP-Server"); - } - ftp.changeWorkingDirectory("ftptest"); - ftp.deleteFile("testmessage"); - ftp.quit(); - ftp.disconnect(); - } - - public void testFileUpload() throws Exception { + public void testFileUpload() throws Exception { + setConnection(); File file = File.createTempFile("amq-data-file-", ".dat"); // lets write some data BufferedWriter writer = new BufferedWriter(new FileWriter(file)); @@ -131,6 +48,31 @@ public class FTPBlobUploadStrategyTest extends EmbeddedBrokerTestSupport { message.setMessageId(new MessageId("testmessage")); message.onSend(); Assert.assertEquals(ftpUrl + "testmessage", message.getURL().toString()); + File uploaded = new File(ftpHomeDirFile, "testmessage"); + assertTrue("File doesn't exists", uploaded.exists()); } + public void testWriteDenied() throws Exception { + userNamePass = "guest"; + setConnection(); + File file = File.createTempFile("amq-data-file-", ".dat"); + // lets write some data + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.append("hello world"); + writer.close(); + + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + ((ActiveMQConnection)connection).setCopyMessageOnSend(false); + + ActiveMQBlobMessage message = (ActiveMQBlobMessage) ((ActiveMQSession)session).createBlobMessage(file); + message.setMessageId(new MessageId("testmessage")); + try { + message.onSend(); + } catch (JMSException e) { + e.printStackTrace(); + return; + } + fail("Should have failed with permission denied exception!"); + } + } diff --git a/activemq-core/src/test/java/org/apache/activemq/blob/FTPTestSupport.java b/activemq-core/src/test/java/org/apache/activemq/blob/FTPTestSupport.java index 27e03c18bc..e2c60a2b4a 100644 --- a/activemq-core/src/test/java/org/apache/activemq/blob/FTPTestSupport.java +++ b/activemq-core/src/test/java/org/apache/activemq/blob/FTPTestSupport.java @@ -17,7 +17,7 @@ import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.WritePermission; import org.jmock.Mockery; -public class FTPTestSupport extends EmbeddedBrokerTestSupport { +public abstract class FTPTestSupport extends EmbeddedBrokerTestSupport { protected static final String ftpServerListenerName = "default"; protected Connection connection;