View Javadoc
1   /**
2    * Copyright 2005-2015 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  /**
17  * Copyright 2005-2014 The Kuali Foundation
18  *
19  * Licensed under the Educational Community License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  * http://www.opensource.org/licenses/ecl2.php
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31  package org.kuali.rice.ksb.messaging.serviceconnectors;
32  
33  import org.kuali.rice.core.api.config.property.ConfigContext;
34  import org.kuali.rice.core.api.util.ClassLoaderUtils;
35  
36  import java.util.HashSet;
37  import java.util.Set;
38  
39  /**
40   * Contains some utility methods for dealing with configuration of the HttpComponents HttpClient.
41   *
42   * <p>To limit the impact of transitioning from Commons HttpClient to HttpComponents, certain legacy parameters
43   * (namely, those that map over directly) are enumerated in this class.  There are methods here to help retrieve
44   * those config param values as well.</p>
45   *
46   * <p>NOTE: The full list of supported parameters can be found in the source for this class.</p>
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public enum HttpClientParams {
51  
52      //
53      // from org.apache.commons.httpclient.params.HttpMethodParams:
54      //
55  
56      USE_EXPECT_CONTINUE("http.protocol.expect-continue", Boolean.class),
57      HTTP_CONTENT_CHARSET("http.protocol.content-charset"),
58      COOKIE_POLICY("http.protocol.cookie-policy"),
59  
60      //
61      // from org.apache.commons.httpclient.params.HttpConnectionParams:
62      //
63  
64      SO_TIMEOUT("http.socket.timeout", Integer.class),
65      TCP_NODELAY("http.tcp.nodelay", Boolean.class),
66      SO_SNDBUF("http.socket.sendbuffer", Integer.class),
67      SO_RCVBUF("http.socket.receivebuffer", Integer.class),
68      SO_LINGER("http.socket.linger", Integer.class),
69      CONNECTION_TIMEOUT("http.connection.timeout", Integer.class),
70      STALE_CONNECTION_CHECK("http.connection.stalecheck", Boolean.class),
71  
72      //
73      // from org.apache.commons.httpclient.params.HttpConnectionManagerParams:
74      //
75  
76      MAX_TOTAL_CONNECTIONS("http.connection-manager.max-total", Integer.class),
77  
78      //
79      // from org.apache.commons.httpclient.params.HttpClientParams:
80      //
81  
82      CONNECTION_MANAGER_TIMEOUT("http.connection-manager.timeout", Integer.class),
83      REJECT_RELATIVE_REDIRECT("http.protocol.reject-relative-redirect", Boolean.class),
84      MAX_REDIRECTS("http.protocol.max-redirects", Integer.class),
85      ALLOW_CIRCULAR_REDIRECTS("http.protocol.allow-circular-redirects", Boolean.class);
86  
87      private String paramName;
88      private Class paramValueClass;
89  
90      private static final Set<String> supportedParamNames = new HashSet<String>();
91  
92      private HttpClientParams(String paramName, Class paramValueClass) {
93          this.paramName = paramName;
94          this.paramValueClass = paramValueClass;
95      }
96  
97      private HttpClientParams(String paramName) {
98          this(paramName, String.class);
99      }
100 
101     public <T> T getValue() {
102         return getValueOrDefault(null);
103     }
104 
105     public <T> T getValueOrDefault(T defaultValue) {
106         T value = null;
107         String strValue = ConfigContext.getCurrentContextConfig().getProperty(getParamName());
108 
109         if (strValue == null) {
110             return defaultValue;
111         }
112 
113         Class<?> paramType = getParamValueClass();
114 
115         if (paramType.equals(Boolean.class)) {
116             value = (T) ConfigContext.getCurrentContextConfig().getBooleanProperty(getParamName());
117         } else if (paramType.equals(Integer.class)) {
118             value = (T) Integer.valueOf(strValue);
119         } else if (paramType.equals(Long.class)) {
120             value = (T) Long.valueOf(strValue);
121         } else if (paramType.equals(Double.class)) {
122             value = (T) Double.valueOf(strValue);
123         } else if (paramType.equals(String.class)) {
124             value = (T) strValue;
125         } else if (paramType.equals(Class.class)) {
126             try {
127                 value = (T) Class.forName(ConfigContext.getCurrentContextConfig().getProperty(getParamName()),
128                         true, ClassLoaderUtils.getDefaultClassLoader());
129             } catch (ClassNotFoundException e) {
130                 throw new RuntimeException("Could not locate the class needed to configure the HttpClient.", e);
131             }
132         } else {
133             throw new RuntimeException("Attempted to configure an HttpClient parameter '" + getParamName() + "' " +
134                     "of a type not supported through Workflow configuration: " + getParamValueClass().getName());
135         }
136 
137         // this may be redundant except in weird cases
138         if (value == null) {
139             return defaultValue;
140         }
141 
142         return value;
143 	}
144 
145     public String getParamName() {
146         return paramName;
147     }
148 
149     public Class getParamValueClass() {
150         return paramValueClass;
151     }
152 }