/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils;
import java.net.URI;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.eclipse.core.net.proxy.IProxyChangeEvent;
import org.eclipse.core.net.proxy.IProxyChangeListener;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
public class DefaultHttpClient extends HttpClient implements IProxyChangeListener {
private URI _uri;
private IProxyService _proxyService;
public DefaultHttpClient(IProxyService proxyService, URI uri) {
super();
getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000);
getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10000);
StringBuffer osId = new StringBuffer();
osId.append(System.getProperty("os.name"));
osId.append(", ");
osId.append(System.getProperty("os.arch"));
osId.append(", ");
osId.append(System.getProperty("os.version"));
getParams().setParameter(HttpClientParams.USER_AGENT, "OpenWGA Developer Studio v" + Activator.getDefault().getBundle().getVersion() + " (" + osId + ")");
_uri = uri;
_proxyService = proxyService;
_proxyService.addProxyChangeListener(this);
updateProxySettings();
}
private void updateProxySettings() {
if (_proxyService != null && _proxyService.isProxiesEnabled()) {
IProxyData[] proxies = _proxyService.select(_uri);
if (proxies != null && proxies.length > 0) {
IProxyData proxy = proxies[0];
getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
if (proxy.isRequiresAuthentication()) {
Credentials credentials = new UsernamePasswordCredentials(proxy.getUserId(), proxy.getPassword());
getState().setProxyCredentials(AuthScope.ANY, credentials);
} else {
getState().setProxyCredentials(AuthScope.ANY, null);
}
} else {
getHostConfiguration().setProxyHost(null);
}
} else {
getHostConfiguration().setProxyHost(null);
}
}
public void proxyInfoChanged(IProxyChangeEvent event) {
updateProxySettings();
}
@Override
protected void finalize() throws Throwable {
if (_proxyService != null) {
_proxyService.removeProxyChangeListener(this);
}
super.finalize();
}
}