Coverage Report - org.kuali.rice.ksb.messaging.HttpClientHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpClientHelper
0%
0/55
0%
0/14
6
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.ksb.messaging;
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.commons.httpclient.HttpMethodRetryHandler;
 24  
 import org.apache.commons.httpclient.HttpVersion;
 25  
 import org.apache.commons.httpclient.params.HostParams;
 26  
 import org.apache.commons.httpclient.params.HttpClientParams;
 27  
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 28  
 import org.apache.commons.httpclient.params.HttpConnectionParams;
 29  
 import org.apache.commons.httpclient.params.HttpMethodParams;
 30  
 import org.apache.commons.httpclient.params.HttpParams;
 31  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 32  
 
 33  
 
 34  
 /**
 35  
  * Contains some utility methods for dealing with configuration of the
 36  
  * Commons HttpClient library.  Specifically, HttpClient parameters are
 37  
  * typed, so we can't just pipe the String values from or configuration
 38  
  * through.  Instead we need to know the type of all the different
 39  
  * HttpClient parameters and set the parameter accordingly.
 40  
  * 
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  0
 public class HttpClientHelper {
 44  
 
 45  
         /**
 46  
          * A Map which defines the type for all non-String parameters for HttpClient.
 47  
          */
 48  0
         private static final Map<String, Class<?>> PARAM_TYPE_MAP = new HashMap<String, Class<?>>();
 49  
         static {
 50  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.class);
 51  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.UNAMBIGUOUS_STATUS_LINE, Boolean.class);
 52  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.SINGLE_COOKIE_HEADER, Boolean.class);
 53  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.STRICT_TRANSFER_ENCODING, Boolean.class);
 54  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.REJECT_HEAD_BODY, Boolean.class);
 55  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, Integer.class);
 56  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.USE_EXPECT_CONTINUE, Boolean.class);
 57  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.class);
 58  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, Integer.class);
 59  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.SO_TIMEOUT, Integer.class);
 60  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.RETRY_HANDLER, HttpMethodRetryHandler.class);
 61  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.DATE_PATTERNS, Collection.class);
 62  0
                 PARAM_TYPE_MAP.put(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, Integer.class);
 63  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_TIMEOUT, Integer.class);
 64  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.TCP_NODELAY, Boolean.class);
 65  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_SNDBUF, Integer.class);
 66  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_RCVBUF, Integer.class);
 67  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.SO_LINGER, Integer.class);
 68  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.CONNECTION_TIMEOUT, Integer.class);
 69  0
                 PARAM_TYPE_MAP.put(HttpConnectionParams.STALE_CONNECTION_CHECK, Boolean.class);
 70  0
                 PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Map.class);
 71  0
                 PARAM_TYPE_MAP.put(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.class);
 72  0
                 PARAM_TYPE_MAP.put(HostParams.DEFAULT_HEADERS, Collection.class);
 73  0
                 PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, Long.class);
 74  0
                 PARAM_TYPE_MAP.put(HttpClientParams.CONNECTION_MANAGER_CLASS, Class.class);
 75  0
                 PARAM_TYPE_MAP.put(HttpClientParams.PREEMPTIVE_AUTHENTICATION, Boolean.class);
 76  0
                 PARAM_TYPE_MAP.put(HttpClientParams.REJECT_RELATIVE_REDIRECT, Boolean.class);
 77  0
                 PARAM_TYPE_MAP.put(HttpClientParams.MAX_REDIRECTS, Integer.class);
 78  0
                 PARAM_TYPE_MAP.put(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.class);
 79  0
         }
 80  
         
 81  
         public static void setParameter(HttpParams params, String paramName, String paramValue) {
 82  0
                 Class<?> paramType = getParameterType(paramName);
 83  0
                 if (paramType.equals(Boolean.class)) {
 84  0
                         params.setBooleanParameter(paramName, Boolean.parseBoolean(paramValue));
 85  0
                 } else if (paramType.equals(Integer.class)) {
 86  0
                         params.setIntParameter(paramName, Integer.parseInt(paramValue));
 87  0
                 } else if (paramType.equals(Long.class)) {
 88  0
                         params.setLongParameter(paramName, Long.parseLong(paramValue));
 89  0
                 } else if (paramType.equals(Double.class)) {
 90  0
                         params.setDoubleParameter(paramName, Double.parseDouble(paramValue));
 91  0
                 } else if (paramType.equals(String.class)) {
 92  0
                         params.setParameter(paramName, paramValue);
 93  0
                 } else if (paramType.equals(Class.class)) {
 94  
                         try {
 95  0
                                 Class<?> configuredClass = Class.forName(paramValue, true, ClassLoaderUtils.getDefaultClassLoader());
 96  0
                                 params.setParameter(paramName, configuredClass);
 97  0
                         } catch (ClassNotFoundException e) {
 98  0
                                 throw new RuntimeException("Could not locate the class needed to configure the HttpClient.", e);
 99  0
                         }
 100  
                 } else {
 101  0
                         throw new RuntimeException("Attempted to configure an HttpClient parameter '" + paramName + "' " +
 102  
                                         "of a type not supported through Workflow configuration: " + paramType.getName());
 103  
                 }
 104  0
         }
 105  
         
 106  
         /**
 107  
          * Returns the expected type of the given HttpClient parameter.  String is the default.
 108  
          */
 109  
         public static Class getParameterType(String parameterName) {
 110  0
                 Class<?> parameterType = PARAM_TYPE_MAP.get(parameterName);
 111  0
                 if (parameterType == null) {
 112  0
                         parameterType = String.class;
 113  
                 }
 114  0
                 return parameterType;
 115  
         }
 116  
         
 117  
 }