KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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  */

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

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

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

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

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