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