View Javadoc

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.commons.vfs.provider.url;
18  
19  import org.apache.commons.httpclient.URIException;
20  import org.apache.commons.vfs.FileName;
21  import org.apache.commons.vfs.FileObject;
22  import org.apache.commons.vfs.FileSystemException;
23  import org.apache.commons.vfs.FileType;
24  import org.apache.commons.vfs.provider.AbstractFileObject;
25  import org.apache.commons.vfs.provider.URLFileName;
26  
27  import java.io.FileNotFoundException;
28  import java.io.InputStream;
29  import java.net.HttpURLConnection;
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.net.URLConnection;
33  
34  /***
35   * A {@link FileObject} implementation backed by a {@link URL}.
36   *
37   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
38   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
39   * @todo Implement set lastModified and get/set attribute
40   * @todo Implement getOutputStream()
41   */
42  public class UrlFileObject
43      extends AbstractFileObject
44      implements FileObject
45  {
46      private URL url;
47  
48      protected UrlFileObject(final UrlFileSystem fs,
49                              final FileName fileName)
50      {
51          super(fileName, fs);
52      }
53  
54      /***
55       * Attaches this file object to its file resource.  This method is called
56       * before any of the doBlah() or onBlah() methods.  Sub-classes can use
57       * this method to perform lazy initialisation.
58       */
59      protected void doAttach() throws Exception
60      {
61          if (url == null)
62          {
63              // url = new URL(getName().getURI());
64              url = createURL(getName());
65          }
66      }
67  
68      protected URL createURL(final FileName name) throws MalformedURLException, FileSystemException, URIException
69      {
70          if (name instanceof URLFileName)
71          {
72              URLFileName urlName = (URLFileName) getName();
73  
74              // TODO: charset
75              return new URL(urlName.getURIEncoded(null));
76          }
77          return new URL(getName().getURI());
78      }
79  
80      /***
81       * Determines the type of the file.
82       */
83      protected FileType doGetType() throws Exception
84      {
85          try
86          {
87              // Attempt to connect & check status
88              final URLConnection conn = url.openConnection();
89              final InputStream in = conn.getInputStream();
90              try
91              {
92                  if (conn instanceof HttpURLConnection)
93                  {
94                      final int status = ((HttpURLConnection) conn).getResponseCode();
95                      // 200 is good, maybe add more later...
96                      if (HttpURLConnection.HTTP_OK != status)
97                      {
98                          return FileType.IMAGINARY;
99                      }
100                 }
101 
102                 return FileType.FILE;
103             }
104             finally
105             {
106                 in.close();
107             }
108         }
109         catch (final FileNotFoundException e)
110         {
111             return FileType.IMAGINARY;
112         }
113     }
114 
115     /***
116      * Returns the size of the file content (in bytes).
117      */
118     protected long doGetContentSize() throws Exception
119     {
120         final URLConnection conn = url.openConnection();
121         final InputStream in = conn.getInputStream();
122         try
123         {
124             return conn.getContentLength();
125         }
126         finally
127         {
128             in.close();
129         }
130     }
131 
132     /***
133      * Returns the last modified time of this file.
134      */
135     protected long doGetLastModifiedTime()
136         throws Exception
137     {
138         final URLConnection conn = url.openConnection();
139         final InputStream in = conn.getInputStream();
140         try
141         {
142             return conn.getLastModified();
143         }
144         finally
145         {
146             in.close();
147         }
148     }
149 
150     /***
151      * Lists the children of the file.
152      */
153     protected String[] doListChildren() throws Exception
154     {
155         throw new FileSystemException("Not implemented.");
156     }
157 
158     /***
159      * Creates an input stream to read the file content from.
160      */
161     protected InputStream doGetInputStream() throws Exception
162     {
163         return url.openStream();
164     }
165 }