View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filestore/common/src/java/org/astrogrid/filestore/common/FileStoreOutputStream.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2005/01/28 10:43:58 $</cvs:date>
5    * <cvs:version>$Revision: 1.3 $</cvs:version>
6    * <cvs:log>
7    *   $Log: FileStoreOutputStream.java,v $
8    *   Revision 1.3  2005/01/28 10:43:58  clq2
9    *   dave_dev_200501141257 (filemanager)
10   *
11   *   Revision 1.2.10.1  2005/01/17 17:19:21  dave
12   *   Fixed bug in FileManagerImpl test (missing '/' in repository path on Unix) ...
13   *   Changed tabs to spaces ..
14   *
15   *   Revision 1.2  2004/11/25 00:19:19  jdt
16   *   Merge from dave-dev-200410061224-200411221626
17   *
18   *   Revision 1.1.2.4  2004/11/09 17:41:36  dave
19   *   Added file:// URL handling to allow server URLs to be tested.
20   *   Added importInit and exportInit to server implementation.
21   *   Moved remaining tests out of extended test abd removed it.
22   *
23   *   Revision 1.1.2.3  2004/10/29 13:21:58  dave
24   *   Added InputStream wrapper ...
25   *
26   *   Revision 1.1.2.2  2004/10/29 12:36:41  dave
27   *   Fixed http connection bug ...
28   *
29   *   Revision 1.1.2.1  2004/10/29 12:22:07  dave
30   *   Added OutputStream wrapper ...
31   *
32   * </cvs:log>
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         // If our URL is a http:// URL.
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 //            connection.setRequestProperty("Content-Type",   "application/octet-stream");
153 //            connection.setRequestProperty("Content-Length", "" + contentLength);
154             this.http.connect();
155             //
156             // Create our stream buffer.
157             this.stream = new BufferedOutputStream(
158                 this.http.getOutputStream(),
159                 BUFFER_SIZE
160                 );
161             }
162         //
163         // If the URL is a file:// URL.
164         else if ("file".equals(url.getProtocol()))
165             {
166             log.debug("  Handling file URL");
167             log.debug("  Path : " + url.getPath());
168             //
169             // Create our file output stream
170             this.stream = new FileOutputStream(
171                 url.getPath()
172                 );
173             }
174         //
175         // If the URL points to a live server.
176         else {
177             log.debug("  Handling generic URL");
178             this.conn = url.openConnection() ;
179             //
180             // Create our stream.
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             // Flush our output stream.
201             this.stream.flush();
202             //
203             // Close our stream.
204             this.stream.close();
205             //
206             // If our URL is a mock
207             if (Handler.PROTOCOL.equals(url.getProtocol()))
208                 {
209                 log.debug("  PASS : Closing mock URL");
210                 }
211             //
212             // If the URL points to a live server.
213             else {
214                 log.debug("  PASS : Closing live URL");
215                 //
216                 // Get the server response.
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     }