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
18package org.apache.harmony.luni.internal.net.www.protocol.file;
19
20import java.io.IOException;
21import java.net.Proxy;
22import java.net.URL;
23import java.net.URLConnection;
24import java.net.URLStreamHandler;
25
26import org.apache.harmony.luni.util.Msg;
27
28/**
29 * This is the handler that is responsible for reading files from the file
30 * system.
31 */
32public class Handler extends URLStreamHandler {
33
34    /**
35     * Returns a connection to the a file pointed by this <code>URL</code> in
36     * the file system
37     *
38     * @return A connection to the resource pointed by this url.
39     * @param url
40     *            URL The URL to which the connection is pointing to
41     *
42     */
43    @Override
44    public URLConnection openConnection(URL url) throws IOException {
45        return openConnection(url, null);
46    }
47
48    /**
49     * The behaviour of this method is the same as openConnection(URL).
50     * <code>proxy</code> is not used in FileURLConnection.
51     *
52     * @param url
53     *            the URL which the connection is pointing to
54     * @param proxy
55     *            Proxy
56     * @return a connection to the resource pointed by this url.
57     *
58     * @throws IOException
59     *             if this handler fails to establish a connection.
60     * @throws IllegalArgumentException
61     *             if the url argument is null.
62     * @throws UnsupportedOperationException
63     *             if the protocol handler doesn't support this method.
64     */
65    @Override
66    public URLConnection openConnection(URL url, Proxy proxy)
67            throws IOException {
68        if (null == url) {
69            // K034b=url and proxy can not be null
70            throw new IllegalArgumentException(Msg.getString("K034b")); //$NON-NLS-1$
71        }
72
73        String host = url.getHost();
74        if (host == null || host.length() == 0
75                || host.equalsIgnoreCase("localhost")) { //$NON-NLS-1$
76            return new FileURLConnection(url);
77        }
78
79        // If a hostname is specified try to get the resource using FTP
80        URL ftpURL = new URL("ftp", host, url.getFile()); //$NON-NLS-1$
81        return (proxy == null) ? ftpURL.openConnection() : ftpURL
82                .openConnection(proxy);
83    }
84
85    /**
86     * Parse the <code>string</code>str into <code>URL</code> u which
87     * already have the context properties. The string generally have the
88     * following format: <code><center>/c:/windows/win.ini</center></code>.
89     *
90     * @param u
91     *            The URL object that's parsed into
92     * @param str
93     *            The string equivalent of the specification URL
94     * @param start
95     *            The index in the spec string from which to begin parsing
96     * @param end
97     *            The index to stop parsing
98     *
99     * @see java.net.URLStreamHandler#toExternalForm(URL)
100     * @see java.net.URL
101     */
102    @Override
103    protected void parseURL(URL u, String str, int start, int end) {
104        if (end < start) {
105            return;
106        }
107        String parseString = ""; //$NON-NLS-1$
108        if (start < end) {
109            parseString = str.substring(start, end).replace('\\', '/');
110        }
111        super.parseURL(u, parseString, 0, parseString.length());
112    }
113}
114