forked from community/device-mgt-core
parent
8585abff8d
commit
36462e2e4e
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.webapp.authenticator.framework.Utils;
|
||||||
|
|
||||||
|
import org.apache.axis2.AxisFault;
|
||||||
|
import org.apache.axis2.client.Options;
|
||||||
|
import org.apache.axis2.client.ServiceClient;
|
||||||
|
import org.apache.axis2.transport.http.HTTPConstants;
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.apache.commons.httpclient.Header;
|
||||||
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.commons.pool.PoolableObjectFactory;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
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.OAuthTokenValidationException;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class OAuthTokenValidationStubFactory implements PoolableObjectFactory {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String basicAuthHeader;
|
||||||
|
private static final Log log = LogFactory.getLog(OAuthTokenValidationStubFactory.class);
|
||||||
|
|
||||||
|
private HttpClient httpClient;
|
||||||
|
|
||||||
|
public OAuthTokenValidationStubFactory(String url, String adminUsername, String adminPassword,
|
||||||
|
Properties properties) {
|
||||||
|
this.url = url;
|
||||||
|
this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes()));
|
||||||
|
|
||||||
|
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
||||||
|
connectionManager.getParams().setDefaultMaxConnectionsPerHost(
|
||||||
|
Integer.parseInt(properties.getProperty("MaxConnectionsPerHost")));
|
||||||
|
connectionManager.getParams().setMaxTotalConnections(
|
||||||
|
Integer.parseInt(properties.getProperty("MaxTotalConnections")));
|
||||||
|
this.httpClient = new DefaultHttpClient((ClientConnectionManager) connectionManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object makeObject() throws Exception {
|
||||||
|
return this.createStub();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroyObject(Object o) throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validateObject(Object o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void activateObject(Object o) throws Exception {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("OAuth token validate stub instance is activated");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void passivateObject(Object o) throws Exception {
|
||||||
|
if (o instanceof OAuth2TokenValidationServiceStub) {
|
||||||
|
OAuth2TokenValidationServiceStub stub = (OAuth2TokenValidationServiceStub) o;
|
||||||
|
stub._getServiceClient().cleanupTransport();
|
||||||
|
stub._getServiceClient().setOptions(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private OAuth2TokenValidationServiceStub createStub() throws OAuthTokenValidationException {
|
||||||
|
OAuth2TokenValidationServiceStub stub;
|
||||||
|
try {
|
||||||
|
stub = new OAuth2TokenValidationServiceStub(url);
|
||||||
|
ServiceClient client = stub._getServiceClient();
|
||||||
|
client.getServiceContext().getConfigurationContext().setProperty(
|
||||||
|
HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
|
||||||
|
|
||||||
|
List<Header> headerList = new ArrayList<>();
|
||||||
|
Header header = new Header();
|
||||||
|
header.setName(HTTPConstants.HEADER_AUTHORIZATION);
|
||||||
|
header.setValue(OAuthConstants.AUTHORIZATION_HEADER_PREFIX_BASIC + " " + basicAuthHeader);
|
||||||
|
headerList.add(header);
|
||||||
|
|
||||||
|
Options options = client.getOptions();
|
||||||
|
options.setProperty(HTTPConstants.HTTP_HEADERS, headerList);
|
||||||
|
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
|
||||||
|
client.setOptions(options);
|
||||||
|
} catch (AxisFault axisFault) {
|
||||||
|
throw new OAuthTokenValidationException("Exception occurred while creating the " +
|
||||||
|
"OAuth2TokenValidationServiceStub.", axisFault);
|
||||||
|
}
|
||||||
|
return stub;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue