001: /*
002: * @(#)Handler.java 1.16 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.net.www.protocol.file;
029:
030: import java.net.InetAddress;
031: import java.net.URLConnection;
032: import java.net.URL;
033: import java.net.MalformedURLException;
034: import java.net.URLStreamHandler;
035: import java.io.InputStream;
036: import java.io.IOException;
037: import sun.net.www.ParseUtil;
038: import java.io.File;
039:
040: /**
041: * Open an file input stream given a URL.
042: * @author James Gosling
043: * @version 1.41, 99/12/04
044: */
045: public class Handler extends URLStreamHandler {
046:
047: private String getHost(URL url) {
048: String host = url.getHost();
049: if (host == null)
050: host = "";
051: return host;
052: }
053:
054: // Fix for bug 6240398: This method was added to preserve UNC file
055: // paths. URLStreamHandler did not handle it then. But
056: // with j2se1.4.2 updates, parent class URLStreamHandler handles UNC
057: // file paths. Hence we don't need this.
058: /*
059: protected String toExternalForm(URL u) {
060: StringBuffer result = new StringBuffer(u.getProtocol());
061: result.append(":");
062: if (u.getAuthority() != null) {
063: result.append("//");
064: result.append(u.getAuthority());
065: } else {
066: result.append("//");
067: }
068: if (u.getFile() != null) {
069: result.append(u.getFile());
070: }
071: if (u.getRef() != null) {
072: result.append("#");
073: result.append(u.getRef());
074: }
075: return result.toString();
076: }
077: */
078:
079: protected void parseURL(URL u, String spec, int start, int limit) {
080: /*
081: * Ugly backwards compatibility. Flip any file separator
082: * characters to be forward slashes. This is a nop on Unix
083: * and "fixes" win32 file paths. According to RFC 2396,
084: * only forward slashes may be used to represent hierarchy
085: * separation in a URL but previous releases unfortunately
086: * performed this "fixup" behavior in the file URL parsing code
087: * rather than forcing this to be fixed in the caller of the URL
088: * class where it belongs. Since backslash is an "unwise"
089: * character that would normally be encoded if literally intended
090: * as a non-seperator character the damage of veering away from the
091: * specification is presumably limited.
092: */
093: super .parseURL(u, spec.replace(File.separatorChar, '/'), start,
094: limit);
095: }
096:
097: public synchronized URLConnection openConnection(URL url)
098: throws IOException {
099:
100: String path;
101: String file = url.getFile();
102: String host = url.getHost();
103:
104: path = ParseUtil.decode(file);
105: path = path.replace('/', '\\');
106: path = path.replace('|', ':');
107:
108: if ((host == null) || host.equals("")
109: || host.equalsIgnoreCase("localhost")
110: || host.equals("~")) {
111: return createFileURLConnection(url, new File(path));
112: }
113:
114: /*
115: * attempt to treat this as a UNC path. See 4180841
116: */
117: path = "\\\\" + host + path;
118: File f = new File(path);
119: if (f.exists()) {
120: return createFileURLConnection(url, f);
121: }
122:
123: /*
124: * Now attempt an ftp connection.
125: */
126: /* Commented out, because CDC/Foundation doesn't support FTP
127: URLConnection uc;
128: URL newurl;
129:
130: try {
131: newurl = new URL("ftp", host, file +
132: (url.getRef() == null ? "":
133: "#" + url.getRef()));
134: uc = newurl.openConnection();
135: } catch (IOException e) {
136: uc = null;
137: }
138: if (uc == null) {
139: throw new IOException("Unable to connect to: " +
140: url.toExternalForm());
141: }
142: return uc;
143: */
144: throw new IOException("URL connection with specified hostname "
145: + "is not supported. " + "Only localhost is supported.");
146: }
147:
148: /**
149: * Template method to be overriden by Java Plug-in. [stanleyh]
150: */
151: protected URLConnection createFileURLConnection(URL url, File file) {
152: return new FileURLConnection(url, file);
153: }
154: }
|