001/**
002 * Copyright 2005-2017 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/**
017* Copyright 2005-2014 The Kuali Foundation
018*
019* Licensed under the Educational Community License, Version 2.0 (the "License");
020* you may not use this file except in compliance with the License.
021* You may obtain a copy of the License at
022*
023* http://www.opensource.org/licenses/ecl2.php
024*
025* Unless required by applicable law or agreed to in writing, software
026* distributed under the License is distributed on an "AS IS" BASIS,
027* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
028* See the License for the specific language governing permissions and
029* limitations under the License.
030*/
031package org.kuali.rice.ksb.messaging.serviceconnectors;
032
033import org.kuali.rice.core.api.config.property.ConfigContext;
034import org.kuali.rice.core.api.util.ClassLoaderUtils;
035
036import java.util.HashSet;
037import java.util.Set;
038
039/**
040 * Contains some utility methods for dealing with configuration of the HttpComponents HttpClient.
041 *
042 * <p>To limit the impact of transitioning from Commons HttpClient to HttpComponents, certain legacy parameters
043 * (namely, those that map over directly) are enumerated in this class.  There are methods here to help retrieve
044 * those config param values as well.</p>
045 *
046 * <p>NOTE: The full list of supported parameters can be found in the source for this class.</p>
047 *
048 * @author Kuali Rice Team (rice.collab@kuali.org)
049 */
050public enum HttpClientParams {
051
052    //
053    // from org.apache.commons.httpclient.params.HttpMethodParams:
054    //
055
056    USE_EXPECT_CONTINUE("http.protocol.expect-continue", Boolean.class),
057    HTTP_CONTENT_CHARSET("http.protocol.content-charset"),
058    COOKIE_POLICY("http.protocol.cookie-policy"),
059
060    //
061    // from org.apache.commons.httpclient.params.HttpConnectionParams:
062    //
063
064    SO_TIMEOUT("http.socket.timeout", Integer.class),
065    TCP_NODELAY("http.tcp.nodelay", Boolean.class),
066    SO_SNDBUF("http.socket.sendbuffer", Integer.class),
067    SO_RCVBUF("http.socket.receivebuffer", Integer.class),
068    SO_LINGER("http.socket.linger", Integer.class),
069    CONNECTION_TIMEOUT("http.connection.timeout", Integer.class),
070    STALE_CONNECTION_CHECK("http.connection.stalecheck", Boolean.class),
071
072    //
073    // from org.apache.commons.httpclient.params.HttpConnectionManagerParams:
074    //
075
076    MAX_TOTAL_CONNECTIONS("http.connection-manager.max-total", Integer.class),
077
078    //
079    // from org.apache.commons.httpclient.params.HttpClientParams:
080    //
081
082    CONNECTION_MANAGER_TIMEOUT("http.connection-manager.timeout", Integer.class),
083    REJECT_RELATIVE_REDIRECT("http.protocol.reject-relative-redirect", Boolean.class),
084    MAX_REDIRECTS("http.protocol.max-redirects", Integer.class),
085    ALLOW_CIRCULAR_REDIRECTS("http.protocol.allow-circular-redirects", Boolean.class);
086
087    private String paramName;
088    private Class paramValueClass;
089
090    private static final Set<String> supportedParamNames = new HashSet<String>();
091
092    private HttpClientParams(String paramName, Class paramValueClass) {
093        this.paramName = paramName;
094        this.paramValueClass = paramValueClass;
095    }
096
097    private HttpClientParams(String paramName) {
098        this(paramName, String.class);
099    }
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}