1 19 20 package jcifs.http; 21 22 import java.io.IOException ; 23 24 import java.net.HttpURLConnection ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.URLStreamHandler ; 28 import java.net.URLStreamHandlerFactory ; 29 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 34 40 public class Handler extends URLStreamHandler { 41 42 45 public static final int DEFAULT_HTTP_PORT = 80; 46 47 private static final Map PROTOCOL_HANDLERS = new HashMap (); 48 49 private static final String HANDLER_PKGS_PROPERTY = 50 "java.protocol.handler.pkgs"; 51 52 60 private static final String [] JVM_VENDOR_DEFAULT_PKGS = new String [] { 61 "sun.net.www.protocol" 62 }; 63 64 private static URLStreamHandlerFactory factory; 65 66 73 public static void setURLStreamHandlerFactory( 74 URLStreamHandlerFactory factory) { 75 synchronized (PROTOCOL_HANDLERS) { 76 if (Handler.factory != null) { 77 throw new IllegalStateException ( 78 "URLStreamHandlerFactory already set."); 79 } 80 PROTOCOL_HANDLERS.clear(); 81 Handler.factory = factory; 82 } 83 } 84 85 90 protected int getDefaultPort() { 91 return DEFAULT_HTTP_PORT; 92 } 93 94 protected URLConnection openConnection(URL url) throws IOException { 95 url = new URL (url, url.toExternalForm(), 96 getDefaultStreamHandler(url.getProtocol())); 97 return new NtlmHttpURLConnection((HttpURLConnection ) 98 url.openConnection()); 99 } 100 101 private static URLStreamHandler getDefaultStreamHandler(String protocol) 102 throws IOException { 103 synchronized (PROTOCOL_HANDLERS) { 104 URLStreamHandler handler = (URLStreamHandler ) 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 path = System.getProperty(HANDLER_PKGS_PROPERTY); 112 StringTokenizer tokenizer = new StringTokenizer (path, "|"); 113 while (tokenizer.hasMoreTokens()) { 114 String provider = tokenizer.nextToken().trim(); 115 if (provider.equals("jcifs")) continue; 116 String className = provider + "." + protocol + ".Handler"; 117 try { 118 Class handlerClass = null; 119 try { 120 handlerClass = Class.forName(className); 121 } catch (Exception ex) { } 122 if (handlerClass == null) { 123 handlerClass = ClassLoader.getSystemClassLoader( 124 ).loadClass(className); 125 } 126 handler = (URLStreamHandler ) handlerClass.newInstance(); 127 break; 128 } catch (Exception ex) { } 129 } 130 } 131 if (handler == null) { 132 for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) { 133 String className = JVM_VENDOR_DEFAULT_PKGS[i] + "." + 134 protocol + ".Handler"; 135 try { 136 Class handlerClass = null; 137 try { 138 handlerClass = Class.forName(className); 139 } catch (Exception ex) { } 140 if (handlerClass == null) { 141 handlerClass = ClassLoader.getSystemClassLoader( 142 ).loadClass(className); 143 } 144 handler = (URLStreamHandler ) handlerClass.newInstance(); 145 } catch (Exception ex) { } 146 if (handler != null) break; 147 } 148 } 149 if (handler == null) { 150 throw new IOException ( 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 |