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.ConnectException;
024    import java.net.MalformedURLException;
025    import java.net.URL;
026    
027    import javax.jms.JMSException;
028    
029    import org.apache.activemq.command.ActiveMQBlobMessage;
030    import org.apache.commons.net.ftp.FTPClient;
031    
032    /**
033     * A FTP implementation of {@link BlobUploadStrategy}.
034     */
035    public class FTPBlobUploadStrategy implements BlobUploadStrategy {
036            
037            private URL url;
038            private String ftpUser = "";
039            private String ftpPass = "";
040            private BlobTransferPolicy transferPolicy;
041            
042            public FTPBlobUploadStrategy(BlobTransferPolicy transferPolicy) throws MalformedURLException {
043                    this.transferPolicy = transferPolicy;
044                    this.url = new URL(this.transferPolicy.getUploadUrl());
045                    
046                    setUserInformation(url.getUserInfo());
047            }
048    
049            public URL uploadFile(ActiveMQBlobMessage message, File file)
050                            throws JMSException, IOException {
051                    return uploadStream(message, new FileInputStream(file));
052            }
053    
054            public URL uploadStream(ActiveMQBlobMessage message, InputStream in)
055                            throws JMSException, IOException {
056                    String connectUrl = url.getHost();
057                    int port = url.getPort() < 1 ? 21 : url.getPort();
058                    
059                    FTPClient ftp = new FTPClient();
060                    try {
061                    ftp.connect(connectUrl, port);
062            } catch(ConnectException e) {
063                    throw new JMSException("Problem connecting the FTP-server");
064            }
065                    if(!ftp.login(ftpUser, ftpPass)) {
066                            ftp.quit();
067                            ftp.disconnect();
068                            throw new JMSException("Cant Authentificate to FTP-Server");
069                    }
070                    String path = url.getPath();
071            String workingDir = path.substring(0, path.lastIndexOf("/"));
072                    String filename = message.getMessageId().toString().replaceAll(":", "_");
073            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
074            
075            String url;
076            if(!ftp.changeWorkingDirectory(workingDir)) {
077                    url = this.url.toString().replaceFirst(this.url.getPath(), "")+"/";
078            } else {
079                    url = this.url.toString();
080            }
081            
082                    ftp.storeFile(filename, in);
083                    ftp.quit();
084                    ftp.disconnect();
085                    
086                    return new URL(url + filename);
087            }
088            
089            private void setUserInformation(String userInfo) {
090                    if(userInfo != null) {
091                            String[] userPass = userInfo.split(":");
092                            if(userPass.length > 0) this.ftpUser = userPass[0];
093                            if(userPass.length > 1) this.ftpPass = userPass[1];
094                    } else {
095                            this.ftpUser = "anonymous";
096                            this.ftpPass = "anonymous";
097                    }
098            }
099    
100    }