Repository: incubator-usergrid Updated Branches: refs/heads/master 5f4a66b26 -> 49ae4ac5b
Adds connection pool for HTTP client connections from SSO client system to SSO central. Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/a649022f Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/a649022f Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/a649022f Branch: refs/heads/master Commit: a649022f2509fb2952006ac99d23ba05145dca0c Parents: 5f4a66b Author: Dave Johnson <[email protected]> Authored: Mon May 11 15:33:21 2015 -0700 Committer: Dave Johnson <[email protected]> Committed: Mon May 11 15:33:21 2015 -0700 ---------------------------------------------------------------------- .../main/resources/usergrid-default.properties | 5 ++ stack/rest/pom.xml | 6 ++ .../rest/management/ManagementResource.java | 71 +++++++++++++++++--- 3 files changed, 73 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a649022f/stack/config/src/main/resources/usergrid-default.properties ---------------------------------------------------------------------- diff --git a/stack/config/src/main/resources/usergrid-default.properties b/stack/config/src/main/resources/usergrid-default.properties index b895310..b9cc05d 100644 --- a/stack/config/src/main/resources/usergrid-default.properties +++ b/stack/config/src/main/resources/usergrid-default.properties @@ -121,6 +121,11 @@ usergrid.sysadmin.approve.organizations=false # server. See also: https://issues.apache.org/jira/browse/USERGRID-567 usergrid.central.url= +# HTTP Client connection pool for connections from SSO client system `to SSO central +usergrid.central.connection.pool.size=40; +usergrid.central.connection.timeout=10000; +usergrid.central.read.timeout=10000 + # Where to store temporary files usergrid.temp.files=/tmp/usergrid http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a649022f/stack/rest/pom.xml ---------------------------------------------------------------------- diff --git a/stack/rest/pom.xml b/stack/rest/pom.xml index 943e868..6a2c87f 100644 --- a/stack/rest/pom.xml +++ b/stack/rest/pom.xml @@ -589,6 +589,12 @@ <artifactId>jbcrypt</artifactId> </dependency> + <dependency> + <groupId>com.sun.jersey.contribs</groupId> + <artifactId>jersey-apache-client</artifactId> + <version>${jersey-version}</version> + </dependency> + </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a649022f/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java index 6a0f4f2..ad604d5 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java @@ -24,12 +24,21 @@ import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.json.JSONConfiguration; import com.sun.jersey.api.view.Viewable; +import com.sun.jersey.client.apache.ApacheHttpClient; +import com.sun.jersey.client.apache.ApacheHttpClientHandler; import org.apache.amber.oauth2.common.error.OAuthError; import org.apache.amber.oauth2.common.exception.OAuthProblemException; import org.apache.amber.oauth2.common.message.OAuthResponse; import org.apache.amber.oauth2.common.message.types.GrantType; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpConnectionManager; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.params.HttpClientParams; +import org.apache.commons.httpclient.params.HttpConnectionManagerParams; +import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.shiro.codec.Base64; import org.apache.usergrid.exception.NotImplementedException; import org.apache.usergrid.management.ApplicationCreator; @@ -104,6 +113,9 @@ public class ManagementResource extends AbstractContextResource { @Autowired MetricsFactory metricsFactory; + private static Client jerseyClient = null; + + // names for metrics to be collected private static final String SSO_TOKENS_REJECTED = "sso_tokens_rejected"; private static final String SSO_TOKENS_VALIDATED = "sso_tokens_validated"; @@ -111,10 +123,11 @@ public class ManagementResource extends AbstractContextResource { private static final String SSO_PROCESSING_TIME = "sso_processing_time"; // usergrid configuration property names needed - public static final String USERGRID_CENTRAL_URL = "usergrid.central.url"; public static final String USERGRID_SYSADMIN_LOGIN_NAME = "usergrid.sysadmin.login.name"; - public static final String USERGRID_SYSADMIN_LOGIN_ALLOWED = "usergrid.sysadmin.login.allowed"; - + public static final String USERGRID_CENTRAL_URL = "usergrid.central.url"; + public static final String CENTRAL_CONNECTION_POOL_SIZE = "usergrid.central.connection.pool.size"; + public static final String CENTRAL_CONNECTION_TIMEOUT = "usergrid.central.connection.timeout"; + public static final String CENTRAL_READ_TIMEOUT = "usergrid.central.read.timeout"; public ManagementResource() { logger.info( "ManagementResource initialized" ); @@ -188,7 +201,7 @@ public class ManagementResource extends AbstractContextResource { } - private Response getAccessTokenInternal(UriInfo ui, String authorization, String grant_type, String username, + private Response getAccessTokenInternal(UriInfo ui, String authorization, String grant_type, String username, String password, String client_id, String client_secret, long ttl, String callback, boolean adminData, boolean me) throws Exception { @@ -651,7 +664,6 @@ public class ManagementResource extends AbstractContextResource { return response; } - /** * Look up Admin User via UG Central's /management/me endpoint. * @@ -678,10 +690,7 @@ public class ManagementResource extends AbstractContextResource { // use our favorite HTTP client to GET /management/me - ClientConfig clientConfig = new DefaultClientConfig(); - clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - Client client = Client.create( clientConfig ); - + Client client = getJerseyClient(); final JsonNode accessInfoNode; try { accessInfoNode = client.resource( me ) @@ -701,6 +710,50 @@ public class ManagementResource extends AbstractContextResource { } + synchronized Client getJerseyClient() { + + if ( jerseyClient == null ) { + + // create HTTPClient and with configured connection pool + + int poolSize = 100; // connections + final String poolSizeStr = properties.getProperty( CENTRAL_CONNECTION_POOL_SIZE ); + if ( poolSizeStr != null ) { + poolSize = Integer.parseInt( poolSizeStr ); + } + + MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager(); + HttpConnectionManagerParams cmParams = cm.getParams(); + cmParams.setMaxTotalConnections( poolSize ); + HttpClient httpClient = new HttpClient( cm ); + + // create Jersey Client using that HTTPClient and with configured timeouts + + int timeout = 20000; // ms + final String timeoutStr = properties.getProperty( CENTRAL_CONNECTION_TIMEOUT ); + if ( timeoutStr != null ) { + timeout = Integer.parseInt( timeoutStr ); + } + + int readTimeout = 20000; // ms + final String readTimeoutStr = properties.getProperty( CENTRAL_READ_TIMEOUT ); + if ( readTimeoutStr != null ) { + readTimeout = Integer.parseInt( readTimeoutStr ); + } + + ClientConfig clientConfig = new DefaultClientConfig(); + clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE ); + clientConfig.getProperties().put( ClientConfig.PROPERTY_CONNECT_TIMEOUT, timeout ); // ms + clientConfig.getProperties().put( ClientConfig.PROPERTY_READ_TIMEOUT, readTimeout ); // ms + + ApacheHttpClientHandler handler = new ApacheHttpClientHandler( httpClient, clientConfig ); + jerseyClient = new ApacheHttpClient( handler ); + } + + return jerseyClient; + } + + /** * Check that authentication is allowed. If external token validation is enabled (Central Usergrid SSO) * then only superusers should be allowed to login directly to this Usergrid instance.
