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 initialization.
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              try (InputStream in = conn.getInputStream()) {
81                  if (conn instanceof HttpURLConnection) {
82                      final int status = ((HttpURLConnection) conn).getResponseCode();
83                      // 200 is good, maybe add more later...
84                      if (HttpURLConnection.HTTP_OK != status) {
85                          return FileType.IMAGINARY;
86                      }
87                  }
88  
89                  return FileType.FILE;
90              }
91          } catch (final FileNotFoundException e) {
92              return FileType.IMAGINARY;
93          }
94      }
95  
96      /**
97       * Returns the size of the file content (in bytes).
98       */
99      @Override
100     protected long doGetContentSize() throws Exception {
101         final URLConnection conn = url.openConnection();
102         try (InputStream in = conn.getInputStream()) {
103             return conn.getContentLength();
104         }
105     }
106 
107     /**
108      * Returns the last modified time of this file.
109      */
110     @Override
111     protected long doGetLastModifiedTime() throws Exception {
112         final URLConnection conn = url.openConnection();
113         try (InputStream in = conn.getInputStream()) {
114             return conn.getLastModified();
115         }
116     }
117 
118     /**
119      * Lists the children of the file.
120      */
121     @Override
122     protected String[] doListChildren() throws Exception {
123         throw new FileSystemException("Not implemented.");
124     }
125 
126     /**
127      * Creates an input stream to read the file content from.
128      */
129     @Override
130     protected InputStream doGetInputStream(final int bufferSize) throws Exception {
131         return url.openStream();
132     }
133 }