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