1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-12 18:52:21 -05:00

Add a checkConfig method to detect configuration errors at connect time

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@49 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-04-25 23:29:46 +00:00
parent aad97b08b4
commit 93bc82da15
4 changed files with 86 additions and 41 deletions

View File

@ -130,24 +130,75 @@ public class ExchangeSession {
dateFormatter = new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
}
/**
* Update http client configuration (proxy)
*
* @param httpClient current Http client
*/
protected void configureClient(HttpClient httpClient) {
String enableProxy = Settings.getProperty("davmail.enableProxy");
String proxyHost = null;
String proxyPort = null;
String proxyUser = null;
String proxyPassword = null;
if ("true".equals(enableProxy)) {
proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPassword = Settings.getProperty("davmail.proxyPassword");
}
// configure proxy
if (proxyHost != null && proxyHost.length() > 0) {
httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
if (proxyUser != null && proxyUser.length() > 0) {
// detect ntlm authentication (windows domain name in user name)
int backslashindex = proxyUser.indexOf("\\");
if (backslashindex > 0) {
httpClient.getState().setProxyCredentials(null, proxyHost,
new NTCredentials(proxyUser.substring(backslashindex + 1),
proxyPassword, null,
proxyUser.substring(0, backslashindex)));
} else {
httpClient.getState().setProxyCredentials(null, proxyHost,
new UsernamePasswordCredentials(proxyUser, proxyPassword));
}
}
}
}
public void checkConfig() throws IOException {
try {
String url = Settings.getProperty("davmail.url");
// create an HttpClient instance
HttpClient httpClient = new HttpClient();
configureClient(httpClient);
// get webmail root url (will follow redirects)
HttpMethod testMethod = new GetMethod(url);
int status = httpClient.executeMethod(testMethod);
testMethod.releaseConnection();
logger.debug("Test configuration status: " + status);
if (status != HttpStatus.SC_OK) {
throw new IOException("Unable to connect to OWA at " + url + ", status code " +
status + ", check configuration");
}
} catch (Exception exc) {
logger.error("DavMail configuration exception: \n"+exc.getMessage(), exc);
throw new IOException("DavMail configuration exception: \n"+exc.getMessage(), exc);
}
}
public void login(String userName, String password) throws Exception {
try {
String url = Settings.getProperty("davmail.url");
String enableProxy = Settings.getProperty("davmail.enableProxy");
String proxyHost = null;
String proxyPort = null;
String proxyUser = null;
String proxyPassword = null;
if ("true".equals(enableProxy)) {
proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPassword = Settings.getProperty("davmail.proxyPassword");
}
// get proxy configuration from setttings properties
URL urlObject = new URL(url);
// webdavresource is unable to create the correct url type
HttpURL httpURL;
@ -176,23 +227,7 @@ public class ExchangeSession {
// do not send basic auth automatically
httpClient.getState().setAuthenticationPreemptive(false);
// configure proxy
if (proxyHost != null && proxyHost.length() > 0) {
httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
if (proxyUser != null && proxyUser.length() > 0) {
// detect ntlm authentication (windows domain name in user name)
int backslashindex = proxyUser.indexOf("\\");
if (backslashindex > 0) {
httpClient.getState().setProxyCredentials(null, proxyHost,
new NTCredentials(proxyUser.substring(backslashindex + 1),
proxyPassword, null,
proxyUser.substring(0, backslashindex)));
} else {
httpClient.getState().setProxyCredentials(null, proxyHost,
new UsernamePasswordCredentials(proxyUser, proxyPassword));
}
}
}
configureClient(httpClient);
// get webmail root url (will follow redirects)
// providing credentials

View File

@ -4,10 +4,11 @@ import davmail.AbstractConnection;
import davmail.DavGatewayTray;
import davmail.exchange.ExchangeSession;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
/**
* Dav Gateway pop connection implementation
@ -53,6 +54,8 @@ public class PopConnection extends AbstractConnection {
StringTokenizer tokens;
try {
session = new ExchangeSession();
session.checkConfig();
sendOK("DavMail POP ready at " + new Date());
for (; ;) {
@ -93,16 +96,10 @@ public class PopConnection extends AbstractConnection {
} else {
password = tokens.nextToken();
try {
session = new ExchangeSession();
session.login(userName, password);
messages = session.getAllMessages();
sendOK("PASS");
state = AUTHENTICATED;
} catch (UnknownHostException e) {
DavGatewayTray.error("Connection failed", e);
sendERR("Connection failed : " + e + " " + e.getMessage());
// close connection
break;
} catch (Exception e) {
DavGatewayTray.error("Authentication failed", e);
sendERR("authentication failed : " + e + " " + e.getMessage());
@ -185,7 +182,12 @@ public class PopConnection extends AbstractConnection {
os.flush();
}
} catch (IOException e) {
DavGatewayTray.error("Exception handling client", e);
DavGatewayTray.error(e.getMessage());
try {
sendERR(e.getMessage());
} catch (IOException e2) {
DavGatewayTray.debug("Exception sending error to client", e2);
}
} finally {
try {
client.close();

View File

@ -31,6 +31,8 @@ public class SmtpConnection extends AbstractConnection {
try {
session = new ExchangeSession();
session.checkConfig();
sendClient("220 DavMail SMTP ready at " + new Date());
for (; ;) {
line = readClient();
@ -118,7 +120,12 @@ public class SmtpConnection extends AbstractConnection {
os.flush();
}
} catch (IOException e) {
DavGatewayTray.error("Exception handling client",e);
DavGatewayTray.error(e.getMessage());
try {
sendClient("500 "+e.getMessage());
} catch (IOException e2) {
DavGatewayTray.debug("Exception sending error to client", e2);
}
} finally {
try {
client.close();

View File

@ -14,6 +14,7 @@ public class TestExchangeSession {
ExchangeSession session = new ExchangeSession();
// test auth
try {
session.checkConfig();
session.login(argv[1], argv[2]);
ExchangeSession.Folder folder = session.selectFolder(argv[3]);