mirror of https://github.com/apache/activemq.git
added a simple default cut of the DefaultBlobUploadStrategy implementation along with a test case for the URI based configuration
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@511798 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
12a5355b2c
commit
52c22cca2e
|
@ -504,9 +504,15 @@ public class ActiveMQConnectionFactory extends JNDIBaseStorable implements Conne
|
|||
setRedeliveryPolicy(rp);
|
||||
rc = true;
|
||||
}
|
||||
|
||||
|
||||
BlobTransferPolicy blobTransferPolicy = new BlobTransferPolicy();
|
||||
if ( IntrospectionSupport.setProperties(blobTransferPolicy, properties, "blobTransferPolicy.") ) {
|
||||
setBlobTransferPolicy(blobTransferPolicy);
|
||||
rc = true;
|
||||
}
|
||||
|
||||
rc |= IntrospectionSupport.setProperties(this, properties);
|
||||
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ public class BlobTransferPolicy {
|
|||
private String defaultUploadUrl = "http://localhost:8080";
|
||||
private String brokerUploadUrl;
|
||||
private String uploadUrl;
|
||||
private int bufferSize = 128 * 1024;
|
||||
private BlobUploadStrategy uploadStrategy;
|
||||
|
||||
/**
|
||||
|
@ -96,6 +97,17 @@ public class BlobTransferPolicy {
|
|||
this.uploadStrategy = uploadStrategy;
|
||||
}
|
||||
|
||||
public int getBufferSize() {
|
||||
return bufferSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default buffer size used when uploading or downloading files
|
||||
*/
|
||||
public void setBufferSize(int bufferSize) {
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
protected BlobUploadStrategy createUploadStrategy() {
|
||||
return new DefaultBlobUploadStrategy(this);
|
||||
}
|
||||
|
|
|
@ -20,10 +20,13 @@ import org.apache.activemq.command.ActiveMQBlobMessage;
|
|||
|
||||
import javax.jms.JMSException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
* A default implementation of {@link BlobUploadStrategy} which uses the URL class to upload
|
||||
|
@ -37,17 +40,36 @@ public class DefaultBlobUploadStrategy implements BlobUploadStrategy {
|
|||
}
|
||||
|
||||
public URL uploadFile(ActiveMQBlobMessage message, File file) throws JMSException, IOException {
|
||||
URL url = createUploadURL(message);
|
||||
// TODO upload to URL
|
||||
// return url;
|
||||
throw new JMSException("Not implemented yet!");
|
||||
return uploadStream(message, new FileInputStream(file));
|
||||
}
|
||||
|
||||
public URL uploadStream(ActiveMQBlobMessage message, InputStream in) throws JMSException, IOException {
|
||||
public URL uploadStream(ActiveMQBlobMessage message, InputStream fis) throws JMSException, IOException {
|
||||
URL url = createUploadURL(message);
|
||||
// TODO upload to URL
|
||||
// return url;
|
||||
throw new JMSException("Not implemented yet!");
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
OutputStream os = connection.getOutputStream();
|
||||
|
||||
byte[] buf = new byte[transferPolicy.getBufferSize()];
|
||||
for (int c = fis.read(buf); c != -1; c = fis.read(buf)) {
|
||||
os.write(buf, 0, c);
|
||||
}
|
||||
os.close();
|
||||
fis.close();
|
||||
|
||||
/*
|
||||
// Read the response.
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
System.out.println(inputLine);
|
||||
}
|
||||
in.close();
|
||||
*/
|
||||
|
||||
// TODO we now need to ensure that the return code is OK?
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
protected URL createUploadURL(ActiveMQBlobMessage message) throws JMSException, MalformedURLException {
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.activemq.blob;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
|
||||
/**
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class BlobTransferPolicyUriTest extends TestCase {
|
||||
public void testBlobTransferPolicyIsConfiguredViaUri() throws Exception {
|
||||
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?jms.blobTransferPolicy.defaultUploadUrl=http://foo.com");
|
||||
BlobTransferPolicy policy = factory.getBlobTransferPolicy();
|
||||
assertEquals("http://foo.com", policy.getDefaultUploadUrl());
|
||||
assertEquals("http://foo.com", policy.getUploadUrl());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue