001: /* jcifs smb client library in Java
002: * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
003: * "Eric Glass" <jcifs at samba dot org>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package jcifs.http;
021:
022: import java.io.IOException;
023:
024: import java.net.HttpURLConnection;
025: import java.net.URL;
026: import java.net.URLConnection;
027: import java.net.URLStreamHandler;
028: import java.net.URLStreamHandlerFactory;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.StringTokenizer;
033:
034: /**
035: * A <code>URLStreamHandler</code> used to provide NTLM authentication
036: * capabilities to the default HTTP handler. This acts as a wrapper,
037: * handling authentication and passing control to the underlying
038: * stream handler.
039: */
040: public class Handler extends URLStreamHandler {
041:
042: /**
043: * The default HTTP port (<code>80</code>).
044: */
045: public static final int DEFAULT_HTTP_PORT = 80;
046:
047: private static final Map PROTOCOL_HANDLERS = new HashMap();
048:
049: private static final String HANDLER_PKGS_PROPERTY = "java.protocol.handler.pkgs";
050:
051: /**
052: * Vendor-specific default packages. If no packages are specified in
053: * "java.protocol.handler.pkgs", the VM uses one or more default
054: * packages, which are vendor specific. Sun's is included below
055: * for convenience; others could be as well. If a particular vendor's
056: * package isn't listed, it can be specified in
057: * "java.protocol.handler.pkgs".
058: */
059: private static final String[] JVM_VENDOR_DEFAULT_PKGS = new String[] { "sun.net.www.protocol" };
060:
061: private static URLStreamHandlerFactory factory;
062:
063: /**
064: * Sets the URL stream handler factory for the environment. This
065: * allows specification of the factory used in creating underlying
066: * stream handlers. This can be called once per JVM instance.
067: *
068: * @param factory The URL stream handler factory.
069: */
070: public static void setURLStreamHandlerFactory(
071: URLStreamHandlerFactory factory) {
072: synchronized (PROTOCOL_HANDLERS) {
073: if (Handler.factory != null) {
074: throw new IllegalStateException(
075: "URLStreamHandlerFactory already set.");
076: }
077: PROTOCOL_HANDLERS.clear();
078: Handler.factory = factory;
079: }
080: }
081:
082: /**
083: * Returns the default HTTP port.
084: *
085: * @return An <code>int</code> containing the default HTTP port.
086: */
087: protected int getDefaultPort() {
088: return DEFAULT_HTTP_PORT;
089: }
090:
091: protected URLConnection openConnection(URL url) throws IOException {
092: url = new URL(url, url.toExternalForm(),
093: getDefaultStreamHandler(url.getProtocol()));
094: return new NtlmHttpURLConnection((HttpURLConnection) url
095: .openConnection());
096: }
097:
098: private static URLStreamHandler getDefaultStreamHandler(
099: String protocol) throws IOException {
100: synchronized (PROTOCOL_HANDLERS) {
101: URLStreamHandler handler = (URLStreamHandler) PROTOCOL_HANDLERS
102: .get(protocol);
103: if (handler != null)
104: return handler;
105: if (factory != null) {
106: handler = factory.createURLStreamHandler(protocol);
107: }
108: if (handler == null) {
109: String path = System.getProperty(HANDLER_PKGS_PROPERTY);
110: StringTokenizer tokenizer = new StringTokenizer(path,
111: "|");
112: while (tokenizer.hasMoreTokens()) {
113: String provider = tokenizer.nextToken().trim();
114: if (provider.equals("jcifs"))
115: continue;
116: String className = provider + "." + protocol
117: + ".Handler";
118: try {
119: Class handlerClass = null;
120: try {
121: handlerClass = Class.forName(className);
122: } catch (Exception ex) {
123: }
124: if (handlerClass == null) {
125: handlerClass = ClassLoader
126: .getSystemClassLoader().loadClass(
127: className);
128: }
129: handler = (URLStreamHandler) handlerClass
130: .newInstance();
131: break;
132: } catch (Exception ex) {
133: }
134: }
135: }
136: if (handler == null) {
137: for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) {
138: String className = JVM_VENDOR_DEFAULT_PKGS[i] + "."
139: + protocol + ".Handler";
140: try {
141: Class handlerClass = null;
142: try {
143: handlerClass = Class.forName(className);
144: } catch (Exception ex) {
145: }
146: if (handlerClass == null) {
147: handlerClass = ClassLoader
148: .getSystemClassLoader().loadClass(
149: className);
150: }
151: handler = (URLStreamHandler) handlerClass
152: .newInstance();
153: } catch (Exception ex) {
154: }
155: if (handler != null)
156: break;
157: }
158: }
159: if (handler == null) {
160: throw new IOException(
161: "Unable to find default handler for protocol: "
162: + protocol);
163: }
164: PROTOCOL_HANDLERS.put(protocol, handler);
165: return handler;
166: }
167: }
168:
169: }
|