Index: src/java/org/apache/commons/httpclient/ConnectMethod.java =================================================================== retrieving revision 1.22 diff -u -r1.22 ConnectMethod.java --- src/java/org/apache/commons/httpclient/ConnectMethod.java 10 Nov 2003 23:19:49 -0000 1.22 +++ src/java/org/apache/commons/httpclient/ConnectMethod.java 8 Dec 2003 03:27:34 -0000 @@ -65,13 +65,14 @@ import java.io.IOException; +import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Establishes a tunneled HTTP connection via the CONNECT method. * - * @author Ortwin Gl???ck + * @author Ortwin Gl???ck * @author dIon Gillard * @author Mike Bowler * @author Oleg Kalnichevski @@ -223,7 +224,7 @@ } buffer.append(" HTTP/1.1"); String line = buffer.toString(); - conn.printLine(line); + conn.printLine(line, getParams().getCredentialCharset()); if (Wire.enabled()) { Wire.output(line); } Index: src/java/org/apache/commons/httpclient/HttpConnection.java =================================================================== retrieving revision 1.81 diff -u -r1.81 HttpConnection.java --- src/java/org/apache/commons/httpclient/HttpConnection.java 11 Nov 2003 17:35:07 -0000 1.81 +++ src/java/org/apache/commons/httpclient/HttpConnection.java 8 Dec 2003 03:27:37 -0000 @@ -79,6 +79,7 @@ import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; +import org.apache.commons.httpclient.util.EncodingUtil; import org.apache.commons.httpclient.util.TimeoutController; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -983,6 +984,8 @@ } /** + * @deprecated Use {@link #print(String, String)} + * * Writes the specified String (as bytes) to the output stream. * * @param data the string to be written @@ -996,6 +999,22 @@ } /** + * Writes the specified String (as bytes) to the output stream. + * + * @param data the string to be written + * @params charset the charset to use for writing the data + * @throws IllegalStateException if the connection is not open + * @throws IOException if an I/O problem occurs + */ + public void print(String data, String charset) + throws IOException, IllegalStateException { + LOG.trace("enter HttpConnection.print(String)"); + write(EncodingUtil.getBytes(data, charset)); + } + + /** + * @deprecated Use {@link #println(String, String)} + * * Writes the specified String (as bytes), followed by * "\r\n".getBytes() to the output stream. * @@ -1009,6 +1028,21 @@ writeLine(HttpConstants.getBytes(data)); } + /** + * Writes the specified String (as bytes), followed by + * "\r\n".getBytes() to the output stream. + * + * @param data the data to be written + * @params charset the charset to use for writing the data + * @throws IllegalStateException if the connection is not open + * @throws IOException if an I/O problem occurs + */ + public void printLine(String data, String charset) + throws IOException, IllegalStateException { + LOG.trace("enter HttpConnection.printLine(String)"); + writeLine(EncodingUtil.getBytes(data, charset)); + } + /** * Writes "\r\n".getBytes() to the output stream. * Index: src/java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== retrieving revision 1.190 diff -u -r1.190 HttpMethodBase.java --- src/java/org/apache/commons/httpclient/HttpMethodBase.java 13 Nov 2003 22:24:46 -0000 1.190 +++ src/java/org/apache/commons/httpclient/HttpMethodBase.java 8 Dec 2003 03:27:42 -0000 @@ -2037,13 +2037,15 @@ + "HttpConnection)"); addRequestHeaders(state, conn); + String charset = getParams().getHttpElementCharset(); + Header[] headers = getRequestHeaders(); for (int i = 0; i < headers.length; i++) { String s = headers[i].toExternalForm(); if (Wire.enabled()) { Wire.output(s); } - conn.print(s); + conn.print(s, charset); } } @@ -2074,7 +2076,7 @@ if (Wire.enabled()) { Wire.output(requestLine); } - conn.print(requestLine); + conn.print(requestLine, getParams().getHttpElementCharset()); } /** Index: src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java =================================================================== retrieving revision 1.28 diff -u -r1.28 MultiThreadedHttpConnectionManager.java --- src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 19 Nov 2003 00:43:12 -0000 1.28 +++ src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 8 Dec 2003 03:27:46 -0000 @@ -1360,6 +1360,41 @@ } } + /* (non-Javadoc) + * @see org.apache.commons.httpclient.HttpConnection#print(java.lang.String, java.lang.String) + */ + public void print(String data, String charset) throws IOException, IllegalStateException { + if (hasConnection()) { + wrappedConnection.print(data, charset); + } else { + throw new IllegalStateException("Connection has been released"); + } + } + + /* (non-Javadoc) + * @see org.apache.commons.httpclient.HttpConnection#printLine(java.lang.String, java.lang.String) + */ + public void printLine(String data, String charset) + throws IOException, IllegalStateException { + if (hasConnection()) { + wrappedConnection.printLine(data, charset); + } else { + throw new IllegalStateException("Connection has been released"); + } + } + + /* (non-Javadoc) + * @see org.apache.commons.httpclient.HttpConnection#setSocketTimeout(int) + */ + public void setSocketTimeout(int timeout) throws SocketException, IllegalStateException { + // TODO Auto-generated method stub + if (hasConnection()) { + wrappedConnection.setSocketTimeout(timeout); + } else { + throw new IllegalStateException("Connection has been released"); + } + } + } } Index: src/java/org/apache/commons/httpclient/auth/AuthScheme.java =================================================================== retrieving revision 1.4 diff -u -r1.4 AuthScheme.java --- src/java/org/apache/commons/httpclient/auth/AuthScheme.java 22 Apr 2003 17:00:25 -0000 1.4 +++ src/java/org/apache/commons/httpclient/auth/AuthScheme.java 8 Dec 2003 03:27:46 -0000 @@ -64,6 +64,7 @@ package org.apache.commons.httpclient.auth; import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.params.HttpMethodParams; /** *

@@ -138,6 +139,8 @@ String getID(); /** + * @deprecated Use {@link #authenticate(Credentials, String, String, HttpMethodParams)} + * * Produces an authorization string for the given set of {@link Credentials}, * method name and URI using the given authentication scheme. * @@ -158,5 +161,32 @@ */ String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException; - + + /** + * Produces an authorization string for the given set of {@link Credentials}, + * method name and URI using the given authentication scheme. + * + * @param credentials The set of credentials to be used for athentication + * @param method The name of the method that requires authorization. + * This parameter may be ignored, if it is irrelevant + * or not applicable to the given authentication scheme + * @param uri The URI for which authorization is needed. + * This parameter may be ignored, if it is irrelevant or not + * applicable to the given authentication scheme + * @param params The parameters to use during authentication. + * @throws AuthenticationException if authorization string cannot + * be generated due to an authentication failure + * + * @return the authorization string + * + * @see org.apache.commons.httpclient.HttpMethod#getName() + * @see org.apache.commons.httpclient.HttpMethod#getPath() + */ + String authenticate( + Credentials credentials, + String method, + String uri, + HttpMethodParams params + ) throws AuthenticationException; + } Index: src/java/org/apache/commons/httpclient/auth/BasicScheme.java =================================================================== retrieving revision 1.7 diff -u -r1.7 BasicScheme.java --- src/java/org/apache/commons/httpclient/auth/BasicScheme.java 14 Nov 2003 02:28:49 -0000 1.7 +++ src/java/org/apache/commons/httpclient/auth/BasicScheme.java 8 Dec 2003 03:27:47 -0000 @@ -66,7 +66,9 @@ import org.apache.commons.httpclient.HttpConstants; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.util.Base64; +import org.apache.commons.httpclient.util.EncodingUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -143,7 +145,48 @@ } /** - * Return a basic Authorization header value for the given + * Produces basic authorization string for the given set of + * {@link Credentials}. + * + * @param credentials The set of credentials to be used for athentication + * @param method Method name is ignored by the basic authentication scheme + * @param uri URI is ignored by the basic authentication scheme + * @param params The parameters to use for determining what charset to use for encoding the + * credentials. + * @throws InvalidCredentialsException if authentication credentials + * are not valid or not applicable for this authentication scheme + * @throws AuthenticationException if authorization string cannot + * be generated due to an authentication failure + * + * @return a basic authorization string + */ + public String authenticate( + Credentials credentials, + String method, + String uri, + HttpMethodParams params + ) throws AuthenticationException { + + LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)"); + + UsernamePasswordCredentials usernamepassword = null; + try { + usernamepassword = (UsernamePasswordCredentials) credentials; + } catch (ClassCastException e) { + throw new InvalidCredentialsException( + "Credentials cannot be used for basic authentication: " + + credentials.getClass().getName()); + } + + return BasicScheme.authenticate( + usernamepassword, + params.getCredentialCharset()); + } + + /** + * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)} + * + * Returns a basic Authorization header value for the given * {@link UsernamePasswordCredentials}. * * @param credentials The credentials to encode. @@ -151,18 +194,35 @@ * @return a basic authorization string */ public static String authenticate(UsernamePasswordCredentials credentials) { + return authenticate(credentials, HttpConstants.DEFAULT_CONTENT_CHARSET); + } - LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials)"); + /** + * Returns a basic Authorization header value for the given + * {@link UsernamePasswordCredentials} and charset. + * + * @param credentials The credentials to encode. + * @param charset The charset to use for encoding the credentials + * + * @return a basic authorization string + */ + public static String authenticate(UsernamePasswordCredentials credentials, String charset) { + + LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)"); if (credentials == null) { throw new IllegalArgumentException("Credentials may not be null"); } + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } StringBuffer buffer = new StringBuffer(); buffer.append(credentials.getUserName()); buffer.append(":"); buffer.append(credentials.getPassword()); return "Basic " + HttpConstants.getAsciiString( - Base64.encode(HttpConstants.getContentBytes(buffer.toString()))); + Base64.encode(EncodingUtil.getBytes(buffer.toString(), charset))); } + } Index: src/java/org/apache/commons/httpclient/auth/DigestScheme.java =================================================================== retrieving revision 1.11 diff -u -r1.11 DigestScheme.java --- src/java/org/apache/commons/httpclient/auth/DigestScheme.java 3 Oct 2003 20:57:36 -0000 1.11 +++ src/java/org/apache/commons/httpclient/auth/DigestScheme.java 8 Dec 2003 03:27:48 -0000 @@ -71,6 +71,8 @@ import org.apache.commons.httpclient.HttpConstants; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.httpclient.util.EncodingUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -82,12 +84,20 @@ * is unsupported. If auth and auth-int are provided, auth is * used. *

