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 */
017package org.apache.commons.vfs2.provider.url;
018
019import java.io.FileNotFoundException;
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.HttpURLConnection;
023import java.net.URL;
024import java.net.URLConnection;
025
026import org.apache.commons.vfs2.FileName;
027import org.apache.commons.vfs2.FileSystemException;
028import org.apache.commons.vfs2.FileType;
029import org.apache.commons.vfs2.provider.AbstractFileName;
030import org.apache.commons.vfs2.provider.AbstractFileObject;
031import org.apache.commons.vfs2.provider.URLFileName;
032
033/**
034 * A {@link org.apache.commons.vfs2.FileObject FileObject} implementation backed by a {@link URL}.
035 * <p>
036 * TODO - Implement set lastModified and get/set attribute
037 * </p>
038 * <p>
039 * TODO - Implement getOutputStream().
040 * </p>
041 */
042public class UrlFileObject extends AbstractFileObject<UrlFileSystem> {
043    private URL url;
044
045    /**
046     * Constructs a new instance.
047     *
048     * @param fileSystem the file system.
049     * @param fileName the file name.
050     */
051    protected UrlFileObject(final UrlFileSystem fileSystem, final AbstractFileName fileName) {
052        super(fileName, fileSystem);
053    }
054
055    /**
056     * Creates a URL from the given file name.
057     *
058     * @param name the file name.
059     * @return a new URL.
060     * @throws IOException if an I/O error occurs.
061     */
062    protected URL createURL(final FileName name) throws IOException {
063        if (name instanceof URLFileName) {
064            final URLFileName urlName = (URLFileName) getName();
065
066            // TODO: charset
067            return new URL(urlName.getURIEncoded(null));
068        }
069        return new URL(getName().getURI());
070    }
071
072    /**
073     * Attaches this file object to its file resource. This method is called before any of the doBlah() or onBlah()
074     * methods. Subclasses can use this method to perform lazy initialization.
075     */
076    @Override
077    protected void doAttach() throws Exception {
078        if (url == null) {
079            // url = new URL(getName().getURI());
080            url = createURL(getName());
081        }
082    }
083
084    /**
085     * Returns the size of the file content (in bytes).
086     */
087    @Override
088    protected long doGetContentSize() throws Exception {
089        final URLConnection conn = url.openConnection();
090        try (InputStream unused = conn.getInputStream()) {
091            return conn.getContentLength();
092        }
093    }
094
095    /**
096     * Creates an input stream to read the file content from.
097     */
098    @Override
099    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}