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.vfs2.provider.url;
18  
19  import java.io.FileNotFoundException;
20  import java.io.InputStream;
21  import java.net.HttpURLConnection;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.net.URLConnection;
25  
26  import org.apache.commons.httpclient.URIException;
27  import org.apache.commons.vfs2.FileName;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileType;
30  import org.apache.commons.vfs2.provider.AbstractFileName;
31  import org.apache.commons.vfs2.provider.AbstractFileObject;
32  import org.apache.commons.vfs2.provider.URLFileName;
33  
34  /**
35   * A {@link org.apache.commons.vfs2.FileObject FileObject}
36   * implementation backed by a {@link URL}.
37   * <p>
38   * TODO - Implement set lastModified and get/set attribute
39   * <p>
40   * TODO - Implement getOutputStream().
41   */
42  public class UrlFileObject extends AbstractFileObject<UrlFileSystem>
43  {
44      private URL url;
45  
46      protected UrlFileObject(final UrlFileSystem fs,
47                              final AbstractFileName 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      @Override
58      protected void doAttach() throws Exception
59      {
60          if (url == null)
61          {
62              // url = new URL(getName().getURI());
63              url = createURL(getName());
64          }
65      }
66  
67      protected URL createURL(final FileName name) throws MalformedURLException, FileSystemException, URIException
68      {
69          if (name instanceof URLFileName)
70          {
71              final URLFileName urlName = (URLFileName) getName();
72  
73              // TODO: charset
74              return new URL(urlName.getURIEncoded(null));
75          }
76          return new URL(getName().getURI());
77      }
78  
79      /**
80       * Determines the type of the file.
81       */
82      @Override
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     @Override
119     protected long doGetContentSize() throws Exception
120     {
121         final URLConnection conn = url.openConnection();
122         final InputStream in = conn.getInputStream();
123         try
124         {
125             return conn.getContentLength();
126         }
127         finally
128         {
129             in.close();
130         }
131     }
132 
133     /**
134      * Returns the last modified time of this file.
135      */
136     @Override
137     protected long doGetLastModifiedTime()
138         throws Exception
139     {
140         final URLConnection conn = url.openConnection();
141         final InputStream in = conn.getInputStream();
142         try
143         {
144             return conn.getLastModified();
145         }
146         finally
147         {
148             in.close();
149         }
150     }
151 
152     /**
153      * Lists the children of the file.
154      */
155     @Override
156     protected String[] doListChildren() throws Exception
157     {
158         throw new FileSystemException("Not implemented.");
159     }
160 
161     /**
162      * Creates an input stream to read the file content from.
163      */
164     @Override
165     protected InputStream doGetInputStream() throws Exception
166     {
167         return url.openStream();
168     }
169 }