📄 ntlmhttpurlconnection.java
字号:
/* jcifs smb client library in Java
* Copyright (C) 2002 "Michael B. Allen" <jcifs at samba dot org>
* "Eric Glass" <jcifs at samba dot org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package jcifs.http;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import jcifs.Config;
import jcifs.ntlmssp.NtlmFlags;
import jcifs.ntlmssp.NtlmMessage;
import jcifs.ntlmssp.Type1Message;
import jcifs.ntlmssp.Type2Message;
import jcifs.ntlmssp.Type3Message;
import jcifs.util.Base64;
/**
* Wraps an <code>HttpURLConnection</code> to provide NTLM authentication
* services.
*
* Please read <a href="../../../httpclient.html">Using jCIFS NTLM Authentication for HTTP Connections</a>.
*/
public class NtlmHttpURLConnection extends HttpURLConnection {
private static final int MAX_REDIRECTS =
Integer.parseInt(System.getProperty("http.maxRedirects", "20"));
private static final int LM_COMPATIBILITY =
Config.getInt("jcifs.smb.lmCompatibility", 0);
private static final String DEFAULT_DOMAIN;
private HttpURLConnection connection;
private Map requestProperties;
private Map headerFields;
private ByteArrayOutputStream cachedOutput;
private String authProperty;
private String authMethod;
private boolean handshakeComplete;
static {
String domain = System.getProperty("http.auth.ntlm.domain");
if (domain == null) domain = Type3Message.getDefaultDomain();
DEFAULT_DOMAIN = domain;
}
public NtlmHttpURLConnection(HttpURLConnection connection) {
super(connection.getURL());
this.connection = connection;
requestProperties = new HashMap();
}
public void connect() throws IOException {
if (connected) return;
connection.connect();
connected = true;
}
private void handshake() throws IOException {
if (handshakeComplete) return;
doHandshake();
handshakeComplete = true;
}
public URL getURL() {
return connection.getURL();
}
public int getContentLength() {
try {
handshake();
} catch (IOException ex) { }
return connection.getContentLength();
}
public String getContentType() {
try {
handshake();
} catch (IOException ex) { }
return connection.getContentType();
}
public String getContentEncoding() {
try {
handshake();
} catch (IOException ex) { }
return connection.getContentEncoding();
}
public long getExpiration() {
try {
handshake();
} catch (IOException ex) { }
return connection.getExpiration();
}
public long getDate() {
try {
handshake();
} catch (IOException ex) { }
return connection.getDate();
}
public long getLastModified() {
try {
handshake();
} catch (IOException ex) { }
return connection.getLastModified();
}
public String getHeaderField(String header) {
try {
handshake();
} catch (IOException ex) { }
return connection.getHeaderField(header);
}
private Map getHeaderFields0() {
if (headerFields != null) return headerFields;
Map map = new HashMap();
String key = connection.getHeaderFieldKey(0);
String value = connection.getHeaderField(0);
for (int i = 1; key != null || value != null; i++) {
List values = (List) map.get(key);
if (values == null) {
values = new ArrayList();
map.put(key, values);
}
values.add(value);
key = connection.getHeaderFieldKey(i);
value = connection.getHeaderField(i);
}
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
entry.setValue(Collections.unmodifiableList((List)
entry.getValue()));
}
return (headerFields = Collections.unmodifiableMap(map));
}
public Map getHeaderFields() {
if (headerFields != null) return headerFields;
try {
handshake();
} catch (IOException ex) { }
return getHeaderFields0();
}
public int getHeaderFieldInt(String header, int def) {
try {
handshake();
} catch (IOException ex) { }
return connection.getHeaderFieldInt(header, def);
}
public long getHeaderFieldDate(String header, long def) {
try {
handshake();
} catch (IOException ex) { }
return connection.getHeaderFieldDate(header, def);
}
public String getHeaderFieldKey(int index) {
try {
handshake();
} catch (IOException ex) { }
return connection.getHeaderFieldKey(index);
}
public String getHeaderField(int index) {
try {
handshake();
} catch (IOException ex) { }
return connection.getHeaderField(index);
}
public Object getContent() throws IOException {
try {
handshake();
} catch (IOException ex) { }
return connection.getContent();
}
public Object getContent(Class[] classes) throws IOException {
try {
handshake();
} catch (IOException ex) { }
return connection.getContent(classes);
}
public Permission getPermission() throws IOException {
return connection.getPermission();
}
public InputStream getInputStream() throws IOException {
try {
handshake();
} catch (IOException ex) { }
return connection.getInputStream();
}
public OutputStream getOutputStream() throws IOException {
try {
connect();
} catch (IOException ex) { }
OutputStream output = connection.getOutputStream();
cachedOutput = new ByteArrayOutputStream();
return new CacheStream(output, cachedOutput);
}
public String toString() {
return connection.toString();
}
public void setDoInput(boolean doInput) {
connection.setDoInput(doInput);
this.doInput = doInput;
}
public boolean getDoInput() {
return connection.getDoInput();
}
public void setDoOutput(boolean doOutput) {
connection.setDoOutput(doOutput);
this.doOutput = doOutput;
}
public boolean getDoOutput() {
return connection.getDoOutput();
}
public void setAllowUserInteraction(boolean allowUserInteraction) {
connection.setAllowUserInteraction(allowUserInteraction);
this.allowUserInteraction = allowUserInteraction;
}
public boolean getAllowUserInteraction() {
return connection.getAllowUserInteraction();
}
public void setUseCaches(boolean useCaches) {
connection.setUseCaches(useCaches);
this.useCaches = useCaches;
}
public boolean getUseCaches() {
return connection.getUseCaches();
}
public void setIfModifiedSince(long ifModifiedSince) {
connection.setIfModifiedSince(ifModifiedSince);
this.ifModifiedSince = ifModifiedSince;
}
public long getIfModifiedSince() {
return connection.getIfModifiedSince();
}
public boolean getDefaultUseCaches() {
return connection.getDefaultUseCaches();
}
public void setDefaultUseCaches(boolean defaultUseCaches) {
connection.setDefaultUseCaches(defaultUseCaches);
}
public void setRequestProperty(String key, String value) {
if (key == null) throw new NullPointerException();
List values = new ArrayList();
values.add(value);
boolean found = false;
Iterator entries = requestProperties.entrySet().iterator();
while (entries.hasNext()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -