1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38
39
40
41
42 public class HttpClientHelper {
43
44
45
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
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 }