KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > http > HttpFileObject


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

16 package org.apache.commons.vfs.provider.http;
17
18 import org.apache.commons.httpclient.Header;
19 import org.apache.commons.httpclient.HttpClient;
20 import org.apache.commons.httpclient.HttpMethod;
21 import org.apache.commons.httpclient.URIException;
22 import org.apache.commons.httpclient.methods.GetMethod;
23 import org.apache.commons.httpclient.methods.HeadMethod;
24 import org.apache.commons.httpclient.util.DateParser;
25 import org.apache.commons.httpclient.util.URIUtil;
26 import org.apache.commons.vfs.FileContentInfoFactory;
27 import org.apache.commons.vfs.FileName;
28 import org.apache.commons.vfs.FileSystemException;
29 import org.apache.commons.vfs.FileType;
30 import org.apache.commons.vfs.RandomAccessContent;
31 import org.apache.commons.vfs.provider.AbstractFileObject;
32 import org.apache.commons.vfs.provider.URLFileName;
33 import org.apache.commons.vfs.util.MonitorInputStream;
34 import org.apache.commons.vfs.util.RandomAccessMode;
35
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.net.HttpURLConnection JavaDoc;
39
40 /**
41  * A file object backed by commons httpclient.
42  *
43  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
44  * @todo status codes
45  */

46 public class HttpFileObject
47     extends AbstractFileObject
48 {
49     private final HttpFileSystem fileSystem;
50     private final String JavaDoc urlCharset;
51     private HeadMethod method;
52
53     protected HttpFileObject(final FileName name,
54                              final HttpFileSystem fileSystem)
55     {
56         super(name, fileSystem);
57         this.fileSystem = fileSystem;
58         urlCharset = HttpFileSystemConfigBuilder.getInstance().getUrlCharset(getFileSystem().getFileSystemOptions());
59     }
60
61     /**
62      * Detaches this file object from its file resource.
63      */

64     protected void doDetach()
65         throws Exception JavaDoc
66     {
67         method = null;
68     }
69
70     /**
71      * Determines the type of this file. Must not return null. The return
72      * value of this method is cached, so the implementation can be expensive.
73      */

74     protected FileType doGetType()
75         throws Exception JavaDoc
76     {
77         // Use the HEAD method to probe the file.
78
method = new HeadMethod();
79         setupMethod(method);
80         final HttpClient client = fileSystem.getClient();
81         final int status = client.executeMethod(method);
82         method.releaseConnection();
83         if (status == HttpURLConnection.HTTP_OK)
84         {
85             return FileType.FILE;
86         }
87         else if (status == HttpURLConnection.HTTP_NOT_FOUND
88             || status == HttpURLConnection.HTTP_GONE)
89         {
90             return FileType.IMAGINARY;
91         }
92         else
93         {
94             throw new FileSystemException("vfs.provider.http/head.error", getName());
95         }
96     }
97
98     /**
99      * Lists the children of this file.
100      */

101     protected String JavaDoc[] doListChildren()
102         throws Exception JavaDoc
103     {
104         throw new Exception JavaDoc("Not implemented.");
105     }
106
107     /**
108      * Returns the size of the file content (in bytes).
109      */

110     protected long doGetContentSize()
111         throws Exception JavaDoc
112     {
113         final Header header = method.getResponseHeader("content-length");
114         if (header == null)
115         {
116             // Assume 0 content-length
117
return 0;
118         }
119         return Integer.parseInt(header.getValue());
120     }
121
122     /**
123      * Returns the last modified time of this file.
124      * <p/>
125      * This implementation throws an exception.
126      */

127     protected long doGetLastModifiedTime()
128         throws Exception JavaDoc
129     {
130         final Header header = method.getResponseHeader("last-modified");
131         if (header == null)
132         {
133             throw new FileSystemException("vfs.provider.http/last-modified.error", getName());
134         }
135         return DateParser.parseDate(header.getValue()).getTime();
136     }
137
138     /**
139      * Creates an input stream to read the file content from. Is only called
140      * if {@link #doGetType} returns {@link FileType#FILE}.
141      * <p/>
142      * <p>It is guaranteed that there are no open output streams for this file
143      * when this method is called.
144      * <p/>
145      * <p>The returned stream does not have to be buffered.
146      */

147     protected InputStream doGetInputStream()
148         throws Exception JavaDoc
149     {
150         final GetMethod getMethod = new GetMethod();
151         setupMethod(getMethod);
152         final int status = fileSystem.getClient().executeMethod(getMethod);
153         if (status != HttpURLConnection.HTTP_OK)
154         {
155             throw new FileSystemException("vfs.provider.http/get.error", getName());
156         }
157
158         return new HttpInputStream(getMethod);
159     }
160
161     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception JavaDoc
162     {
163         return new HttpRandomAccesContent(this, mode);
164     }
165
166     /**
167      * Prepares a Method object.
168      */

169     void setupMethod(final HttpMethod method) throws FileSystemException, URIException
170     {
171         String JavaDoc pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(urlCharset);
172         method.setPath(pathEncoded);
173         method.setFollowRedirects(true);
174         method.setRequestHeader("User-Agent", "Jakarta-Commons-VFS");
175     }
176
177     protected String JavaDoc encodePath(final String JavaDoc decodedPath) throws URIException
178     {
179         String JavaDoc pathEncoded = URIUtil.encodePath(decodedPath);
180         return pathEncoded;
181     }
182
183     /**
184      * An InputStream that cleans up the HTTP connection on close.
185      */

186     static class HttpInputStream
187         extends MonitorInputStream
188     {
189         private final GetMethod method;
190
191         public HttpInputStream(final GetMethod method)
192             throws IOException JavaDoc
193         {
194             super(method.getResponseBodyAsStream());
195             this.method = method;
196         }
197
198         /**
199          * Called after the stream has been closed.
200          */

201         protected void onClose()
202             throws IOException JavaDoc
203         {
204             method.releaseConnection();
205         }
206     }
207
208
209     protected FileContentInfoFactory getFileContentInfoFactory()
210     {
211         return new HttpFileContentInfoFactory();
212     }
213
214     HeadMethod getHeadMethod()
215     {
216         return method;
217     }
218
219     /*
220     protected Map doGetAttributes() throws Exception
221     {
222         TreeMap map = new TreeMap();
223
224         Header contentType = method.getResponseHeader("content-type");
225         if (contentType != null)
226         {
227             HeaderElement[] element = contentType.getValues();
228             if (element != null && element.length > 0)
229             {
230                 map.put("content-type", element[0].getName());
231             }
232         }
233
234         map.put("content-encoding", method.getResponseCharSet());
235         return map;
236     }
237     */

238 }
239
Popular Tags