Coverage Report - org.kuali.rice.ksb.messaging.serviceconnectors.HttpInvokerConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpInvokerConnector
0%
0/65
0%
0/18
2.4
HttpInvokerConnector$CustomHttpMethodRetryHandler
0%
0/11
0%
0/8
2.4
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.ksb.messaging.serviceconnectors;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.net.SocketException;
 21  
 import java.net.SocketTimeoutException;
 22  
 import java.net.URL;
 23  
 import java.util.HashMap;
 24  
 import java.util.Iterator;
 25  
 import java.util.Map;
 26  
 import java.util.Properties;
 27  
 
 28  
 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
 29  
 import org.apache.commons.httpclient.HostConfiguration;
 30  
 import org.apache.commons.httpclient.HttpClient;
 31  
 import org.apache.commons.httpclient.HttpMethod;
 32  
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 33  
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 34  
 import org.apache.commons.httpclient.params.HttpClientParams;
 35  
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 36  
 import org.apache.commons.httpclient.params.HttpConnectionParams;
 37  
 import org.apache.commons.httpclient.params.HttpMethodParams;
 38  
 import org.apache.commons.httpclient.params.HttpParams;
 39  
 import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
 40  
 import org.apache.commons.lang.StringUtils;
 41  
 import org.apache.log4j.Logger;
 42  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 43  
 import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
 44  
 import org.kuali.rice.ksb.messaging.HttpClientHelper;
 45  
 import org.kuali.rice.ksb.messaging.KSBHttpInvokerProxyFactoryBean;
 46  
 import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor;
 47  
 import org.kuali.rice.ksb.security.httpinvoker.AuthenticationCommonsHttpInvokerRequestExecutor;
 48  
 
 49  
 
 50  
 /**
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  * @since 0.9
 53  
  */
 54  0
 public class HttpInvokerConnector extends AbstractServiceConnector {
 55  
 
 56  0
         private static final Logger LOG = Logger.getLogger(HttpInvokerConnector.class);
 57  
 
 58  
         private HttpClientParams httpClientParams;
 59  
 
 60  0
         private boolean httpClientInitialized = false;
 61  
 
 62  
         private static final String IDLE_CONNECTION_THREAD_INTERVAL_PROPERTY = "ksb.thinClient.idleConnectionThreadInterval";
 63  
         private static final String IDLE_CONNECTION_TIMEOUT_PROPERTY = "ksb.thinClient.idleConnectionTimeout";
 64  
         private static final String DEFAULT_IDLE_CONNECTION_THREAD_INTERVAL = "7500";
 65  
         private static final String DEFAULT_IDLE_CONNECTION_TIMEOUT = "5000";
 66  
         private static final String RETRY_SOCKET_EXCEPTION_PROPERTY = "ksb.thinClient.retrySocketException";
 67  
         
 68  
         private static IdleConnectionTimeoutThread ictt;
 69  
         
 70  
         public HttpInvokerConnector(final JavaServiceConfiguration serviceConfiguration, final URL alternateEndpointUrl) {
 71  0
                 super(serviceConfiguration, alternateEndpointUrl);
 72  0
                 initializeHttpClientParams();
 73  0
         }
 74  
         
 75  
         protected JavaServiceConfiguration getJavaServiceConfiguration() {
 76  0
                 return (JavaServiceConfiguration)getServiceConfiguration();
 77  
         }
 78  
         
 79  
         public Object getService() {
 80  0
             LOG.debug("Getting connector for endpoint " + getActualEndpointUrl());
 81  0
                 KSBHttpInvokerProxyFactoryBean client = new KSBHttpInvokerProxyFactoryBean();
 82  0
                 client.setServiceUrl(getActualEndpointUrl().toExternalForm());
 83  0
                 client.setServiceConfiguration(getJavaServiceConfiguration());
 84  
                 
 85  
                 KSBHttpInvokerRequestExecutor executor;
 86  
                 
 87  0
                 if (getCredentialsSource() != null) {
 88  0
                     executor = new AuthenticationCommonsHttpInvokerRequestExecutor(getHttpClient(), getCredentialsSource(), getJavaServiceConfiguration());
 89  
                 } else {
 90  0
                     executor = new KSBHttpInvokerRequestExecutor(getHttpClient());
 91  
                 }
 92  0
                 executor.setSecure(getJavaServiceConfiguration().getBusSecurity());
 93  0
                 client.setHttpInvokerRequestExecutor(executor);        
 94  0
                 client.afterPropertiesSet();
 95  0
                 return getServiceProxyWithFailureMode(client.getObject(), getJavaServiceConfiguration());
 96  
         }
 97  
 
 98  
         /**
 99  
          * Creates a commons HttpClient for service invocation. Config parameters
 100  
          * that start with http.* are used to configure the client.
 101  
          * 
 102  
          * TODO we need to add support for other invocation protocols and
 103  
          * implementations, but for now...
 104  
          */
 105  
         public HttpClient getHttpClient() {
 106  0
                 return new HttpClient(this.httpClientParams);
 107  
         }
 108  
 
 109  
         protected void initializeHttpClientParams() {
 110  0
                 synchronized (HttpInvokerConnector.class) {
 111  0
                 if (! this.httpClientInitialized) {
 112  0
                     this.httpClientParams = new HttpClientParams();
 113  0
                         configureDefaultHttpClientParams(this.httpClientParams);
 114  0
                         Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
 115  0
                         for (Iterator<Object> iterator = configProps.keySet().iterator(); iterator.hasNext();) {
 116  0
                                 String paramName = (String) iterator.next();
 117  0
                                 if (paramName.startsWith("http.")) {
 118  0
                                         HttpClientHelper.setParameter(this.httpClientParams, paramName, (String) configProps.get(paramName));
 119  
                                 }
 120  0
                         }
 121  0
                                 runIdleConnectionTimeout();
 122  0
                         this.httpClientInitialized = true;
 123  
                 }
 124  0
         }
 125  0
         }
 126  
 
 127  
         protected void configureDefaultHttpClientParams(HttpParams params) {
 128  0
                 params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS, MultiThreadedHttpConnectionManager.class);
 129  0
                 params.setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109);
 130  0
                 params.setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 10000);
 131  0
                 Map<HostConfiguration, Integer> maxHostConnectionsMap = new HashMap<HostConfiguration, Integer>();
 132  0
                 maxHostConnectionsMap.put(HostConfiguration.ANY_HOST_CONFIGURATION, new Integer(20));
 133  0
                 params.setParameter(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, maxHostConnectionsMap);
 134  0
                 params.setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 20);
 135  0
                 params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
 136  0
                 params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2*60*1000);
 137  
                 
 138  
 
 139  0
                 boolean retrySocketException = new Boolean(ConfigContext.getCurrentContextConfig().getProperty(RETRY_SOCKET_EXCEPTION_PROPERTY));
 140  0
                 if (retrySocketException) {
 141  0
                     LOG.info("Installing custom HTTP retry handler to retry requests in face of SocketExceptions");
 142  0
                     params.setParameter(HttpMethodParams.RETRY_HANDLER, new CustomHttpMethodRetryHandler());
 143  
                 }
 144  
 
 145  
                 
 146  0
         }
 147  
         
 148  
 
 149  
         
 150  
         /**
 151  
          * Idle connection timeout thread added as a part of the fix for ensuring that 
 152  
          * threads that timed out need to be cleaned or and send back to the pool so that 
 153  
          * other clients can use it.
 154  
          *
 155  
          */
 156  
         private void runIdleConnectionTimeout() {
 157  0
             if (ictt != null) {
 158  0
                     String timeoutInterval = ConfigContext.getCurrentContextConfig().getProperty(IDLE_CONNECTION_THREAD_INTERVAL_PROPERTY);
 159  0
                     if (StringUtils.isBlank(timeoutInterval)) {
 160  0
                         timeoutInterval = DEFAULT_IDLE_CONNECTION_THREAD_INTERVAL;
 161  
                     }
 162  0
                     String connectionTimeout = ConfigContext.getCurrentContextConfig().getProperty(IDLE_CONNECTION_TIMEOUT_PROPERTY);
 163  0
                     if (StringUtils.isBlank(connectionTimeout)) {
 164  0
                         connectionTimeout = DEFAULT_IDLE_CONNECTION_TIMEOUT;
 165  
                     }
 166  
                     
 167  0
                     ictt.addConnectionManager(getHttpClient().getHttpConnectionManager());
 168  0
                     ictt.setTimeoutInterval(new Integer(timeoutInterval));
 169  0
                     ictt.setConnectionTimeout(new Integer(connectionTimeout));
 170  
                     //start the thread
 171  0
                     ictt.start();
 172  
             }
 173  0
         }
 174  
         
 175  
         public static void shutdownIdleConnectionTimeout() {
 176  0
                 if (ictt != null) {
 177  
                         try {
 178  0
                                 ictt.shutdown();
 179  0
                         } catch (Exception e) {
 180  0
                                 LOG.error("Failed to shutdown idle connection thread.", e);
 181  0
                         }
 182  
                 }
 183  0
         }
 184  
         
 185  
         private static final class CustomHttpMethodRetryHandler extends DefaultHttpMethodRetryHandler {
 186  
 
 187  
                 private static final int MAX_RETRIES = 1;
 188  
                 
 189  
                 public CustomHttpMethodRetryHandler() {
 190  0
                         super(MAX_RETRIES, true);
 191  0
         }
 192  
 
 193  
                 @Override
 194  
             public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
 195  0
                 boolean shouldRetry = super.retryMethod(method, exception, executionCount);
 196  0
                 if (!shouldRetry && executionCount < MAX_RETRIES) {
 197  0
                         if (exception instanceof SocketException) {
 198  0
                                 LOG.warn("Retrying request because of SocketException!", exception);
 199  0
                                 shouldRetry = true;
 200  0
                         } else if (exception instanceof SocketTimeoutException) {
 201  0
                                 LOG.warn("Retrying request because of SocketTimeoutException!", exception);
 202  0
                                 shouldRetry = true;
 203  
                         }
 204  
                 }
 205  0
                 return shouldRetry;
 206  
             }
 207  
             
 208  
         }
 209  
         
 210  
 
 211  
         
 212  
 }