001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.net.www.protocol.file;
027:
028: import java.net.InetAddress;
029: import java.net.URLConnection;
030: import java.net.URL;
031: import java.net.Proxy;
032: import java.net.MalformedURLException;
033: import java.net.URLStreamHandler;
034: import java.io.InputStream;
035: import java.io.IOException;
036: import sun.net.www.ParseUtil;
037: import java.io.File;
038:
039: /**
040: * Open an file input stream given a URL.
041: * @author James Gosling
042: * @version 1.57, 07/05/05
043: */
044: public class Handler extends URLStreamHandler {
045:
046: private String getHost(URL url) {
047: String host = url.getHost();
048: if (host == null)
049: host = "";
050: return host;
051: }
052:
053: protected void parseURL(URL u, String spec, int start, int limit) {
054: /*
055: * Ugly backwards compatibility. Flip any file separator
056: * characters to be forward slashes. This is a nop on Unix
057: * and "fixes" win32 file paths. According to RFC 2396,
058: * only forward slashes may be used to represent hierarchy
059: * separation in a URL but previous releases unfortunately
060: * performed this "fixup" behavior in the file URL parsing code
061: * rather than forcing this to be fixed in the caller of the URL
062: * class where it belongs. Since backslash is an "unwise"
063: * character that would normally be encoded if literally intended
064: * as a non-seperator character the damage of veering away from the
065: * specification is presumably limited.
066: */
067: super .parseURL(u, spec.replace(File.separatorChar, '/'), start,
068: limit);
069: }
070:
071: public synchronized URLConnection openConnection(URL u)
072: throws IOException {
073: return openConnection(u, null);
074: }
075:
076: public synchronized URLConnection openConnection(URL u, Proxy p)
077: throws IOException {
078: String host = u.getHost();
079: if (host == null || host.equals("") || host.equals("~")
080: || host.equalsIgnoreCase("localhost")) {
081: File file = new File(ParseUtil.decode(u.getPath()));
082: return createFileURLConnection(u, file);
083: }
084:
085: /* If you reach here, it implies that you have a hostname
086: so attempt an ftp connection.
087: */
088: URLConnection uc;
089: URL ru;
090:
091: try {
092: ru = new URL("ftp", host, u.getFile()
093: + (u.getRef() == null ? "" : "#" + u.getRef()));
094: if (p != null) {
095: uc = ru.openConnection(p);
096: } else {
097: uc = ru.openConnection();
098: }
099: } catch (IOException e) {
100: uc = null;
101: }
102: if (uc == null) {
103: throw new IOException("Unable to connect to: "
104: + u.toExternalForm());
105: }
106: return uc;
107: }
108:
109: // Template method to be overriden by Java Plug-in. [stanleyh]
110: //
111: protected URLConnection createFileURLConnection(URL u, File file) {
112: return new FileURLConnection(u, file);
113: }
114:
115: /**
116: * Compares the host components of two URLs.
117: * @param u1 the URL of the first host to compare
118: * @param u2 the URL of the second host to compare
119: * @return <tt>true</tt> if and only if they
120: * are equal, <tt>false</tt> otherwise.
121: */
122: protected boolean hostsEqual(URL u1, URL u2) {
123: /*
124: * Special case for file: URLs
125: * per RFC 1738 no hostname is equivalent to 'localhost'
126: * i.e. file:///path is equal to file://localhost/path
127: */
128: String s1 = u1.getHost();
129: String s2 = u2.getHost();
130: if ("localhost".equalsIgnoreCase(s1)
131: && (s2 == null || "".equals(s2)))
132: return true;
133: if ("localhost".equalsIgnoreCase(s2)
134: && (s1 == null || "".equals(s1)))
135: return true;
136: return super.hostsEqual(u1, u2);
137: }
138: }
|