KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > http > Handler


1 /* jcifs smb client library in Java
2  * Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package com.knowgate.jcifs.http;
21
22 import java.io.IOException JavaDoc;
23
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.net.URLStreamHandler JavaDoc;
28 import java.net.URLStreamHandlerFactory JavaDoc;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 /**
35  * A <code>URLStreamHandler</code> used to provide NTLM authentication
36  * capabilities to the default HTTP handler. This acts as a wrapper,
37  * handling authentication and passing control to the underlying
38  * stream handler.
39  * @version 0.9.1
40  */

41 public class Handler extends URLStreamHandler JavaDoc {
42
43     /**
44      * The default HTTP port (<code>80</code>).
45      */

46     public static final int DEFAULT_HTTP_PORT = 80;
47
48     private static final Map JavaDoc PROTOCOL_HANDLERS = new HashMap JavaDoc();
49
50     private static final String JavaDoc HANDLER_PKGS_PROPERTY =
51             "java.protocol.handler.pkgs";
52
53     /**
54      * Vendor-specific default packages. If no packages are specified in
55      * "java.protocol.handler.pkgs", the VM uses one or more default
56      * packages, which are vendor specific. Sun's is included below
57      * for convenience; others could be as well. If a particular vendor's
58      * package isn't listed, it can be specified in
59      * "java.protocol.handler.pkgs".
60      */

61     private static final String JavaDoc[] JVM_VENDOR_DEFAULT_PKGS = new String JavaDoc[] {
62         "sun.net.www.protocol"
63     };
64
65     private static URLStreamHandlerFactory JavaDoc factory;
66
67     /**
68      * Sets the URL stream handler factory for the environment. This
69      * allows specification of the factory used in creating underlying
70      * stream handlers. This can be called once per JVM instance.
71      *
72      * @param factory The URL stream handler factory.
73      */

74     public static void setURLStreamHandlerFactory(
75             URLStreamHandlerFactory JavaDoc factory) {
76         synchronized (PROTOCOL_HANDLERS) {
77             if (Handler.factory != null) {
78                 throw new IllegalStateException JavaDoc(
79                         "URLStreamHandlerFactory already set.");
80             }
81             PROTOCOL_HANDLERS.clear();
82             Handler.factory = factory;
83         }
84     }
85
86     /**
87      * Returns the default HTTP port.
88      *
89      * @return An <code>int</code> containing the default HTTP port.
90      */

91     protected int getDefaultPort() {
92         return DEFAULT_HTTP_PORT;
93     }
94
95     protected URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc {
96         url = new URL JavaDoc(url, url.toExternalForm(),
97                 getDefaultStreamHandler(url.getProtocol()));
98         return new NtlmHttpURLConnection((HttpURLConnection JavaDoc)
99                 url.openConnection());
100     }
101
102     private static URLStreamHandler JavaDoc getDefaultStreamHandler(String JavaDoc protocol)
103             throws IOException JavaDoc {
104         synchronized (PROTOCOL_HANDLERS) {
105             URLStreamHandler JavaDoc handler = (URLStreamHandler JavaDoc)
106                     PROTOCOL_HANDLERS.get(protocol);
107             if (handler != null) return handler;
108             if (factory != null) {
109                 handler = factory.createURLStreamHandler(protocol);
110             }
111             if (handler == null) {
112                 String JavaDoc path = System.getProperty(HANDLER_PKGS_PROPERTY);
113                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(path, "|");
114                 while (tokenizer.hasMoreTokens()) {
115                     String JavaDoc provider = tokenizer.nextToken().trim();
116                     if (provider.equals("jcifs")) continue;
117                     String JavaDoc className = provider + "." + protocol + ".Handler";
118                     try {
119                         Class JavaDoc handlerClass = null;
120                         try {
121                             handlerClass = Class.forName(className);
122                         } catch (Exception JavaDoc ex) { }
123                         if (handlerClass == null) {
124                             handlerClass = ClassLoader.getSystemClassLoader(
125                                     ).loadClass(className);
126                         }
127                         handler = (URLStreamHandler JavaDoc) handlerClass.newInstance();
128                         break;
129                     } catch (Exception JavaDoc ex) { }
130                 }
131             }
132             if (handler == null) {
133                 for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) {
134                     String JavaDoc className = JVM_VENDOR_DEFAULT_PKGS[i] + "." +
135                             protocol + ".Handler";
136                     try {
137                         Class JavaDoc handlerClass = null;
138                         try {
139                             handlerClass = Class.forName(className);
140                         } catch (Exception JavaDoc ex) { }
141                         if (handlerClass == null) {
142                             handlerClass = ClassLoader.getSystemClassLoader(
143                                     ).loadClass(className);
144                         }
145                         handler = (URLStreamHandler JavaDoc) handlerClass.newInstance();
146                     } catch (Exception JavaDoc ex) { }
147                     if (handler != null) break;
148                 }
149             }
150             if (handler == null) {
151                 throw new IOException JavaDoc(
152                         "Unable to find default handler for protocol: " +
153                                 protocol);
154             }
155             PROTOCOL_HANDLERS.put(protocol, handler);
156             return handler;
157         }
158     }
159
160 }
161
Popular Tags