001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.url;
018:
019: import org.apache.commons.httpclient.URIException;
020: import org.apache.commons.vfs.FileName;
021: import org.apache.commons.vfs.FileObject;
022: import org.apache.commons.vfs.FileSystemException;
023: import org.apache.commons.vfs.FileType;
024: import org.apache.commons.vfs.provider.AbstractFileObject;
025: import org.apache.commons.vfs.provider.URLFileName;
026:
027: import java.io.FileNotFoundException;
028: import java.io.InputStream;
029: import java.net.HttpURLConnection;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.net.URLConnection;
033:
034: /**
035: * A {@link FileObject} implementation backed by a {@link URL}.
036: *
037: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
038: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
039: * @todo Implement set lastModified and get/set attribute
040: * @todo Implement getOutputStream()
041: */
042: public class UrlFileObject extends AbstractFileObject implements
043: FileObject {
044: private URL url;
045:
046: protected UrlFileObject(final UrlFileSystem fs,
047: final FileName fileName) {
048: super (fileName, fs);
049: }
050:
051: /**
052: * Attaches this file object to its file resource. This method is called
053: * before any of the doBlah() or onBlah() methods. Sub-classes can use
054: * this method to perform lazy initialisation.
055: */
056: protected void doAttach() throws Exception {
057: if (url == null) {
058: // url = new URL(getName().getURI());
059: url = createURL(getName());
060: }
061: }
062:
063: protected URL createURL(final FileName name)
064: throws MalformedURLException, FileSystemException,
065: URIException {
066: if (name instanceof URLFileName) {
067: URLFileName urlName = (URLFileName) getName();
068:
069: // TODO: charset
070: return new URL(urlName.getURIEncoded(null));
071: }
072: return new URL(getName().getURI());
073: }
074:
075: /**
076: * Determines the type of the file.
077: */
078: protected FileType doGetType() throws Exception {
079: try {
080: // Attempt to connect & check status
081: final URLConnection conn = url.openConnection();
082: final InputStream in = conn.getInputStream();
083: try {
084: if (conn instanceof HttpURLConnection) {
085: final int status = ((HttpURLConnection) conn)
086: .getResponseCode();
087: // 200 is good, maybe add more later...
088: if (HttpURLConnection.HTTP_OK != status) {
089: return FileType.IMAGINARY;
090: }
091: }
092:
093: return FileType.FILE;
094: } finally {
095: in.close();
096: }
097: } catch (final FileNotFoundException e) {
098: return FileType.IMAGINARY;
099: }
100: }
101:
102: /**
103: * Returns the size of the file content (in bytes).
104: */
105: protected long doGetContentSize() throws Exception {
106: final URLConnection conn = url.openConnection();
107: final InputStream in = conn.getInputStream();
108: try {
109: return conn.getContentLength();
110: } finally {
111: in.close();
112: }
113: }
114:
115: /**
116: * Returns the last modified time of this file.
117: */
118: protected long doGetLastModifiedTime() throws Exception {
119: final URLConnection conn = url.openConnection();
120: final InputStream in = conn.getInputStream();
121: try {
122: return conn.getLastModified();
123: } finally {
124: in.close();
125: }
126: }
127:
128: /**
129: * Lists the children of the file.
130: */
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: protected InputStream doGetInputStream() throws Exception {
139: return url.openStream();
140: }
141: }
|