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;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.httpclient.HttpMethodRetryHandler;
024: import org.apache.commons.httpclient.HttpVersion;
025: import org.apache.commons.httpclient.params.HostParams;
026: import org.apache.commons.httpclient.params.HttpClientParams;
027: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
028: import org.apache.commons.httpclient.params.HttpConnectionParams;
029: import org.apache.commons.httpclient.params.HttpMethodParams;
030: import org.apache.commons.httpclient.params.HttpParams;
031: import org.kuali.rice.util.ClassLoaderUtils;
032:
033: /**
034: * Contains some utility methods for dealing with configuration of the
035: * Commons HttpClient library. Specifically, HttpClient parameters are
036: * typed, so we can't just pipe the String values from or configuration
037: * through. Instead we need to know the type of all the different
038: * HttpClient parameters and set the parameter accordingly.
039: *
040: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
041: */
042: public class HttpClientHelper {
043:
044: /**
045: * A Map which defines the type for all non-String parameters for HttpClient.
046: */
047: private static final Map<String, Class<?>> PARAM_TYPE_MAP = new HashMap<String, Class<?>>();
048: static {
049: PARAM_TYPE_MAP.put(HttpMethodParams.PROTOCOL_VERSION,
050: HttpVersion.class);
051: PARAM_TYPE_MAP.put(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE,
052: Boolean.class);
053: PARAM_TYPE_MAP.put(HttpMethodParams.SINGLE_COOKIE_HEADER,
054: Boolean.class);
055: PARAM_TYPE_MAP.put(HttpMethodParams.STRICT_TRANSFER_ENCODING,
056: Boolean.class);
057: PARAM_TYPE_MAP.put(HttpMethodParams.REJECT_HEAD_BODY,
058: Boolean.class);
059: PARAM_TYPE_MAP.put(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT,
060: Integer.class);
061: PARAM_TYPE_MAP.put(HttpMethodParams.USE_EXPECT_CONTINUE,
062: Boolean.class);
063: PARAM_TYPE_MAP.put(HttpMethodParams.WARN_EXTRA_INPUT,
064: Boolean.class);
065: PARAM_TYPE_MAP.put(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT,
066: Integer.class);
067: PARAM_TYPE_MAP.put(HttpMethodParams.SO_TIMEOUT, Integer.class);
068: PARAM_TYPE_MAP.put(HttpMethodParams.RETRY_HANDLER,
069: HttpMethodRetryHandler.class);
070: PARAM_TYPE_MAP.put(HttpMethodParams.DATE_PATTERNS,
071: Collection.class);
072: PARAM_TYPE_MAP.put(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT,
073: Integer.class);
074: PARAM_TYPE_MAP.put(HttpConnectionParams.SO_TIMEOUT,
075: Integer.class);
076: PARAM_TYPE_MAP.put(HttpConnectionParams.TCP_NODELAY,
077: Boolean.class);
078: PARAM_TYPE_MAP.put(HttpConnectionParams.SO_SNDBUF,
079: Integer.class);
080: PARAM_TYPE_MAP.put(HttpConnectionParams.SO_RCVBUF,
081: Integer.class);
082: PARAM_TYPE_MAP.put(HttpConnectionParams.SO_LINGER,
083: Integer.class);
084: PARAM_TYPE_MAP.put(HttpConnectionParams.CONNECTION_TIMEOUT,
085: Integer.class);
086: PARAM_TYPE_MAP.put(HttpConnectionParams.STALE_CONNECTION_CHECK,
087: Boolean.class);
088: PARAM_TYPE_MAP.put(
089: HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
090: Map.class);
091: PARAM_TYPE_MAP.put(
092: HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
093: Integer.class);
094: PARAM_TYPE_MAP
095: .put(HostParams.DEFAULT_HEADERS, Collection.class);
096: PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_TIMEOUT,
097: Long.class);
098: PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_CLASS,
099: Class.class);
100: PARAM_TYPE_MAP.put(HttpClientParams.PREEMPTIVE_AUTHENTICATION,
101: Boolean.class);
102: PARAM_TYPE_MAP.put(HttpClientParams.REJECT_RELATIVE_REDIRECT,
103: Boolean.class);
104: PARAM_TYPE_MAP.put(HttpClientParams.MAX_REDIRECTS,
105: Integer.class);
106: PARAM_TYPE_MAP.put(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS,
107: Boolean.class);
108: }
109:
110: public static void setParameter(HttpParams params,
111: String paramName, String paramValue) {
112: Class<?> paramType = getParameterType(paramName);
113: if (paramType.equals(Boolean.class)) {
114: params.setBooleanParameter(paramName, Boolean
115: .parseBoolean(paramValue));
116: } else if (paramType.equals(Integer.class)) {
117: params.setIntParameter(paramName, Integer
118: .parseInt(paramValue));
119: } else if (paramType.equals(Long.class)) {
120: params.setLongParameter(paramName, Long
121: .parseLong(paramValue));
122: } else if (paramType.equals(Double.class)) {
123: params.setDoubleParameter(paramName, Double
124: .parseDouble(paramValue));
125: } else if (paramType.equals(String.class)) {
126: params.setParameter(paramName, paramValue);
127: } else if (paramType.equals(Class.class)) {
128: try {
129: Class<?> configuredClass = Class.forName(paramValue,
130: true, ClassLoaderUtils.getDefaultClassLoader());
131: params.setParameter(paramName, configuredClass);
132: } catch (ClassNotFoundException e) {
133: throw new RuntimeException(
134: "Could not locate the class needed to configure the HttpClient.",
135: e);
136: }
137: } else {
138: throw new RuntimeException(
139: "Attempted to configure an HttpClient parameter '"
140: + paramName
141: + "' "
142: + "of a type not supported through Workflow configuration: "
143: + paramType.getName());
144: }
145: }
146:
147: /**
148: * Returns the expected type of the given HttpClient parameter. String is the default.
149: */
150: public static Class getParameterType(String parameterName) {
151: Class<?> parameterType = PARAM_TYPE_MAP.get(parameterName);
152: if (parameterType == null) {
153: parameterType = String.class;
154: }
155: return parameterType;
156: }
157:
158: }
|