001: /*
002: * Name: HttpClientCredentialsProvider.java
003: * Version: $Id: HttpClientCredentialsProvider.java 8974 2007-09-07 01:30:35Z mpreston $
004: * Copyright: Copyright (c) 2006, Bostech Corporation
005: * Author: mpreston
006: * Created on: May 1, 2006
007: * Description: Class to provide the appropriate type of credentials depending on the authorization
008: * scheme requested by the server.
009: */
010: package com.bostechcorp.cbesb.runtime.component.http.client;
011:
012: import java.io.IOException;
013:
014: import org.apache.commons.httpclient.Credentials;
015: import org.apache.commons.httpclient.auth.AuthScheme;
016: import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
017: import org.apache.commons.httpclient.auth.CredentialsProvider;
018: import org.apache.commons.httpclient.auth.NTLMScheme;
019: import org.apache.commons.httpclient.auth.RFC2617Scheme;
020: import org.apache.commons.httpclient.NTCredentials;
021: import org.apache.commons.httpclient.UsernamePasswordCredentials;
022:
023: public class HttpClientCredentialsProvider implements
024: CredentialsProvider {
025:
026: /**
027: * Constructor
028: */
029: public HttpClientCredentialsProvider() {
030: }
031:
032: /**
033: * Sets the user and password to use for authentication.
034: * @param username - the username to log in to the server. If domain name should be specified,
035: * then pass the username as <code><i>domain\user</i></code>.
036: * @param password - the password.
037: */
038: public void setUsernamePassword(String username, String password) {
039: _username = username;
040: _password = password;
041: }
042:
043: /**
044: * Sets the user and password to use for authentication on a proxy server.
045: * @param username - the username to log in to the server. If domain name should be specified,
046: * then pass the username as <code><i>domain\user</i></code>.
047: * @param password - the password.
048: */
049: public void setProxyUsernamePassword(String username,
050: String password) {
051: _proxyUsername = username;
052: _proxyPassword = password;
053: }
054:
055: /**
056: * Called when a server requires authentication. If the authentication scheme is NTLM, then NTCredentials
057: * are supplied, otherwise UsernamePasswordCredentials are supplied.
058: * @param authscheme - The authorization scheme requested by the server.
059: * @param host - The server hostname
060: * @param port - The server port
061: * @param proxy - True if logging into a proxy server. False otherwise.
062: * @throws CredentialsNotAvailableException
063: */
064: public Credentials getCredentials(AuthScheme authscheme,
065: String host, int port, boolean proxy)
066: throws CredentialsNotAvailableException {
067: String tmpuser;
068: String tmppwd;
069: if (proxy) {
070: tmpuser = _proxyUsername;
071: tmppwd = _proxyPassword;
072: } else {
073: tmpuser = _username;
074: tmppwd = _password;
075: }
076:
077: if (authscheme == null) {
078: return null;
079: }
080: try {
081: if (authscheme instanceof NTLMScheme) {
082: String user = "";
083: String domain = "";
084: if (tmpuser.indexOf("\\") > -1) {
085: int index = tmpuser.indexOf("\\");
086: user = tmpuser.substring(index + 1);
087: domain = tmpuser.substring(0, index);
088: } else {
089: throw new CredentialsNotAvailableException(
090: "Could not determine domain from supplied username '"
091: + tmpuser
092: + "'. Please specify in 'domain\\username' format.");
093: }
094: return new NTCredentials(user, tmppwd, host, domain);
095:
096: } else if (authscheme instanceof RFC2617Scheme) {
097: return new UsernamePasswordCredentials(tmpuser, tmppwd);
098: } else {
099: throw new CredentialsNotAvailableException(
100: "Unsupported authentication scheme: "
101: + authscheme.getSchemeName());
102: }
103: } catch (IOException e) {
104: throw new CredentialsNotAvailableException(e.getMessage(),
105: e);
106: }
107: }
108:
109: private String _username = "";
110: private String _password = "";
111: private String _proxyUsername = "";
112: private String _proxyPassword = "";
113:
114: }
|