| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.kuali.rice.ksb.messaging.serviceconnectors; |
| 18 |
|
|
| 19 |
|
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; |
| 20 |
|
import org.apache.commons.httpclient.HostConfiguration; |
| 21 |
|
import org.apache.commons.httpclient.HttpClient; |
| 22 |
|
import org.apache.commons.httpclient.HttpMethod; |
| 23 |
|
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
| 24 |
|
import org.apache.commons.httpclient.cookie.CookiePolicy; |
| 25 |
|
import org.apache.commons.httpclient.params.HttpClientParams; |
| 26 |
|
import org.apache.commons.httpclient.params.HttpConnectionManagerParams; |
| 27 |
|
import org.apache.commons.httpclient.params.HttpConnectionParams; |
| 28 |
|
import org.apache.commons.httpclient.params.HttpMethodParams; |
| 29 |
|
import org.apache.commons.httpclient.params.HttpParams; |
| 30 |
|
import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread; |
| 31 |
|
import org.apache.commons.lang.StringUtils; |
| 32 |
|
import org.apache.log4j.Logger; |
| 33 |
|
import org.kuali.rice.core.api.config.property.ConfigContext; |
| 34 |
|
import org.kuali.rice.ksb.messaging.HttpClientHelper; |
| 35 |
|
import org.kuali.rice.ksb.messaging.KSBHttpInvokerProxyFactoryBean; |
| 36 |
|
import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor; |
| 37 |
|
import org.kuali.rice.ksb.messaging.ServiceInfo; |
| 38 |
|
import org.kuali.rice.ksb.security.httpinvoker.AuthenticationCommonsHttpInvokerRequestExecutor; |
| 39 |
|
|
| 40 |
|
import java.io.IOException; |
| 41 |
|
import java.net.SocketException; |
| 42 |
|
import java.net.SocketTimeoutException; |
| 43 |
|
import java.util.HashMap; |
| 44 |
|
import java.util.Iterator; |
| 45 |
|
import java.util.Map; |
| 46 |
|
import java.util.Properties; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@author |
| 51 |
|
@since |
| 52 |
|
|
|
|
|
| 0% |
Uncovered Elements: 79 (79) |
Complexity: 17 |
Complexity Density: 0.31 |
|
| 53 |
|
public class HttpInvokerConnector extends AbstractServiceConnector { |
| 54 |
|
|
| 55 |
|
private static final Logger LOG = Logger.getLogger(HttpInvokerConnector.class); |
| 56 |
|
|
| 57 |
|
private HttpClientParams httpClientParams; |
| 58 |
|
|
| 59 |
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 71 |
0
|
public HttpInvokerConnector(final ServiceInfo serviceInfo) {... |
| 72 |
0
|
super(serviceInfo); |
| 73 |
0
|
initializeHttpClientParams(); |
| 74 |
|
} |
| 75 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
| 76 |
0
|
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 |
0
|
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 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 102 |
0
|
public HttpClient getHttpClient() {... |
| 103 |
0
|
return new HttpClient(this.httpClientParams); |
| 104 |
|
} |
| 105 |
|
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 106 |
0
|
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 |
|
} |
| 118 |
0
|
runIdleConnectionTimeout(); |
| 119 |
0
|
this.httpClientInitialized = true; |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
|
| 124 |
0
|
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 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 153 |
0
|
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 |
|
|
| 168 |
0
|
ictt.start(); |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 172 |
0
|
public static void shutdownIdleConnectionTimeout() {... |
| 173 |
0
|
if (ictt != null) { |
| 174 |
0
|
try { |
| 175 |
0
|
ictt.shutdown(); |
| 176 |
|
} catch (Exception e) { |
| 177 |
0
|
LOG.error("Failed to shutdown idle connection thread.", e); |
| 178 |
|
} |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 6 |
Complexity Density: 0.6 |
|
| 182 |
|
private static final class CustomHttpMethodRetryHandler extends DefaultHttpMethodRetryHandler { |
| 183 |
|
|
| 184 |
|
private static final int MAX_RETRIES = 1; |
| 185 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 186 |
0
|
public CustomHttpMethodRetryHandler() {... |
| 187 |
0
|
super(MAX_RETRIES, true); |
| 188 |
|
} |
| 189 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
| 190 |
0
|
@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 |
|
} |