001 /** 002 * Copyright 2005-2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ksb.messaging; 017 018 import org.apache.commons.httpclient.HttpMethodRetryHandler; 019 import org.apache.commons.httpclient.HttpVersion; 020 import org.apache.commons.httpclient.params.HostParams; 021 import org.apache.commons.httpclient.params.HttpClientParams; 022 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 023 import org.apache.commons.httpclient.params.HttpConnectionParams; 024 import org.apache.commons.httpclient.params.HttpMethodParams; 025 import org.apache.commons.httpclient.params.HttpParams; 026 import org.kuali.rice.core.api.util.ClassLoaderUtils; 027 028 import java.util.Collection; 029 import java.util.HashMap; 030 import java.util.Map; 031 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 (rice.collab@kuali.org) 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, HttpVersion.class); 050 PARAM_TYPE_MAP.put(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE, Boolean.class); 051 PARAM_TYPE_MAP.put(HttpMethodParams.SINGLE_COOKIE_HEADER, Boolean.class); 052 PARAM_TYPE_MAP.put(HttpMethodParams.STRICT_TRANSFER_ENCODING, Boolean.class); 053 PARAM_TYPE_MAP.put(HttpMethodParams.REJECT_HEAD_BODY, Boolean.class); 054 PARAM_TYPE_MAP.put(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, Integer.class); 055 PARAM_TYPE_MAP.put(HttpMethodParams.USE_EXPECT_CONTINUE, Boolean.class); 056 PARAM_TYPE_MAP.put(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.class); 057 PARAM_TYPE_MAP.put(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, Integer.class); 058 PARAM_TYPE_MAP.put(HttpMethodParams.SO_TIMEOUT, Integer.class); 059 PARAM_TYPE_MAP.put(HttpMethodParams.RETRY_HANDLER, HttpMethodRetryHandler.class); 060 PARAM_TYPE_MAP.put(HttpMethodParams.DATE_PATTERNS, Collection.class); 061 PARAM_TYPE_MAP.put(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, Integer.class); 062 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_TIMEOUT, Integer.class); 063 PARAM_TYPE_MAP.put(HttpConnectionParams.TCP_NODELAY, Boolean.class); 064 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_SNDBUF, Integer.class); 065 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_RCVBUF, Integer.class); 066 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_LINGER, Integer.class); 067 PARAM_TYPE_MAP.put(HttpConnectionParams.CONNECTION_TIMEOUT, Integer.class); 068 PARAM_TYPE_MAP.put(HttpConnectionParams.STALE_CONNECTION_CHECK, Boolean.class); 069 PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Map.class); 070 PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.class); 071 PARAM_TYPE_MAP.put(HostParams.DEFAULT_HEADERS, Collection.class); 072 PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, Long.class); 073 PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_CLASS, Class.class); 074 PARAM_TYPE_MAP.put(HttpClientParams.PREEMPTIVE_AUTHENTICATION, Boolean.class); 075 PARAM_TYPE_MAP.put(HttpClientParams.REJECT_RELATIVE_REDIRECT, Boolean.class); 076 PARAM_TYPE_MAP.put(HttpClientParams.MAX_REDIRECTS, Integer.class); 077 PARAM_TYPE_MAP.put(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.class); 078 } 079 080 public static void setParameter(HttpParams params, String paramName, String paramValue) { 081 Class<?> paramType = getParameterType(paramName); 082 if (paramType.equals(Boolean.class)) { 083 params.setBooleanParameter(paramName, Boolean.parseBoolean(paramValue)); 084 } else if (paramType.equals(Integer.class)) { 085 params.setIntParameter(paramName, Integer.parseInt(paramValue)); 086 } else if (paramType.equals(Long.class)) { 087 params.setLongParameter(paramName, Long.parseLong(paramValue)); 088 } else if (paramType.equals(Double.class)) { 089 params.setDoubleParameter(paramName, Double.parseDouble(paramValue)); 090 } else if (paramType.equals(String.class)) { 091 params.setParameter(paramName, paramValue); 092 } else if (paramType.equals(Class.class)) { 093 try { 094 Class<?> configuredClass = Class.forName(paramValue, true, ClassLoaderUtils.getDefaultClassLoader()); 095 params.setParameter(paramName, configuredClass); 096 } catch (ClassNotFoundException e) { 097 throw new RuntimeException("Could not locate the class needed to configure the HttpClient.", e); 098 } 099 } 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 }