KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > url > UrlFileObject


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.url;
17
18 import org.apache.commons.httpclient.URIException;
19 import org.apache.commons.vfs.FileName;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileType;
23 import org.apache.commons.vfs.provider.AbstractFileObject;
24 import org.apache.commons.vfs.provider.URLFileName;
25
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.HttpURLConnection JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32
33 /**
34  * A {@link FileObject} implementation backed by a {@link URL}.
35  *
36  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
37  * @todo Implement set lastModified and get/set attribute
38  * @todo Implement getOutputStream()
39  */

40 public class UrlFileObject
41     extends AbstractFileObject
42     implements FileObject
43 {
44     private URL JavaDoc url;
45
46     protected UrlFileObject(final UrlFileSystem fs,
47                             final FileName fileName)
48     {
49         super(fileName, fs);
50     }
51
52     /**
53      * Attaches this file object to its file resource. This method is called
54      * before any of the doBlah() or onBlah() methods. Sub-classes can use
55      * this method to perform lazy initialisation.
56      */

57     protected void doAttach() throws Exception JavaDoc
58     {
59         if (url == null)
60         {
61             // url = new URL(getName().getURI());
62
url = createURL(getName());
63         }
64     }
65
66     protected URL JavaDoc createURL(final FileName name) throws MalformedURLException JavaDoc, FileSystemException, URIException
67     {
68         if (name instanceof URLFileName)
69         {
70             URLFileName urlName = (URLFileName) getName();
71
72             // TODO: charset
73
return new URL JavaDoc(urlName.getURIEncoded(null));
74         }
75         return new URL JavaDoc(getName().getURI());
76     }
77
78     /**
79      * Determines the type of the file.
80      */

81     protected FileType doGetType() throws Exception JavaDoc
82     {
83         try
84         {
85             // Attempt to connect & check status
86
final URLConnection JavaDoc conn = url.openConnection();
87             final InputStream JavaDoc in = conn.getInputStream();
88             try
89             {
90                 if (conn instanceof HttpURLConnection JavaDoc)
91                 {
92                     final int status = ((HttpURLConnection JavaDoc) conn).getResponseCode();
93                     // 200 is good, maybe add more later...
94
if (HttpURLConnection.HTTP_OK != status)
95                     {
96                         return FileType.IMAGINARY;
97                     }
98                 }
99
100                 return FileType.FILE;
101             }
102             finally
103             {
104                 in.close();
105             }
106         }
107         catch (final FileNotFoundException JavaDoc e)
108         {
109             return FileType.IMAGINARY;
110         }
111     }
112
113     /**
114      * Returns the size of the file content (in bytes).
115      */

116     protected long doGetContentSize() throws Exception JavaDoc
117     {
118         final URLConnection JavaDoc conn = url.openConnection();
119         final InputStream JavaDoc in = conn.getInputStream();
120         try
121         {
122             return conn.getContentLength();
123         }
124         finally
125         {
126             in.close();
127         }
128     }
129
130     /**
131      * Returns the last modified time of this file.
132      */

133     protected long doGetLastModifiedTime()
134         throws Exception JavaDoc
135     {
136         final URLConnection JavaDoc conn = url.openConnection();
137         final InputStream JavaDoc in = conn.getInputStream();
138         try
139         {
140             return conn.getLastModified();
141         }
142         finally
143         {
144             in.close();
145         }
146     }
147
148     /**
149      * Lists the children of the file.
150      */

151     protected String JavaDoc[] doListChildren() throws Exception JavaDoc
152     {
153         throw new FileSystemException("Not implemented.");
154     }
155
156     /**
157      * Creates an input stream to read the file content from.
158      */

159     protected InputStream JavaDoc doGetInputStream() throws Exception JavaDoc
160     {
161         return url.openStream();
162     }
163 }
164
Popular Tags