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 com.knowgate.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: * @version 0.9.1
040: */
041: public class Handler extends URLStreamHandler {
042:
043: /**
044: * The default HTTP port (<code>80</code>).
045: */
046: public static final int DEFAULT_HTTP_PORT = 80;
047:
048: private static final Map PROTOCOL_HANDLERS = new HashMap();
049:
050: private static final String HANDLER_PKGS_PROPERTY = "java.protocol.handler.pkgs";
051:
052: /**
053: * Vendor-specific default packages. If no packages are specified in
054: * "java.protocol.handler.pkgs", the VM uses one or more default
055: * packages, which are vendor specific. Sun's is included below
056: * for convenience; others could be as well. If a particular vendor's
057: * package isn't listed, it can be specified in
058: * "java.protocol.handler.pkgs".
059: */
060: private static final String[] JVM_VENDOR_DEFAULT_PKGS = new String[] { "sun.net.www.protocol" };
061:
062: private static URLStreamHandlerFactory factory;
063:
064: /**
065: * Sets the URL stream handler factory for the environment. This
066: * allows specification of the factory used in creating underlying
067: * stream handlers. This can be called once per JVM instance.
068: *
069: * @param factory The URL stream handler factory.
070: */
071: public static void setURLStreamHandlerFactory(
072: URLStreamHandlerFactory factory) {
073: synchronized (PROTOCOL_HANDLERS) {
074: if (Handler.factory != null) {
075: throw new IllegalStateException(
076: "URLStreamHandlerFactory already set.");
077: }
078: PROTOCOL_HANDLERS.clear();
079: Handler.factory = factory;
080: }
081: }
082:
083: /**
084: * Returns the default HTTP port.
085: *
086: * @return An <code>int</code> containing the default HTTP port.
087: */
088: protected int getDefaultPort() {
089: return DEFAULT_HTTP_PORT;
090: }
091:
092: protected URLConnection openConnection(URL url) throws IOException {
093: url = new URL(url, url.toExternalForm(),
094: getDefaultStreamHandler(url.getProtocol()));
095: return new NtlmHttpURLConnection((HttpURLConnection) url
096: .openConnection());
097: }
098:
099: private static URLStreamHandler getDefaultStreamHandler(
100: String protocol) throws IOException {
101: synchronized (PROTOCOL_HANDLERS) {
102: URLStreamHandler handler = (URLStreamHandler) PROTOCOL_HANDLERS
103: .get(protocol);
104: if (handler != null)
105: return handler;
106: if (factory != null) {
107: handler = factory.createURLStreamHandler(protocol);
108: }
109: if (handler == null) {
110: String path = System.getProperty(HANDLER_PKGS_PROPERTY);
111: StringTokenizer tokenizer = new StringTokenizer(path,
112: "|");
113: while (tokenizer.hasMoreTokens()) {
114: String provider = tokenizer.nextToken().trim();
115: if (provider.equals("jcifs"))
116: continue;
117: String className = provider + "." + protocol
118: + ".Handler";
119: try {
120: Class handlerClass = null;
121: try {
122: handlerClass = Class.forName(className);
123: } catch (Exception ex) {
124: }
125: if (handlerClass == null) {
126: handlerClass = ClassLoader
127: .getSystemClassLoader().loadClass(
128: className);
129: }
130: handler = (URLStreamHandler) handlerClass
131: .newInstance();
132: break;
133: } catch (Exception ex) {
134: }
135: }
136: }
137: if (handler == null) {
138: for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) {
139: String className = JVM_VENDOR_DEFAULT_PKGS[i] + "."
140: + protocol + ".Handler";
141: try {
142: Class handlerClass = null;
143: try {
144: handlerClass = Class.forName(className);
145: } catch (Exception ex) {
146: }
147: if (handlerClass == null) {
148: handlerClass = ClassLoader
149: .getSystemClassLoader().loadClass(
150: className);
151: }
152: handler = (URLStreamHandler) handlerClass
153: .newInstance();
154: } catch (Exception ex) {
155: }
156: if (handler != null)
157: break;
158: }
159: }
160: if (handler == null) {
161: throw new IOException(
162: "Unable to find default handler for protocol: "
163: + protocol);
164: }
165: PROTOCOL_HANDLERS.put(protocol, handler);
166: return handler;
167: }
168: }
169:
170: }
|