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     */
017    package org.apache.activemq.blob;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.net.MalformedURLException;
024    import java.net.URL;
025    
026    import javax.jms.JMSException;
027    
028    import org.apache.activemq.command.ActiveMQBlobMessage;
029    import org.apache.commons.net.ftp.FTPClient;
030    
031    /**
032     * A FTP implementation of {@link BlobUploadStrategy}.
033     */
034    public class FTPBlobUploadStrategy extends FTPStrategy implements BlobUploadStrategy {
035            
036            public FTPBlobUploadStrategy(BlobTransferPolicy transferPolicy) throws MalformedURLException {
037                    super(transferPolicy);
038            }
039    
040            public URL uploadFile(ActiveMQBlobMessage message, File file) throws JMSException, IOException {
041                    return uploadStream(message, new FileInputStream(file));
042            }
043    
044            public URL uploadStream(ActiveMQBlobMessage message, InputStream in)
045                            throws JMSException, IOException {
046    
047                FTPClient ftp = createFTP();
048                try {
049                    String path = url.getPath();
050                String workingDir = path.substring(0, path.lastIndexOf("/"));
051                    String filename = message.getMessageId().toString().replaceAll(":", "_");
052                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
053                
054                String url;
055                if(!ftp.changeWorkingDirectory(workingDir)) {
056                    url = this.url.toString().replaceFirst(this.url.getPath(), "")+"/";
057                } else {
058                    url = this.url.toString();
059                }
060                
061                    if (!ftp.storeFile(filename, in)) {
062                        throw new JMSException("FTP store failed: " + ftp.getReplyString());
063                    }
064                    return new URL(url + filename);
065                } finally {
066                    ftp.quit();
067                    ftp.disconnect();
068                }
069                    
070            }
071    
072    }