View Javadoc

1   /**
2    * Copyright 2005-2013 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  package org.kuali.rice.ksb.messaging;
17  
18  import org.apache.commons.httpclient.HttpMethodRetryHandler;
19  import org.apache.commons.httpclient.HttpVersion;
20  import org.apache.commons.httpclient.params.HostParams;
21  import org.apache.commons.httpclient.params.HttpClientParams;
22  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
23  import org.apache.commons.httpclient.params.HttpConnectionParams;
24  import org.apache.commons.httpclient.params.HttpMethodParams;
25  import org.apache.commons.httpclient.params.HttpParams;
26  import org.kuali.rice.core.api.util.ClassLoaderUtils;
27  
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  
33  /**
34   * Contains some utility methods for dealing with configuration of the
35   * Commons HttpClient library.  Specifically, HttpClient parameters are
36   * typed, so we can't just pipe the String values from or configuration
37   * through.  Instead we need to know the type of all the different
38   * HttpClient parameters and set the parameter accordingly.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class HttpClientHelper {
43  
44  	/**
45  	 * A Map which defines the type for all non-String parameters for HttpClient.
46  	 */
47  	private static final Map<String, Class<?>> PARAM_TYPE_MAP = new HashMap<String, Class<?>>();
48  	static {
49  		PARAM_TYPE_MAP.put(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.class);
50  		PARAM_TYPE_MAP.put(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE, Boolean.class);
51  		PARAM_TYPE_MAP.put(HttpMethodParams.SINGLE_COOKIE_HEADER, Boolean.class);
52  		PARAM_TYPE_MAP.put(HttpMethodParams.STRICT_TRANSFER_ENCODING, Boolean.class);
53  		PARAM_TYPE_MAP.put(HttpMethodParams.REJECT_HEAD_BODY, Boolean.class);
54  		PARAM_TYPE_MAP.put(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, Integer.class);
55  		PARAM_TYPE_MAP.put(HttpMethodParams.USE_EXPECT_CONTINUE, Boolean.class);
56  		PARAM_TYPE_MAP.put(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.class);
57  		PARAM_TYPE_MAP.put(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, Integer.class);
58  		PARAM_TYPE_MAP.put(HttpMethodParams.SO_TIMEOUT, Integer.class);
59  		PARAM_TYPE_MAP.put(HttpMethodParams.RETRY_HANDLER, HttpMethodRetryHandler.class);
60  		PARAM_TYPE_MAP.put(HttpMethodParams.DATE_PATTERNS, Collection.class);
61  		PARAM_TYPE_MAP.put(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, Integer.class);
62  		PARAM_TYPE_MAP.put(HttpConnectionParams.SO_TIMEOUT, Integer.class);
63  		PARAM_TYPE_MAP.put(HttpConnectionParams.TCP_NODELAY, Boolean.class);
64  		PARAM_TYPE_MAP.put(HttpConnectionParams.SO_SNDBUF, Integer.class);
65  		PARAM_TYPE_MAP.put(HttpConnectionParams.SO_RCVBUF, Integer.class);
66  		PARAM_TYPE_MAP.put(HttpConnectionParams.SO_LINGER, Integer.class);
67  		PARAM_TYPE_MAP.put(HttpConnectionParams.CONNECTION_TIMEOUT, Integer.class);
68  		PARAM_TYPE_MAP.put(HttpConnectionParams.STALE_CONNECTION_CHECK, Boolean.class);
69  		PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Map.class);
70  		PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.class);
71  		PARAM_TYPE_MAP.put(HostParams.DEFAULT_HEADERS, Collection.class);
72  		PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, Long.class);
73  		PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_CLASS, Class.class);
74  		PARAM_TYPE_MAP.put(HttpClientParams.PREEMPTIVE_AUTHENTICATION, Boolean.class);
75  		PARAM_TYPE_MAP.put(HttpClientParams.REJECT_RELATIVE_REDIRECT, Boolean.class);
76  		PARAM_TYPE_MAP.put(HttpClientParams.MAX_REDIRECTS, Integer.class);
77  		PARAM_TYPE_MAP.put(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.class);
78  	}
79  	
80  	public static void setParameter(HttpParams params, String paramName, String paramValue) {
81  		Class<?> paramType = getParameterType(paramName);
82  		if (paramType.equals(Boolean.class)) {
83  			params.setBooleanParameter(paramName, Boolean.parseBoolean(paramValue));
84  		} else if (paramType.equals(Integer.class)) {
85  			params.setIntParameter(paramName, Integer.parseInt(paramValue));
86  		} else if (paramType.equals(Long.class)) {
87  			params.setLongParameter(paramName, Long.parseLong(paramValue));
88  		} else if (paramType.equals(Double.class)) {
89  			params.setDoubleParameter(paramName, Double.parseDouble(paramValue));
90  		} else if (paramType.equals(String.class)) {
91  			params.setParameter(paramName, paramValue);
92  		} else if (paramType.equals(Class.class)) {
93  			try {
94  				Class<?> configuredClass = Class.forName(paramValue, true, ClassLoaderUtils.getDefaultClassLoader());
95  				params.setParameter(paramName, configuredClass);
96  			} catch (ClassNotFoundException e) {
97  				throw new RuntimeException("Could not locate the class needed to configure the HttpClient.", e);
98  			}
99  		} else {
100 			throw new RuntimeException("Attempted to configure an HttpClient parameter '" + paramName + "' " +
101 					"of a type not supported through Workflow configuration: " + paramType.getName());
102 		}
103 	}
104 	
105 	/**
106 	 * Returns the expected type of the given HttpClient parameter.  String is the default.
107 	 */
108 	public static Class getParameterType(String parameterName) {
109 		Class<?> parameterType = PARAM_TYPE_MAP.get(parameterName);
110 		if (parameterType == null) {
111 			parameterType = String.class;
112 		}
113 		return parameterType;
114 	}
115 	
116 }