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