📄 handler.java
字号:
/**
* Handler.java
*
* Copyright 2009 Tidal Software. All rights reserved.
*
* Revision History:
* Date Name Action
* ------------------------------------------------
* Feb 12, 2009 wayne Created
*/
package com.tidalsoft.webconsole.sso.http;
import java.io.*;
import java.net.*;
import java.util.*;
import com.tidalsoft.webconsole.sso.*;
/**
* A <code>URLStreamHandler</code> used to provide NTLM authentication
* capabilities to the default HTTP handler. This acts as a wrapper, handling
* authentication and passing control to the underlying stream handler.
*/
public class Handler extends URLStreamHandler {
/**
* The default HTTP port (<code>80</code>).
*/
public static final int DEFAULT_HTTP_PORT = 80;
private static final Map PROTOCOL_HANDLERS = new HashMap();
private static final String PKG_NAME = "com.tidalsoft.webconsole.sso";
/**
* Vendor-specific default packages. If no packages are specified in
* "java.protocol.handler.pkgs", the VM uses one or more default packages,
* which are vendor specific. Sun's is included below for convenience;
* others could be as well. If a particular vendor's package isn't listed,
* it can be specified in "java.protocol.handler.pkgs".
*/
private static final String[] JVM_VENDOR_DEFAULT_PKGS = new String[] { "sun.net.www.protocol" };
private static URLStreamHandlerFactory factory;
/**
* Sets the URL stream handler factory for the environment. This allows
* specification of the factory used in creating underlying stream handlers.
* This can be called once per JVM instance.
*
* @param factory
* The URL stream handler factory.
*/
public static void setURLStreamHandlerFactory(
URLStreamHandlerFactory factory) {
synchronized (PROTOCOL_HANDLERS) {
if (Handler.factory != null) {
throw new IllegalStateException(
"URLStreamHandlerFactory already set.");
}
PROTOCOL_HANDLERS.clear();
Handler.factory = factory;
}
}
/**
* Returns the default HTTP port.
*
* @return An <code>int</code> containing the default HTTP port.
*/
protected int getDefaultPort() {
return DEFAULT_HTTP_PORT;
}
protected URLConnection openConnection(URL url) throws IOException {
try {
url = new URL(url, url.toExternalForm(),
getDefaultStreamHandler(url.getProtocol()));
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
SpnegoHttpURLConnection spnegoConnection = new SpnegoHttpURLConnection(
connection);
spnegoConnection.setUserCred(UserCredentialFactory.getUserCred());
return spnegoConnection;
} catch (Exception ie) {
ie.printStackTrace();
return null;
}
}
private static URLStreamHandler getDefaultStreamHandler(String protocol)
throws IOException {
synchronized (PROTOCOL_HANDLERS) {
URLStreamHandler handler = (URLStreamHandler) PROTOCOL_HANDLERS
.get(protocol);
if (handler != null)
return handler;
if (factory != null) {
handler = factory.createURLStreamHandler(protocol);
}
if (handler == null) {
StringTokenizer tokenizer = new StringTokenizer(PKG_NAME, "|");
while (tokenizer.hasMoreTokens()) {
String provider = tokenizer.nextToken().trim();
if (provider.equals(PKG_NAME))
continue;
String className = provider + "." + protocol + ".Handler";
try {
Class handlerClass = null;
try {
handlerClass = Class.forName(className);
} catch (Exception ex) {
}
if (handlerClass == null) {
handlerClass = ClassLoader.getSystemClassLoader()
.loadClass(className);
}
handler = (URLStreamHandler) handlerClass.newInstance();
break;
} catch (Exception ex) {
}
}
}
if (handler == null) {
for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) {
String className = JVM_VENDOR_DEFAULT_PKGS[i] + "."
+ protocol + ".Handler";
try {
Class handlerClass = null;
try {
handlerClass = Class.forName(className);
} catch (Exception ex) {
}
if (handlerClass == null) {
handlerClass = ClassLoader.getSystemClassLoader()
.loadClass(className);
}
handler = (URLStreamHandler) handlerClass.newInstance();
} catch (Exception ex) {
}
if (handler != null)
break;
}
}
if (handler == null) {
throw new IOException(
"Unable to find default handler for protocol: "
+ protocol);
}
PROTOCOL_HANDLERS.put(protocol, handler);
return handler;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -