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} implementation backed by a {@link URL}.
36   * <p>
37   * TODO - Implement set lastModified and get/set attribute
38   * </p>
39   * <p>
40   * TODO - Implement getOutputStream().
41   * </p>
42   */
43  public class UrlFileObject extends AbstractFileObject<UrlFileSystem> {
44      private URL url;
45  
46      protected UrlFileObject(final UrlFileSystem fs, final AbstractFileName fileName) {
47          super(fileName, fs);
48      }
49  
50      /**
51       * Attaches this file object to its file resource. This method is called before any of the doBlah() or onBlah()
52       * methods. Sub-classes can use this method to perform lazy initialisation.
53       */
54      @Override
55      protected void doAttach() throws Exception {
56          if (url == null) {
57              // url = new URL(getName().getURI());
58              url = createURL(getName());
59          }
60      }
61  
62      protected URL createURL(final FileName name) throws MalformedURLException, FileSystemException, URIException {
63          if (name instanceof URLFileName) {
64              final URLFileName../../../../org/apache/commons/vfs2/provider/URLFileName.html#URLFileName">URLFileName urlName = (URLFileName) getName();
65  
66              // TODO: charset
67              return new URL(urlName.getURIEncoded(null));
68          }
69          return new URL(getName().getURI());
70      }
71  
72      /**
73       * Determines the type of the file.
74       */
75      @Override
76      protected FileType doGetType() throws Exception {
77          try {
78              // Attempt to connect & check status
79              final URLConnection conn = url.openConnection();
80              final InputStream in = conn.getInputStream();
81              try {
82                  if (conn instanceof HttpURLConnection) {
83                      final int status = ((HttpURLConnection) conn).getResponseCode();
84                      // 200 is good, maybe add more later...
85                      if (HttpURLConnection.HTTP_OK != status) {
86                          return FileType.IMAGINARY;
87                      }
88                  }
89  
90                  return FileType.FILE;
91              } finally {
92                  in.close();
93              }
94          } catch (final FileNotFoundException e) {
95              return FileType.IMAGINARY;
96          }
97      }
98  
99      /**
100      * Returns the size of the file content (in bytes).
101      */
102     @Override
103     protected long doGetContentSize() throws Exception {
104         final URLConnection conn = url.openConnection();
105         final InputStream in = conn.getInputStream();
106         try {
107             return conn.getContentLength();
108         } finally {
109             in.close();
110         }
111     }
112 
113     /**
114      * Returns the last modified time of this file.
115      */
116     @Override
117     protected long doGetLastModifiedTime() throws Exception {
118         final URLConnection conn = url.openConnection();
119         final InputStream in = conn.getInputStream();
120         try {
121             return conn.getLastModified();
122         } finally {
123             in.close();
124         }
125     }
126 
127     /**
128      * Lists the children of the file.
129      */
130     @Override
131     protected String[] doListChildren() throws Exception {
132         throw new FileSystemException("Not implemented.");
133     }
134 
135     /**
136      * Creates an input stream to read the file content from.
137      */
138     @Override
139     protected InputStream doGetInputStream() throws Exception {
140         return url.openStream();
141     }
142 }