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