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.net.MalformedURLException;
020    import java.net.URL;
021    
022    /**
023     * The policy for configuring how BLOBs (Binary Large OBjects) are transferred
024     * out of band between producers, brokers and consumers.
025     *
026     * @version $Revision: $
027     */
028    public class BlobTransferPolicy {
029        private String defaultUploadUrl = "http://localhost:8080/uploads/";
030        private String brokerUploadUrl;
031        private String uploadUrl;
032        private int bufferSize = 128 * 1024;
033        private BlobUploadStrategy uploadStrategy;
034        private BlobDownloadStrategy downloadStrategy;
035    
036        /**
037         * Returns a copy of this policy object
038         */
039        public BlobTransferPolicy copy() {
040            BlobTransferPolicy that = new BlobTransferPolicy();
041            that.defaultUploadUrl = this.defaultUploadUrl;
042            that.brokerUploadUrl = this.brokerUploadUrl;
043            that.uploadUrl = this.uploadUrl;
044            that.uploadStrategy = this.uploadStrategy;
045            return that;
046        }
047    
048        public String getUploadUrl() {
049            if (uploadUrl == null) {
050                uploadUrl = getBrokerUploadUrl();
051                if (uploadUrl == null) {
052                    uploadUrl = getDefaultUploadUrl();
053                }
054            }
055            return uploadUrl;
056        }
057    
058        /**
059         * Sets the upload URL to use explicitly on the client which will
060         * overload the default or the broker's URL. This allows the client to decide
061         * where to upload files to irrespective of the brokers configuration.
062         */
063        public void setUploadUrl(String uploadUrl) {
064            this.uploadUrl = uploadUrl;
065        }
066    
067        public String getBrokerUploadUrl() {
068            return brokerUploadUrl;
069        }
070    
071        /**
072         * Called by the JMS client when a broker advertises its upload URL
073         */
074        public void setBrokerUploadUrl(String brokerUploadUrl) {
075            this.brokerUploadUrl = brokerUploadUrl;
076        }
077    
078        public String getDefaultUploadUrl() {
079            return defaultUploadUrl;
080        }
081    
082        /**
083         * Sets the default upload URL to use if the broker does not
084         * have a configured upload URL
085         */
086        public void setDefaultUploadUrl(String defaultUploadUrl) {
087            this.defaultUploadUrl = defaultUploadUrl;
088        }
089    
090        public BlobUploadStrategy getUploadStrategy() {
091            if (uploadStrategy == null) {
092                uploadStrategy = createUploadStrategy();
093            }
094            return uploadStrategy;
095        }
096    
097        public BlobDownloadStrategy getDownloadStrategy() {
098            if(downloadStrategy == null) {
099                downloadStrategy = createDownloadStrategy();
100            }
101            return downloadStrategy;
102        }
103    
104        /**
105         * Sets the upload strategy to use for uploading BLOBs to some URL
106         */
107        public void setUploadStrategy(BlobUploadStrategy uploadStrategy) {
108            this.uploadStrategy = uploadStrategy;
109        }
110    
111        public int getBufferSize() {
112            return bufferSize;
113        }
114    
115        /**
116         * Sets the default buffer size used when uploading or downloading files
117         */
118        public void setBufferSize(int bufferSize) {
119            this.bufferSize = bufferSize;
120        }
121    
122        /**
123         * Returns the upload strategy depending on the information from the
124         * uploadURL. Currently supportet HTTP and FTP
125         * 
126         * @return
127         */
128        protected BlobUploadStrategy createUploadStrategy() {
129            BlobUploadStrategy strategy;
130            try {
131                URL url = new URL(getUploadUrl());
132    
133                if(url.getProtocol().equalsIgnoreCase("FTP")) {
134                    strategy = new FTPBlobUploadStrategy(this);
135                } else {
136                    strategy = new DefaultBlobUploadStrategy(this);
137        }
138            } catch (MalformedURLException e) {
139                    strategy = new DefaultBlobUploadStrategy(this);
140    }
141            return strategy;
142        }
143        
144        /**
145         * Returns the download strategy depending on the information from the
146         * uploadURL. Currently supportet HTTP and FTP
147         * 
148         * @return
149         */
150        protected BlobDownloadStrategy createDownloadStrategy() {
151            BlobDownloadStrategy strategy;
152            try {
153                URL url = new URL(getUploadUrl());
154                
155                if(url.getProtocol().equalsIgnoreCase("FTP")) {
156                    strategy = new FTPBlobDownloadStrategy();
157                } else {
158                    strategy = new DefaultBlobDownloadStrategy();
159                }
160            } catch (MalformedURLException e) {
161                strategy = new DefaultBlobDownloadStrategy();
162            }
163            return strategy;
164        }
165    
166        
167    }