001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.messaging.serviceconnectors;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.commons.httpclient.HostConfiguration;
025: import org.apache.commons.httpclient.HttpClient;
026: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
027: import org.apache.commons.httpclient.cookie.CookiePolicy;
028: import org.apache.commons.httpclient.params.HttpClientParams;
029: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
030: import org.apache.commons.httpclient.params.HttpConnectionParams;
031: import org.apache.commons.httpclient.params.HttpMethodParams;
032: import org.apache.commons.httpclient.params.HttpParams;
033: import org.apache.log4j.Logger;
034: import org.kuali.bus.security.httpinvoker.AuthenticationCommonsHttpInvokerRequestExecutor;
035: import org.kuali.rice.core.Core;
036:
037: import edu.iu.uis.eden.messaging.HttpClientHelper;
038: import edu.iu.uis.eden.messaging.KEWHttpInvokerProxyFactoryBean;
039: import edu.iu.uis.eden.messaging.KEWHttpInvokerRequestExecutor;
040: import edu.iu.uis.eden.messaging.RemotedServiceHolder;
041: import edu.iu.uis.eden.messaging.ServiceInfo;
042:
043: /**
044: *
045: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
046: * @version $Revision: 1.2.24.1 $ $Date: 2007/10/17 20:32:04 $
047: * @since 0.9
048: */
049: public class HttpInvokerConnector extends AbstractServiceConnector {
050:
051: private static final Logger LOG = Logger
052: .getLogger(HttpInvokerConnector.class);
053:
054: private HttpClientParams httpClientParams;
055:
056: private boolean httpClientInitialized = false;
057:
058: public HttpInvokerConnector(final ServiceInfo serviceInfo) {
059: super (serviceInfo);
060: initializeHttpClientParams();
061: }
062:
063: public RemotedServiceHolder getServiceHolder() throws Exception {
064: LOG.debug("Getting connector for endpoint "
065: + this .getServiceInfo().getEndpointUrl());
066: KEWHttpInvokerProxyFactoryBean client = new KEWHttpInvokerProxyFactoryBean();
067: client.setServiceUrl(this .getServiceInfo().getEndpointUrl());
068: client.setServiceInfo(this .getServiceInfo());
069:
070: KEWHttpInvokerRequestExecutor executor;
071:
072: if (getCredentialsSource() != null) {
073: executor = new AuthenticationCommonsHttpInvokerRequestExecutor(
074: getHttpClient(), getCredentialsSource(),
075: getServiceInfo());
076: } else {
077: executor = new KEWHttpInvokerRequestExecutor(
078: getHttpClient());
079: }
080: executor.setSecure(this .getServiceInfo().getServiceDefinition()
081: .getBusSecurity());
082: client.setHttpInvokerRequestExecutor(executor);
083: client.afterPropertiesSet();
084: return new RemotedServiceHolder(getServiceProxyWithFailureMode(
085: client.getObject(), this .getServiceInfo()), this
086: .getServiceInfo());
087: }
088:
089: /**
090: * Creates a commons HttpClient for service invocation. Config parameters
091: * that start with http.* are used to configure the client.
092: *
093: * TODO we need to add support for other invocation protocols and
094: * implementations, but for now...
095: */
096: public HttpClient getHttpClient() {
097: return new HttpClient(this .httpClientParams);
098: }
099:
100: protected void initializeHttpClientParams() {
101: if (!this .httpClientInitialized) {
102: this .httpClientParams = new HttpClientParams();
103: configureDefaultHttpClientParams(this .httpClientParams);
104: Properties configProps = Core.getCurrentContextConfig()
105: .getProperties();
106: for (Iterator iterator = configProps.keySet().iterator(); iterator
107: .hasNext();) {
108: String paramName = (String) iterator.next();
109: if (paramName.startsWith("http.")) {
110: HttpClientHelper.setParameter(
111: this .httpClientParams, paramName,
112: (String) configProps.get(paramName));
113: }
114: }
115: this .httpClientInitialized = true;
116: }
117: }
118:
119: protected void configureDefaultHttpClientParams(HttpParams params) {
120: params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS,
121: MultiThreadedHttpConnectionManager.class);
122: params.setParameter(HttpMethodParams.COOKIE_POLICY,
123: CookiePolicy.RFC_2109);
124: params.setLongParameter(
125: HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 10000);
126: Map<HostConfiguration, Integer> maxHostConnectionsMap = new HashMap<HostConfiguration, Integer>();
127: maxHostConnectionsMap.put(
128: HostConfiguration.ANY_HOST_CONFIGURATION, new Integer(
129: 20));
130: params.setParameter(
131: HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
132: maxHostConnectionsMap);
133: params.setIntParameter(
134: HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 20);
135: params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT,
136: 10000);
137: }
138: }
|