|
|
@ -24,15 +24,14 @@ import org.apache.axis2.client.ServiceClient;
|
|
|
|
import org.apache.axis2.transport.http.HTTPConstants;
|
|
|
|
import org.apache.axis2.transport.http.HTTPConstants;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.httpclient.Header;
|
|
|
|
import org.apache.commons.httpclient.Header;
|
|
|
|
|
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
|
|
|
|
|
import org.apache.commons.httpclient.HttpConnectionManager;
|
|
|
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
|
|
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
|
|
|
|
|
|
|
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.pool.PoolableObjectFactory;
|
|
|
|
import org.apache.commons.pool.PoolableObjectFactory;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
import org.apache.http.conn.HttpClientConnectionManager;
|
|
|
|
import org.apache.http.conn.ClientConnectionManager;
|
|
|
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
|
|
|
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
|
|
|
|
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub;
|
|
|
|
import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub;
|
|
|
|
import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthConstants;
|
|
|
|
import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthConstants;
|
|
|
@ -46,19 +45,94 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory {
|
|
|
|
|
|
|
|
|
|
|
|
private String url;
|
|
|
|
private String url;
|
|
|
|
private String basicAuthHeader;
|
|
|
|
private String basicAuthHeader;
|
|
|
|
private static final Log log = LogFactory.getLog(OAuthTokenValidationStubFactory.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpClient httpClient;
|
|
|
|
private HttpClient httpClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(OAuthTokenValidationStubFactory.class);
|
|
|
|
|
|
|
|
|
|
|
|
public OAuthTokenValidationStubFactory(String url, String adminUsername, String adminPassword,
|
|
|
|
public OAuthTokenValidationStubFactory(String url, String adminUsername, String adminPassword,
|
|
|
|
Properties properties) {
|
|
|
|
Properties properties) {
|
|
|
|
|
|
|
|
this.validateUrl(url);
|
|
|
|
this.url = url;
|
|
|
|
this.url = url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.validateCredentials(adminUsername, adminPassword);
|
|
|
|
this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes()));
|
|
|
|
this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpConnectionManager connectionManager = this.createConnectionManager(properties);
|
|
|
|
|
|
|
|
this.httpClient = new HttpClient(connectionManager);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Creates an instance of MultiThreadedHttpConnectionManager using HttpClient 3.x APIs
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param properties Properties to configure MultiThreadedHttpConnectionManager
|
|
|
|
|
|
|
|
* @return An instance of properly configured MultiThreadedHttpConnectionManager
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private HttpConnectionManager createConnectionManager(Properties properties) {
|
|
|
|
|
|
|
|
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
|
|
|
|
|
|
|
|
if (properties == null || properties.isEmpty()) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Parameters required to initialize HttpClient instances " +
|
|
|
|
|
|
|
|
"associated with OAuth token validation service stub are not provided");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost");
|
|
|
|
|
|
|
|
if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " +
|
|
|
|
|
|
|
|
"which is 2, will be used");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
params.setDefaultMaxConnectionsPerHost(Integer.parseInt(maxConnectionsPerHostParam));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections");
|
|
|
|
|
|
|
|
if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " +
|
|
|
|
|
|
|
|
"which is 10, will be used");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
params.setMaxTotalConnections(Integer.parseInt(maxTotalConnectionsParam));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
|
|
|
|
|
|
|
connectionManager.setParams(params);
|
|
|
|
|
|
|
|
return connectionManager;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Creates an instance of PoolingHttpClientConnectionManager using HttpClient 4.x APIs
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param properties Properties to configure PoolingHttpClientConnectionManager
|
|
|
|
|
|
|
|
* @return An instance of properly configured PoolingHttpClientConnectionManager
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private HttpClientConnectionManager createClientConnectionManager(Properties properties) {
|
|
|
|
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
|
|
|
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
|
|
|
connectionManager.setDefaultMaxPerRoute(Integer.parseInt(properties.getProperty("MaxConnectionsPerHost")));
|
|
|
|
if (properties != null) {
|
|
|
|
connectionManager.setMaxTotal(Integer.parseInt(properties.getProperty("MaxTotalConnections")));
|
|
|
|
String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost");
|
|
|
|
this.httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
|
|
|
|
if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " +
|
|
|
|
|
|
|
|
"which is 2, will be used");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
connectionManager.setDefaultMaxPerRoute(Integer.parseInt(maxConnectionsPerHostParam));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections");
|
|
|
|
|
|
|
|
if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " +
|
|
|
|
|
|
|
|
"which is 10, will be used");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
connectionManager.setMaxTotal(Integer.parseInt(maxTotalConnectionsParam));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Properties, i.e. MaxTotalConnections/MaxConnectionsPerHost, required to tune the " +
|
|
|
|
|
|
|
|
"HttpClient used in OAuth token validation service stub instances are not provided. " +
|
|
|
|
|
|
|
|
"Therefore, the defaults, 2/10 respectively, will be used");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return connectionManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -88,7 +162,6 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory {
|
|
|
|
if (o instanceof OAuth2TokenValidationServiceStub) {
|
|
|
|
if (o instanceof OAuth2TokenValidationServiceStub) {
|
|
|
|
OAuth2TokenValidationServiceStub stub = (OAuth2TokenValidationServiceStub) o;
|
|
|
|
OAuth2TokenValidationServiceStub stub = (OAuth2TokenValidationServiceStub) o;
|
|
|
|
stub._getServiceClient().cleanupTransport();
|
|
|
|
stub._getServiceClient().cleanupTransport();
|
|
|
|
stub._getServiceClient().setOptions(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -111,10 +184,28 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory {
|
|
|
|
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
|
|
|
|
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
|
|
|
|
client.setOptions(options);
|
|
|
|
client.setOptions(options);
|
|
|
|
} catch (AxisFault axisFault) {
|
|
|
|
} catch (AxisFault axisFault) {
|
|
|
|
throw new OAuthTokenValidationException("Exception occurred while creating the " +
|
|
|
|
throw new OAuthTokenValidationException("Error occurred while creating the " +
|
|
|
|
"OAuth2TokenValidationServiceStub.", axisFault);
|
|
|
|
"OAuth2TokenValidationServiceStub.", axisFault);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stub;
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void validateUrl(String url) {
|
|
|
|
|
|
|
|
if (url == null || url.isEmpty()) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Url provided as the endpoint of the OAuth token validation service " +
|
|
|
|
|
|
|
|
"is null");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void validateCredentials(String adminUsername, String adminPassword) {
|
|
|
|
|
|
|
|
if (adminUsername == null || adminUsername.isEmpty()) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("An appropriate username required to initialize OAuth token " +
|
|
|
|
|
|
|
|
"validation service stub factory hasn't been provided");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adminPassword == null || adminPassword.isEmpty()) {
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("An appropriate password required to initialize OAuth token " +
|
|
|
|
|
|
|
|
"validation service stub factory hasn't been provided");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|