1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.astrogrid.filestore.common ;
36
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.io.FileOutputStream;
40 import java.io.BufferedOutputStream;
41
42 import java.net.URL ;
43 import java.net.URLConnection ;
44 import java.net.HttpURLConnection ;
45 import java.net.MalformedURLException ;
46
47 import org.apache.commons.logging.Log ;
48 import org.apache.commons.logging.LogFactory ;
49
50 import org.astrogrid.filestore.common.transfer.mock.Handler ;
51
52 /***
53 * A wrapper for PUT transfer streams.
54 *
55 */
56 public class FileStoreOutputStream
57 extends OutputStream
58 {
59
60 /***
61 * Our debug logger.
62 *
63 */
64 private static Log log = LogFactory.getLog(FileStoreOutputStream.class);
65
66 /***
67 * Our default buffer size.
68 *
69 */
70 public static final int BUFFER_SIZE = 1024 ;
71
72 /***
73 * Create a FileStoreOutputStream from a string URL.
74 * @param string A string representation of the target URL.
75 * @throws MalformedURLException
76 *
77 */
78 public FileStoreOutputStream(String string)
79 throws MalformedURLException
80 {
81 this(
82 new URL(
83 string
84 )
85 );
86 }
87
88 /***
89 * Create a FileStoreOutputStream from a URL.
90 * @param url The target URL to connect to.
91 *
92 */
93 public FileStoreOutputStream(URL url)
94 {
95 if (null == url)
96 {
97 throw new IllegalArgumentException(
98 "Null url"
99 );
100 }
101 this.url = url ;
102 }
103
104 /***
105 * Our url.
106 *
107 */
108 private URL url ;
109
110 /***
111 * Our URL connection.
112 *
113 */
114 private URLConnection conn ;
115
116 /***
117 * Our HTTP connection.
118 *
119 */
120 private HttpURLConnection http ;
121
122 /***
123 * Our underlying stream.
124 *
125 */
126 private OutputStream stream ;
127
128 /***
129 * Open our connection.
130 * @todo Better exception handling ....
131 * @todo Add support for other protocols, ftp etc ...
132 *
133 */
134 public void open()
135 throws IOException
136 {
137 log.debug("");
138 log.debug("FileStoreOutputStream.open()");
139 log.debug(" URL : " + url);
140
141
142 if ("http".equals(url.getProtocol()))
143 {
144 log.debug(" Handling http URL");
145 this.http = (HttpURLConnection) url.openConnection() ;
146 this.http.setAllowUserInteraction(false);
147 this.http.setDoInput(true);
148 this.http.setDoOutput(true) ;
149 this.http.setUseCaches(false);
150 this.http.setRequestMethod("PUT");
151 this.http.setRequestProperty("User-Agent", this.getClass().getName());
152
153
154 this.http.connect();
155
156
157 this.stream = new BufferedOutputStream(
158 this.http.getOutputStream(),
159 BUFFER_SIZE
160 );
161 }
162
163
164 else if ("file".equals(url.getProtocol()))
165 {
166 log.debug(" Handling file URL");
167 log.debug(" Path : " + url.getPath());
168
169
170 this.stream = new FileOutputStream(
171 url.getPath()
172 );
173 }
174
175
176 else {
177 log.debug(" Handling generic URL");
178 this.conn = url.openConnection() ;
179
180
181 this.stream = this.conn.getOutputStream() ;
182 }
183 log.debug(" PASS : Stream open");
184 }
185
186 /***
187 * Close our connection.
188 * @todo Better exception handling ....
189 *
190 */
191 public void close()
192 throws IOException
193 {
194 log.debug("");
195 log.debug("FileStoreOutputStream.close()");
196 log.debug(" URL : " + url);
197 if (null != this.stream)
198 {
199
200
201 this.stream.flush();
202
203
204 this.stream.close();
205
206
207 if (Handler.PROTOCOL.equals(url.getProtocol()))
208 {
209 log.debug(" PASS : Closing mock URL");
210 }
211
212
213 else {
214 log.debug(" PASS : Closing live URL");
215
216
217 if (null != this.http)
218 {
219 int code = this.http.getResponseCode() ;
220 String msg = this.http.getResponseMessage() ;
221 log.debug(" Response code : " + code) ;
222 log.debug(" Response msg : " + msg) ;
223 }
224 }
225 log.debug(" PASS : Stream closed");
226 }
227 else {
228 throw new IOException(
229 "Stream not open"
230 );
231 }
232 }
233
234 /***
235 * OutputStream method ...
236 *
237 */
238 public void flush()
239 throws IOException
240 {
241 if (null != this.stream)
242 {
243 this.stream.flush();
244 }
245 else {
246 throw new IOException(
247 "Stream not open"
248 );
249 }
250 }
251
252 /***
253 * OutputStream method ...
254 *
255 */
256 public void write(byte[] b)
257 throws IOException
258 {
259 if (null != this.stream)
260 {
261 this.stream.write(b);
262 }
263 else {
264 throw new IOException(
265 "Stream not open"
266 );
267 }
268 }
269
270 /***
271 * OutputStream method ...
272 *
273 */
274 public void write(byte[] b, int off, int len)
275 throws IOException
276 {
277 if (null != this.stream)
278 {
279 this.stream.write(b, off, len);
280 }
281 else {
282 throw new IOException(
283 "Stream not open"
284 );
285 }
286 }
287
288 /***
289 * OutputStream method ...
290 *
291 */
292 public void write(int b)
293 throws IOException
294 {
295 if (null != this.stream)
296 {
297 this.stream.write(b);
298 }
299 else {
300 throw new IOException(
301 "Stream not open"
302 );
303 }
304 }
305
306 }