001: /*
002: * Copyright 1999-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.19, 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 url)
072: throws IOException {
073: return openConnection(url, null);
074: }
075:
076: public synchronized URLConnection openConnection(URL url, Proxy p)
077: throws IOException {
078:
079: String path;
080: String file = url.getFile();
081: String host = url.getHost();
082:
083: path = ParseUtil.decode(file);
084: path = path.replace('/', '\\');
085: path = path.replace('|', ':');
086:
087: if ((host == null) || host.equals("")
088: || host.equalsIgnoreCase("localhost")
089: || host.equals("~")) {
090: return createFileURLConnection(url, new File(path));
091: }
092:
093: /*
094: * attempt to treat this as a UNC path. See 4180841
095: */
096: path = "\\\\" + host + path;
097: File f = new File(path);
098: if (f.exists()) {
099: return createFileURLConnection(url, f);
100: }
101:
102: /*
103: * Now attempt an ftp connection.
104: */
105: URLConnection uc;
106: URL newurl;
107:
108: try {
109: newurl = new URL("ftp", host, file
110: + (url.getRef() == null ? "" : "#" + url.getRef()));
111: if (p != null) {
112: uc = newurl.openConnection(p);
113: } else {
114: uc = newurl.openConnection();
115: }
116: } catch (IOException e) {
117: uc = null;
118: }
119: if (uc == null) {
120: throw new IOException("Unable to connect to: "
121: + url.toExternalForm());
122: }
123: return uc;
124: }
125:
126: /**
127: * Template method to be overriden by Java Plug-in. [stanleyh]
128: */
129: protected URLConnection createFileURLConnection(URL url, File file) {
130: return new FileURLConnection(url, file);
131: }
132:
133: /**
134: * Compares the host components of two URLs.
135: * @param u1 the URL of the first host to compare
136: * @param u2 the URL of the second host to compare
137: * @return <tt>true</tt> if and only if they
138: * are equal, <tt>false</tt> otherwise.
139: */
140: protected boolean hostsEqual(URL u1, URL u2) {
141: /*
142: * Special case for file: URLs
143: * per RFC 1738 no hostname is equivalent to 'localhost'
144: * i.e. file:///path is equal to file://localhost/path
145: */
146: String s1 = u1.getHost();
147: String s2 = u2.getHost();
148: if ("localhost".equalsIgnoreCase(s1)
149: && (s2 == null || "".equals(s2)))
150: return true;
151: if ("localhost".equalsIgnoreCase(s2)
152: && (s1 == null || "".equals(s1)))
153: return true;
154: return super.hostsEqual(u1, u2);
155: }
156: }
|