001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.blob; 018 019import java.io.FilterInputStream; 020import java.io.IOException; 021import java.io.InputStream; 022import java.net.MalformedURLException; 023import java.net.URL; 024 025import javax.jms.JMSException; 026 027import org.apache.activemq.command.ActiveMQBlobMessage; 028import org.apache.commons.net.ftp.FTPClient; 029 030/** 031 * A FTP implementation for {@link BlobDownloadStrategy}. 032 */ 033public class FTPBlobDownloadStrategy extends FTPStrategy implements BlobDownloadStrategy { 034 035 public FTPBlobDownloadStrategy(BlobTransferPolicy transferPolicy) throws MalformedURLException { 036 super(transferPolicy); 037 } 038 039 public InputStream getInputStream(ActiveMQBlobMessage message) throws IOException, JMSException { 040 // Do some checks on the received URL against the transfer policy 041 URL uploadURL = new URL(super.transferPolicy.getUploadUrl()); 042 String protocol = message.getURL().getProtocol(); 043 if (!protocol.equals(uploadURL.getProtocol())) { 044 throw new IOException("The message URL protocol is incorrect"); 045 } 046 047 String host = message.getURL().getHost(); 048 if (!host.equals(uploadURL.getHost())) { 049 throw new IOException("The message URL host is incorrect"); 050 } 051 052 int port = message.getURL().getPort(); 053 if (uploadURL.getPort() != 0 && port != uploadURL.getPort()) { 054 throw new IOException("The message URL port is incorrect"); 055 } 056 057 url = message.getURL(); 058 final FTPClient ftp = createFTP(); 059 String path = url.getPath(); 060 String workingDir = path.substring(0, path.lastIndexOf("/")); 061 String file = path.substring(path.lastIndexOf("/") + 1); 062 ftp.changeWorkingDirectory(workingDir); 063 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 064 065 InputStream input = new FilterInputStream(ftp.retrieveFileStream(file)) { 066 067 public void close() throws IOException { 068 in.close(); 069 ftp.quit(); 070 ftp.disconnect(); 071 } 072 }; 073 074 return input; 075 } 076 077 public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException { 078 url = message.getURL(); 079 final FTPClient ftp = createFTP(); 080 081 String path = url.getPath(); 082 try { 083 if (!ftp.deleteFile(path)) { 084 throw new JMSException("Delete file failed: " + ftp.getReplyString()); 085 } 086 } finally { 087 ftp.quit(); 088 ftp.disconnect(); 089 } 090 091 } 092 093}