KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > blob > DefaultBlobUploadStrategy


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.blob;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import javax.jms.JMSException JavaDoc;
29
30 import org.apache.activemq.command.ActiveMQBlobMessage;
31
32 /**
33  * A default implementation of {@link BlobUploadStrategy} which uses the URL class to upload
34  * files or streams to a remote URL
35  */

36 public class DefaultBlobUploadStrategy implements BlobUploadStrategy {
37     private BlobTransferPolicy transferPolicy;
38
39     public DefaultBlobUploadStrategy(BlobTransferPolicy transferPolicy) {
40         this.transferPolicy = transferPolicy;
41     }
42
43     public URL JavaDoc uploadFile(ActiveMQBlobMessage message, File JavaDoc file) throws JMSException JavaDoc, IOException JavaDoc {
44         return uploadStream(message, new FileInputStream JavaDoc(file));
45     }
46
47     public URL JavaDoc uploadStream(ActiveMQBlobMessage message, InputStream JavaDoc fis) throws JMSException JavaDoc, IOException JavaDoc {
48         URL JavaDoc url = createUploadURL(message);
49
50         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
51         connection.setRequestMethod("PUT");
52         connection.setDoOutput(true);
53         
54         // use chunked mode or otherwise URLConnection loads everything into memory
55
// (chunked mode not supported before JRE 1.5)
56
connection.setChunkedStreamingMode(transferPolicy.getBufferSize());
57         
58         OutputStream JavaDoc os = connection.getOutputStream();
59
60         byte[] buf = new byte[transferPolicy.getBufferSize()];
61         for (int c = fis.read(buf); c != -1; c = fis.read(buf)) {
62             os.write(buf, 0, c);
63             os.flush();
64         }
65         os.close();
66         fis.close();
67         
68         if (!isSuccessfulCode(connection.getResponseCode())) {
69             throw new IOException JavaDoc("PUT was not successful: "
70                     + connection.getResponseCode() + " " + connection.getResponseMessage());
71         }
72
73         return url;
74     }
75
76     public void deleteFile(ActiveMQBlobMessage message) throws IOException JavaDoc, JMSException JavaDoc {
77         URL JavaDoc url = createUploadURL(message);
78
79         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
80         connection.setRequestMethod("DELETE");
81         connection.connect();
82         connection.disconnect();
83
84         if (!isSuccessfulCode(connection.getResponseCode())) {
85             throw new IOException JavaDoc("DELETE was not successful: "
86                     + connection.getResponseCode() + " " + connection.getResponseMessage());
87         }
88     }
89     
90     private boolean isSuccessfulCode(int responseCode) {
91         return responseCode >= 200 && responseCode < 300; // 2xx => successful
92
}
93
94     protected URL JavaDoc createUploadURL(ActiveMQBlobMessage message) throws JMSException JavaDoc, MalformedURLException JavaDoc {
95         return new URL JavaDoc(transferPolicy.getUploadUrl() + message.getMessageId().toString());
96     }
97 }
98
Popular Tags