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