+ *

+ * Credential charset is configured via the + * {@link org.apache.commons.httpclient.params.HttpMethodParams#CREDENTIAL_CHARSET credential + * charset} parameter. Since the digest username is included as clear text in the generated + * Authentication header, the charset of the username must be compatible with the + * {@link org.apache.commons.httpclient.params.HttpMethodParams#HTTP_ELEMENT_CHARSET http element + * charset}. + *

* TODO: make class more stateful regarding repeated authentication requests * * @author Remy Maucherat * @author Rodney Waldhoff * @author Jeff Dever - * @author Ortwin Gl?ck + * @author Ortwin Gl?ck * @author Sean C. Sullivan * @author Adrian Sutton * @author Mike Bowler @@ -220,28 +230,70 @@ } this.getParameters().put("methodname", method); this.getParameters().put("uri", uri); - String digest = createDigest(usernamepassword.getUserName(), - usernamepassword.getPassword()); + String digest = createDigest( + usernamepassword.getUserName(), + usernamepassword.getPassword(), + HttpConstants.DEFAULT_CONTENT_CHARSET); - return "Digest " + createDigestHeader(usernamepassword.getUserName(), - digest); + return "Digest " + createDigestHeader(usernamepassword.getUserName(), digest); } /** + * Produces a digest authorization string for the given set of + * {@link Credentials}, method name and URI. + * + * @param credentials A set of credentials to be used for athentication + * @param method the name of the method that requires authorization. + * @param uri The URI for which authorization is needed. + * @param params The parameters to use for determining what charset to use for encoding the + * credentials. + * + * @throws InvalidCredentialsException if authentication credentials + * are not valid or not applicable for this authentication scheme + * @throws AuthenticationException if authorization string cannot + * be generated due to an authentication failure + * + * @return a digest authorization string + * + * @see org.apache.commons.httpclient.HttpMethod#getName() + * @see org.apache.commons.httpclient.HttpMethod#getPath() + */ + public String authenticate(Credentials credentials, String method, String uri, HttpMethodParams params) + throws AuthenticationException { + + LOG.trace("enter DigestScheme.authenticate(Credentials, String, String)"); + + UsernamePasswordCredentials usernamepassword = null; + try { + usernamepassword = (UsernamePasswordCredentials) credentials; + } catch (ClassCastException e) { + throw new InvalidCredentialsException( + "Credentials cannot be used for digest authentication: " + + credentials.getClass().getName()); + } + this.getParameters().put("methodname", method); + this.getParameters().put("uri", uri); + String digest = createDigest( + usernamepassword.getUserName(), + usernamepassword.getPassword(), + params.getCredentialCharset()); + + return "Digest " + createDigestHeader(usernamepassword.getUserName(), + digest); + } + + /** * Creates an MD5 response digest. * * @param uname Username * @param pwd Password - * @param params The parameters necessary to construct the digest. - * The following parameters are expected: uri, - * realm, nonce, nc, - * cnonce, qop, methodname. + * @param charset The credential charset * * @return The created digest as string. This will be the response tag's * value in the Authentication HTTP header. * @throws AuthenticationException when MD5 is an unsupported algorithm */ - private String createDigest(String uname, String pwd) throws AuthenticationException { + private String createDigest(String uname, String pwd, String charset) throws AuthenticationException { LOG.trace("enter DigestScheme.createDigest(String, String, Map)"); @@ -260,7 +312,6 @@ algorithm="MD5"; } - if (qopVariant == QOP_AUTH_INT) { LOG.warn("qop=auth-int is not supported"); throw new AuthenticationException( @@ -292,8 +343,7 @@ // ":" unq(nonce-value) // ":" unq(cnonce-value) - String tmp2=encode(md5Helper.digest(HttpConstants.getBytes( - a1))); + String tmp2=encode(md5Helper.digest(EncodingUtil.getBytes(a1, charset))); StringBuffer tmp3 = new StringBuffer(tmp2.length() + nonce.length() + cnonce.length() + 2); tmp3.append(tmp2); tmp3.append(':'); @@ -304,7 +354,7 @@ } else if(!algorithm.equals("MD5")) { LOG.warn("Unhandled algorithm " + algorithm + " requested"); } - String md5a1 = encode(md5Helper.digest(HttpConstants.getBytes(a1))); + String md5a1 = encode(md5Helper.digest(EncodingUtil.getBytes(a1, charset))); String a2 = null; if (qopVariant == QOP_AUTH_INT) { Index: src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java =================================================================== retrieving revision 1.13 diff -u -r1.13 HttpAuthenticator.java --- src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java 2 Nov 2003 12:10:28 -0000 1.13 +++ src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java 8 Dec 2003 03:27:49 -0000 @@ -63,12 +63,13 @@ package org.apache.commons.httpclient.auth; -import java.util.Map; import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.logging.Log; @@ -178,7 +179,6 @@ "Authentication scheme(s) not supported: " + challengemap.toString()); } - private static boolean doAuthenticateDefault( HttpMethod method, HttpConnection conn, @@ -205,7 +205,9 @@ "Credentials cannot be used for basic authentication: " + credentials.toString()); } - String auth = BasicScheme.authenticate((UsernamePasswordCredentials) credentials); + String auth = BasicScheme.authenticate( + (UsernamePasswordCredentials) credentials, + method.getParams().getCredentialCharset()); if (auth != null) { String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP; Header header = new Header(s, auth, true); @@ -328,7 +330,11 @@ "No credentials available for the '" + realm + "' authentication realm at " + host); } - String auth = authscheme.authenticate(credentials, method.getName(), method.getPath()); + String auth = authscheme.authenticate( + credentials, + method.getName(), + method.getPath(), + method.getParams()); if (auth != null) { String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP; Header header = new Header(s, auth, true); Index: src/java/org/apache/commons/httpclient/auth/NTLM.java =================================================================== retrieving revision 1.4 diff -u -r1.4 NTLM.java --- src/java/org/apache/commons/httpclient/auth/NTLM.java 16 Aug 2003 00:41:24 -0000 1.4 +++ src/java/org/apache/commons/httpclient/auth/NTLM.java 8 Dec 2003 03:27:50 -0000 @@ -63,7 +63,6 @@ package org.apache.commons.httpclient.auth; -import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; @@ -75,8 +74,7 @@ import org.apache.commons.httpclient.HttpConstants; import org.apache.commons.httpclient.util.Base64; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.httpclient.util.EncodingUtil; /** * Provides an implementation of the NTLM authentication protocol. @@ -102,18 +100,18 @@ */ final class NTLM { + /** Character encoding */ + public static final String DEFAULT_CHARSET = "ASCII"; + /** The current response */ private byte[] currentResponse; /** The current position */ private int currentPosition = 0; - /** Log object for this class. */ - private static final Log LOG = LogFactory.getLog(NTLM.class); - - /** Character encoding */ - public static final String DEFAULT_CHARSET = "ASCII"; - + /** The character set to use for encoding the credentials */ + private String credentialCharset = DEFAULT_CHARSET; + /** * Returns the response for the given message. * @@ -268,14 +266,14 @@ private String getType1Message(String host, String domain) { host = host.toUpperCase(); domain = domain.toUpperCase(); - byte[] hostBytes = getBytes(host); - byte[] domainBytes = getBytes(domain); + byte[] hostBytes = EncodingUtil.getBytes(host, DEFAULT_CHARSET); + byte[] domainBytes = EncodingUtil.getBytes(domain, DEFAULT_CHARSET); int finalLength = 32 + hostBytes.length + domainBytes.length; prepareResponse(finalLength); // The initial id string. - byte[] protocol = getBytes("NTLMSSP"); + byte[] protocol = EncodingUtil.getBytes("NTLMSSP", DEFAULT_CHARSET); addBytes(protocol); addByte((byte) 0); @@ -342,7 +340,7 @@ */ private byte[] parseType2Message(String message) { // Decode the message first. - byte[] msg = Base64.decode(getBytes(message)); + byte[] msg = Base64.decode(EncodingUtil.getBytes(message, DEFAULT_CHARSET)); byte[] nonce = new byte[8]; // The nonce is the 8 bytes starting from the byte in position 24. for (int i = 0; i < 8; i++) { @@ -373,16 +371,16 @@ domain = domain.toUpperCase(); host = host.toUpperCase(); user = user.toUpperCase(); - byte[] domainBytes = getBytes(domain); - byte[] hostBytes = getBytes(host); - byte[] userBytes = getBytes(user); + byte[] domainBytes = EncodingUtil.getBytes(domain, DEFAULT_CHARSET); + byte[] hostBytes = EncodingUtil.getBytes(host, DEFAULT_CHARSET); + byte[] userBytes = EncodingUtil.getBytes(user, credentialCharset); int domainLen = domainBytes.length; int hostLen = hostBytes.length; int userLen = userBytes.length; int finalLength = 64 + ntRespLen + lmRespLen + domainLen + userLen + hostLen; prepareResponse(finalLength); - byte[] ntlmssp = getBytes("NTLMSSP"); + byte[] ntlmssp = EncodingUtil.getBytes("NTLMSSP", DEFAULT_CHARSET); addBytes(ntlmssp); addByte((byte) 0); addByte((byte) 3); @@ -465,7 +463,7 @@ */ private byte[] hashPassword(String password, byte[] nonce) throws AuthenticationException { - byte[] passw = getBytes(password.toUpperCase()); + byte[] passw = EncodingUtil.getBytes(password.toUpperCase(), credentialCharset); byte[] lmPw1 = new byte[7]; byte[] lmPw2 = new byte[7]; @@ -585,18 +583,17 @@ } /** - * Convert a string to a byte array. - * @param s The string - * @return byte[] The resulting byte array. - */ - private static byte[] getBytes(final String s) { - if (s == null) { - throw new IllegalArgumentException("Parameter may not be null"); - } - try { - return s.getBytes(DEFAULT_CHARSET); - } catch (UnsupportedEncodingException unexpectedEncodingException) { - throw new RuntimeException("NTLM requires ASCII support"); - } + * @return Returns the credentialCharset. + */ + public String getCredentialCharset() { + return credentialCharset; + } + + /** + * @param credentialCharset The credentialCharset to set. + */ + public void setCredentialCharset(String credentialCharset) { + this.credentialCharset = credentialCharset; } + } Index: src/java/org/apache/commons/httpclient/auth/NTLMScheme.java =================================================================== retrieving revision 1.11 diff -u -r1.11 NTLMScheme.java --- src/java/org/apache/commons/httpclient/auth/NTLMScheme.java 16 Aug 2003 00:41:24 -0000 1.11 +++ src/java/org/apache/commons/httpclient/auth/NTLMScheme.java 8 Dec 2003 03:27:51 -0000 @@ -65,6 +65,7 @@ import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.NTCredentials; +import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -195,6 +196,40 @@ credentials.getHost(), credentials.getDomain()); return "NTLM " + s; } + + /** + * Create a NTLM authorization string for the given + * challenge and NT credentials. + * + * @param challenge The challenge. + * @param credentials {@link NTCredentials} + * @param charset The charset to use for encoding the credentials + * + * @return a ntlm authorization string + * @throws AuthenticationException is thrown if authentication fails + */ + public static String authenticate( + final NTCredentials credentials, + final String challenge, + String charset + ) throws AuthenticationException { + + LOG.trace("enter NTLMScheme.authenticate(NTCredentials, String)"); + + if (credentials == null) { + throw new IllegalArgumentException("Credentials may not be null"); + } + + NTLM ntlm = new NTLM(); + ntlm.setCredentialCharset(charset); + String s = ntlm.getResponseFor( + challenge, + credentials.getUserName(), + credentials.getPassword(), + credentials.getHost(), + credentials.getDomain()); + return "NTLM " + s; + } /** * Produces NTLM authorization string for the given set of @@ -223,5 +258,44 @@ + credentials.getClass().getName()); } return NTLMScheme.authenticate(ntcredentials, this.ntlmchallenge); + } + + /** + * Produces NTLM authorization string for the given set of + * {@link Credentials}. + * + * @param credentials The set of credentials to be used for athentication + * @param method Method name is ignored by the NTLM authentication scheme + * @param uri URI is ignored by the NTLM authentication scheme + * @param params The parameters to use for determining what charset to use for encoding the + * credentials. + * + * @throws InvalidCredentialsException if authentication credentials + * are not valid or not applicable for this authentication scheme + * @throws AuthenticationException if authorization string cannot + * be generated due to an authentication failure + * + * @return an NTLM authorization string + */ + public String authenticate( + Credentials credentials, + String method, + String uri, + HttpMethodParams params + ) throws AuthenticationException { + LOG.trace("enter NTLMScheme.authenticate(Credentials, String, String)"); + + NTCredentials ntcredentials = null; + try { + ntcredentials = (NTCredentials) credentials; + } catch (ClassCastException e) { + throw new InvalidCredentialsException( + "Credentials cannot be used for NTLM authentication: " + + credentials.getClass().getName()); + } + return NTLMScheme.authenticate( + ntcredentials, + this.ntlmchallenge, + params.getCredentialCharset()); } } Index: src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java =================================================================== retrieving revision 1.4 diff -u -r1.4 DefaultHttpParamsFactory.java --- src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java 20 Oct 2003 22:17:12 -0000 1.4 +++ src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java 8 Dec 2003 03:27:51 -0000 @@ -102,6 +102,7 @@ params.setVersion(HttpVersion.HTTP_1_1); params.setConnectionManagerClass(SimpleHttpConnectionManager.class); params.setCookiePolicy(CookiePolicy.RFC_2109); + params.setHttpElementCharset("US-ASCII"); ArrayList datePatterns = new ArrayList(); datePatterns.addAll( Index: src/java/org/apache/commons/httpclient/params/HttpMethodParams.java =================================================================== retrieving revision 1.5 diff -u -r1.5 HttpMethodParams.java --- src/java/org/apache/commons/httpclient/params/HttpMethodParams.java 22 Oct 2003 19:31:00 -0000 1.5 +++ src/java/org/apache/commons/httpclient/params/HttpMethodParams.java 8 Dec 2003 03:27:52 -0000 @@ -179,6 +179,24 @@ public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue"; /** + * Defines the charset to be used when encoding + * {@link org.apache.commons.httpclient.Credentials}. If not defined then the + * {@link #HTTP_ELEMENT_CHARSET} should be used. + *

+ * This parameter expects a value of type {@link String}. + *

+ */ + public static final String CREDENTIAL_CHARSET = "http.protocol.credential-charset"; + + /** + * Defines the charset to be used for encoding HTTP protocol elements. + *

+ * This parameter expects a value of type {@link String}. + *

+ */ + public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset"; + + /** * Defines {@link CookiePolicy cookie policy} to be used for cookie management. *

* This parameter expects a value of type {@link String}. @@ -195,7 +213,7 @@ * @see #getDefaultParams() */ public HttpMethodParams() { - super(null); + super(getDefaultParams()); } /** @@ -213,6 +231,49 @@ super(defaults); } + /** + * Returns the charset to be used for writing HTTP headers. + * @return The charset + */ + public String getHttpElementCharset() { + String charset = (String) getParameter(HTTP_ELEMENT_CHARSET); + if (charset == null) { + LOG.warn("HTTP element charset not configured, using US-ASCII"); + charset = "US-ASCII"; + } + return charset; + } + + /** + * Sets the charset to be used for writing HTTP headers. + * @param charset The charset + */ + public void setHttpElementCharset(String charset) { + setParameter(HTTP_ELEMENT_CHARSET, charset); + } + + /** + * Returns the charset to be used for {@link org.apache.commons.httpclient.Credentials}. If + * not configured the {@link #HTTP_ELEMENT_CHARSET HTTP element charset} is used. + * @return The charset + */ + public String getCredentialCharset() { + String charset = (String) getParameter(CREDENTIAL_CHARSET); + if (charset == null) { + LOG.debug("Credential charset not configured, using HTTP element charset"); + charset = getHttpElementCharset(); + } + return charset; + } + + /** + * Sets the charset to be used for writing HTTP headers. + * @param charset The charset + */ + public void setCredentialCharset(String charset) { + setParameter(CREDENTIAL_CHARSET, charset); + } + /** * Returns {@link HttpVersion HTTP protocol version} to be used by the * {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that Index: src/java/org/apache/commons/httpclient/util/EncodingUtil.java =================================================================== retrieving revision 1.1 diff -u -r1.1 EncodingUtil.java --- src/java/org/apache/commons/httpclient/util/EncodingUtil.java 15 Jul 2003 12:46:33 -0000 1.1 +++ src/java/org/apache/commons/httpclient/util/EncodingUtil.java 8 Dec 2003 03:27:53 -0000 @@ -62,6 +62,7 @@ */ package org.apache.commons.httpclient.util; +import java.io.UnsupportedEncodingException; import java.util.BitSet; import org.apache.commons.httpclient.NameValuePair; @@ -159,6 +160,36 @@ } return buf.toString(); } + + /** + * Converts the specified string to a byte array. If the charset is not supported the + * default system charset is used. + * + * @param data the string to be encoded + * @param charset the desired character encoding + * @return The resulting byte array. + */ + public static byte[] getBytes(final String data, String charset) { + + if (data == null) { + throw new IllegalArgumentException("data may not be null"); + } + + if (charset == null || charset.length() == 0) { + throw new IllegalArgumentException("charset may not be null or empty"); + } + + try { + return data.getBytes(charset); + } catch (UnsupportedEncodingException e) { + + if (LOG.isWarnEnabled()) { + LOG.warn("Unsupported encoding: " + charset + ". System encoding used."); + } + + return data.getBytes(); + } + } /** * This class should not be instantiated.