/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.social.support;

import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.ClassUtils;

/**
 * Chooses a request factory. Picks a HttpComponentsClientRequestFactory factory if Apache HttpComponents HttpClient is in the classpath.
 * If not, falls back to SimpleClientHttpRequestFactory.
 * @author Craig Walls
 * @author Roy Clarkson
 */
public class ClientHttpRequestFactorySelector {
	
	public static ClientHttpRequestFactory getRequestFactory() {
		if (httpComponentsAvailable) {
			return new HttpComponentsClientHttpRequestFactory();
		} else {
			return new SimpleClientHttpRequestFactory();
		}
	}
	
	private static boolean httpComponentsAvailable = ClassUtils.isPresent("org.apache.http.client.HttpClient", ClientHttpRequestFactory.class.getClassLoader());

}

@@ -654,16 +654,243 @@
 pport;%0A%0A
+import java.net.InetSocketAddress;%0Aimport java.net.Proxy;%0Aimport java.util.Properties;%0A%0Aimport org.apache.http.HttpHost;%0Aimport org.apache.http.conn.params.ConnRoutePNames;%0Aimport org.apache.http.impl.client.DefaultHttpClient;%0A
 import o
@@ -1430,152 +1430,946 @@
 %7B%0A%09%09
-if (httpComponentsAvailable) %7B%0A%09%09%09return new HttpComponentsClientHttpRequestFactory();%0A%09%09%7D else %7B%0A%09%09%09return new SimpleClientHttpRequestFactory()
+Properties properties = System.getProperties();%0A%09%09String proxyHost = properties.getProperty(%22http.proxyHost%22);%0A%09%09int proxyPort = properties.containsKey(%22http.proxyPort%22) ? Integer.valueOf(properties.getProperty(%22http.proxyPort%22)) : 80;%0A%09%09%0A%09%09if (httpComponentsAvailable) %7B%0A%09%09%09HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();%0A%09%09%09if(proxyHost != null) %7B%0A%09%09%09%09DefaultHttpClient httpClient = new DefaultHttpClient();%0A%09%09%09%09HttpHost proxy = new HttpHost(proxyHost, proxyPort);%0A%09%09%09%09httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);%0A%09%09%09%09requestFactory.setHttpClient(httpClient);%0A%09%09%09%7D%09%09%09%0A%09%09%09return requestFactory;%0A%09%09%7D else %7B%0A%09%09%09SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();%0A%09%09%09if(proxyHost != null) %7B%0A%09%09%09%09requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));%0A%09%09%09%7D%0A%09%09%09return requestFactory
 ;%0A%09%09

Set proxy host/port on request factories from http.proxyHost and http.proxyPort system properties, if available