1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-14 03:32:22 -05:00

Add a new setting to retrieve proxies from system configuration

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@971 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-03-24 09:59:23 +00:00
parent 266632480d
commit 577ea1ecdb
9 changed files with 90 additions and 31 deletions

View File

@ -196,9 +196,9 @@ public final class DavGateway {
String version = null;
if (!Settings.getBooleanProperty("davmail.disableUpdateCheck")) {
BufferedReader versionReader = null;
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
GetMethod getMethod = new GetMethod(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
try {
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
int status = httpClient.executeMethod(getMethod);
if (status == HttpStatus.SC_OK) {
versionReader = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream()));

View File

@ -139,6 +139,7 @@ public final class Settings {
SETTINGS.put("davmail.caldavPastDelay", "90");
SETTINGS.put("davmail.allowRemote", Boolean.FALSE.toString());
SETTINGS.put("davmail.bindAddress", "");
SETTINGS.put("davmail.useSystemProxies", Boolean.TRUE.toString());
SETTINGS.put("davmail.enableProxy", Boolean.FALSE.toString());
SETTINGS.put("davmail.proxyHost", "");
SETTINGS.put("davmail.proxyPort", "");

View File

@ -1585,7 +1585,7 @@ public class ExchangeSession {
method.setRequestHeader("Translate", "f");
BufferedReader reader = null;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, false);
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
OutputStreamWriter isoWriter = new OutputStreamWriter(os);

View File

@ -23,6 +23,7 @@ import davmail.Settings;
import davmail.exception.*;
import davmail.ui.tray.DavGatewayTray;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
@ -38,6 +39,7 @@ import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -70,14 +72,16 @@ public final class DavGatewayHttpClientFacade {
/**
* Create a configured HttpClient instance.
*
* @param url target url
* @return httpClient
* @throws DavMailException on error
*/
public static HttpClient getInstance() {
public static HttpClient getInstance(String url) throws DavMailException {
// create an HttpClient instance
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
configureClient(httpClient);
configureClient(httpClient, url);
return httpClient;
}
@ -94,17 +98,10 @@ public final class DavGatewayHttpClientFacade {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, IE_USER_AGENT);
httpClient.getParams().setParameter(HttpClientParams.MAX_REDIRECTS, MAX_REDIRECTS);
HostConfiguration hostConfig = httpClient.getHostConfiguration();
try {
URI httpURI = new URI(url, true);
hostConfig.setHost(httpURI);
// some Exchange servers redirect to a different host for freebusy, use wide auth scope
AuthScope authScope = new AuthScope(null, -1);
httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "", ""));
} catch (URIException e) {
throw new DavMailException("LOG_INVALID_URL", url);
}
configureClient(httpClient);
configureClient(httpClient, url);
// some Exchange servers redirect to a different host for freebusy, use wide auth scope
AuthScope authScope = new AuthScope(null, -1);
httpClient.getState().setCredentials(authScope, new NTCredentials(userName, password, "", ""));
return httpClient;
}
@ -112,8 +109,18 @@ public final class DavGatewayHttpClientFacade {
* Update http client configuration (proxy)
*
* @param httpClient current Http client
* @param url target url
* @throws DavMailException on error
*/
public static void configureClient(HttpClient httpClient) {
public static void configureClient(HttpClient httpClient, String url) throws DavMailException {
try {
HostConfiguration hostConfig = httpClient.getHostConfiguration();
URI httpURI = new URI(url, true);
hostConfig.setHost(httpURI);
} catch (URIException e) {
throw new DavMailException("LOG_INVALID_URL", url);
}
synchronized (LOCK) {
httpClient.setHttpConnectionManager(multiThreadedHttpConnectionManager);
}
@ -127,12 +134,32 @@ public final class DavGatewayHttpClientFacade {
}
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
boolean useSystemProxies = Settings.getBooleanProperty("davmail.useSystemProxies");
String proxyHost = null;
int proxyPort = 0;
String proxyUser = null;
String proxyPassword = null;
if (enableProxy) {
if (useSystemProxies) {
// get proxy for url from system settings
System.setProperty("java.net.useSystemProxies", "true");
try {
List<Proxy> proxyList = ProxySelector.getDefault().select(new java.net.URI(url));
if (!proxyList.isEmpty() && proxyList.get(0).address() != null) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) proxyList.get(0).address();
proxyHost = inetSocketAddress.getHostName();
proxyPort = inetSocketAddress.getPort();
// we may still need authentication credentials
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPassword = Settings.getProperty("davmail.proxyPassword");
}
} catch (URISyntaxException e) {
throw new DavMailException("LOG_INVALID_URL", url);
}
} else if (enableProxy) {
proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getIntProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser");
@ -172,7 +199,7 @@ public final class DavGatewayHttpClientFacade {
*/
public static int getHttpStatus(String url) throws IOException {
int status = 0;
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance();
HttpClient httpClient = DavGatewayHttpClientFacade.getInstance(url);
HttpMethod testMethod = new GetMethod(url);
testMethod.setDoAuthentication(false);
try {
@ -387,6 +414,13 @@ public final class DavGatewayHttpClientFacade {
needNTLM = true;
}
/**
* Test method header for supported authentication mode,
* return true if Basic authentication is not available
*
* @param getMethod http method
* @return true if only NTLM is enabled
*/
public static boolean acceptsNTLMOnly(HttpMethod getMethod) {
Header authenticateHeader = null;
if (getMethod.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
@ -416,14 +450,13 @@ public final class DavGatewayHttpClientFacade {
/**
* Execute test method from checkConfig, with proxy credentials, but without Exchange credentials.
*
* @param httpClient Http client instance
* @param method Http method
* @param followRedirects Follow redirects flag
* @param httpClient Http client instance
* @param method Http method
* @return Http status
* @throws IOException on error
*/
public static int executeTestMethod(HttpClient httpClient, GetMethod method) throws IOException {
// do not follow redirects in expired sessions
// do not follow redirects in expired sessions
method.setFollowRedirects(false);
int status = httpClient.executeMethod(method);
if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED
@ -436,7 +469,7 @@ public final class DavGatewayHttpClientFacade {
return status;
}
/**
* Execute Get method, do not follow redirects.
*

View File

@ -53,6 +53,7 @@ public class SettingsFrame extends JFrame {
protected JTextField sentKeepDelayField;
protected JTextField caldavPastDelayField;
JCheckBox useSystemProxiesField;
JCheckBox enableProxyField;
JTextField httpProxyField;
JTextField httpProxyPortField;
@ -209,10 +210,13 @@ public class SettingsFrame extends JFrame {
}
protected JPanel getProxyPanel() {
JPanel proxyPanel = new JPanel(new GridLayout(5, 2));
JPanel proxyPanel = new JPanel(new GridLayout(6, 2));
proxyPanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_PROXY")));
boolean useSystemProxies = Settings.getBooleanProperty("davmail.useSystemProxies");
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
useSystemProxiesField = new JCheckBox();
useSystemProxiesField.setSelected(useSystemProxies);
enableProxyField = new JCheckBox();
enableProxyField.setSelected(enableProxy);
httpProxyField = new JTextField(Settings.getProperty("davmail.proxyHost"), 15);
@ -220,11 +224,23 @@ public class SettingsFrame extends JFrame {
httpProxyUserField = new JTextField(Settings.getProperty("davmail.proxyUser"), 10);
httpProxyPasswordField = new JPasswordField(Settings.getProperty("davmail.proxyPassword"), 10);
enableProxyField.setEnabled(!useSystemProxies);
httpProxyField.setEnabled(enableProxy);
httpProxyPortField.setEnabled(enableProxy);
httpProxyUserField.setEnabled(enableProxy);
httpProxyPasswordField.setEnabled(enableProxy);
httpProxyUserField.setEnabled(enableProxy || useSystemProxies);
httpProxyPasswordField.setEnabled(enableProxy || useSystemProxies);
useSystemProxiesField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
boolean newUseSystemProxies = useSystemProxiesField.isSelected();
boolean newEnableProxy = enableProxyField.isSelected();
enableProxyField.setEnabled(!newUseSystemProxies);
httpProxyField.setEnabled(!newUseSystemProxies && newEnableProxy);
httpProxyPortField.setEnabled(!newUseSystemProxies && newEnableProxy);
httpProxyUserField.setEnabled(newUseSystemProxies || newEnableProxy);
httpProxyPasswordField.setEnabled(newUseSystemProxies || newEnableProxy);
}
});
enableProxyField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
boolean newEnableProxy = enableProxyField.isSelected();
@ -235,6 +251,7 @@ public class SettingsFrame extends JFrame {
}
});
addSettingComponent(proxyPanel, BundleMessage.format("UI_USE_SYSTEM_PROXIES"), useSystemProxiesField);
addSettingComponent(proxyPanel, BundleMessage.format("UI_ENABLE_PROXY"), enableProxyField);
addSettingComponent(proxyPanel, BundleMessage.format("UI_PROXY_SERVER"), httpProxyField);
addSettingComponent(proxyPanel, BundleMessage.format("UI_PROXY_PORT"), httpProxyPortField);
@ -429,12 +446,15 @@ public class SettingsFrame extends JFrame {
keepDelayField.setText(Settings.getProperty("davmail.keepDelay"));
sentKeepDelayField.setText(Settings.getProperty("davmail.sentKeepDelay"));
caldavPastDelayField.setText(Settings.getProperty("davmail.caldavPastDelay"));
boolean useSystemProxies = Settings.getBooleanProperty("davmail.useSystemProxies");
useSystemProxiesField.setSelected(useSystemProxies);
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
enableProxyField.setSelected(enableProxy);
httpProxyField.setEnabled(enableProxy);
httpProxyPortField.setEnabled(enableProxy);
httpProxyUserField.setEnabled(enableProxy);
httpProxyPasswordField.setEnabled(enableProxy);
enableProxyField.setEnabled(!useSystemProxies);
httpProxyField.setEnabled(!useSystemProxies && enableProxy);
httpProxyPortField.setEnabled(!useSystemProxies && enableProxy);
httpProxyUserField.setEnabled(useSystemProxies ||enableProxy);
httpProxyPasswordField.setEnabled(useSystemProxies || enableProxy);
httpProxyField.setText(Settings.getProperty("davmail.proxyHost"));
httpProxyPortField.setText(Settings.getProperty("davmail.proxyPort"));
httpProxyUserField.setText(Settings.getProperty("davmail.proxyUser"));
@ -542,6 +562,7 @@ public class SettingsFrame extends JFrame {
Settings.setProperty("davmail.keepDelay", keepDelayField.getText());
Settings.setProperty("davmail.sentKeepDelay", sentKeepDelayField.getText());
Settings.setProperty("davmail.caldavPastDelay", caldavPastDelayField.getText());
Settings.setProperty("davmail.useSystemProxies", String.valueOf(useSystemProxiesField.isSelected()));
Settings.setProperty("davmail.enableProxy", String.valueOf(enableProxyField.isSelected()));
Settings.setProperty("davmail.proxyHost", httpProxyField.getText());
Settings.setProperty("davmail.proxyPort", httpProxyPortField.getText());

View File

@ -248,3 +248,4 @@ UI_FORCE_ACTIVESYNC_UPDATE=Force ActiveSync update:
UI_FORCE_ACTIVESYNC_UPDATE_HELP=Force update of Caldav events for ActiveSync connected devices
UI_DEFAULT_DOMAIN=Default domain:
UI_DEFAULT_DOMAIN_HELP=Default windows domain name
UI_USE_SYSTEM_PROXIES=Use system proxy settings :

View File

@ -247,4 +247,5 @@ UI_FORCE_ACTIVESYNC_UPDATE_HELP=Forcer la mise
UI_DEFAULT_DOMAIN=Domaine par défaut :
UI_DEFAULT_DOMAIN_HELP=Nom du domaine windows par défaut
EXCEPTION_UNSUPPORTED_PARAMETER=Paramètre non supporté : {0}
EXCEPTION_INVALID_PARAMETER=Paramètre invalide : {0}
EXCEPTION_INVALID_PARAMETER=Paramètre invalide : {0}
UI_USE_SYSTEM_PROXIES=Utiliser la configuration système :

View File

@ -38,6 +38,7 @@
davmail.keepDelay=30
davmail.sentKeepDelay=90
davmail.caldavPastDelay=90
davmail.useSystemProxies=false
davmail.enableProxy=false
davmail.proxyHost=
davmail.proxyPort=

View File

@ -5,6 +5,7 @@ davmail.keepDelay=30
davmail.sentKeepDelay=90
davmail.caldavPastDelay=90
davmail.useSystemProxies=false
davmail.enableProxy=false
davmail.proxyHost=
davmail.proxyPort=