mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 03:02:22 -05:00
EWS: improve autodiscover implementation
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1412 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
c8f44069bf
commit
dc9659fe3d
@ -148,8 +148,17 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
}
|
||||
|
||||
protected static class AutoDiscoverMethod extends PostMethod {
|
||||
AutoDiscoverMethod(String autodiscoverHost, String userEmail) {
|
||||
super("https://"+autodiscoverHost+"/autodiscover/autodiscover.xml");
|
||||
setAutoDiscoverRequestEntity(userEmail);
|
||||
}
|
||||
|
||||
AutoDiscoverMethod(String userEmail) {
|
||||
super("/autodiscover/autodiscover.xml");
|
||||
setAutoDiscoverRequestEntity(userEmail);
|
||||
}
|
||||
|
||||
void setAutoDiscoverRequestEntity(String userEmail) {
|
||||
String body = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">" +
|
||||
"<Request>" +
|
||||
"<EMailAddress>" + userEmail + "</EMailAddress>" +
|
||||
@ -197,19 +206,40 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
|
||||
protected String getEwsUrlFromAutoDiscover() throws DavMailAuthenticationException {
|
||||
String ewsUrl;
|
||||
AutoDiscoverMethod autoDiscoverMethod = new AutoDiscoverMethod(email);
|
||||
try {
|
||||
ewsUrl = getEwsUrlFromAutoDiscover(null);
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
ewsUrl = getEwsUrlFromAutoDiscover("autodiscover." + email.substring(email.indexOf('@') + 1));
|
||||
} catch (IOException e2) {
|
||||
LOGGER.error(e2.getMessage());
|
||||
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
||||
}
|
||||
}
|
||||
return ewsUrl;
|
||||
}
|
||||
|
||||
protected String getEwsUrlFromAutoDiscover(String autodiscoverHostname) throws IOException {
|
||||
String ewsUrl;
|
||||
AutoDiscoverMethod autoDiscoverMethod;
|
||||
if (autodiscoverHostname != null) {
|
||||
autoDiscoverMethod = new AutoDiscoverMethod(autodiscoverHostname, email);
|
||||
} else {
|
||||
autoDiscoverMethod = new AutoDiscoverMethod(email);
|
||||
}
|
||||
try {
|
||||
int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, autoDiscoverMethod);
|
||||
if (status != HttpStatus.SC_OK) {
|
||||
throw DavGatewayHttpClientFacade.buildHttpException(autoDiscoverMethod);
|
||||
}
|
||||
ewsUrl = autoDiscoverMethod.ewsUrl;
|
||||
|
||||
// update host name
|
||||
DavGatewayHttpClientFacade.setClientHost(httpClient, ewsUrl);
|
||||
|
||||
if (ewsUrl == null) {
|
||||
throw new IOException("Ews url not found");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
throw new DavMailAuthenticationException("EXCEPTION_EWS_NOT_AVAILABLE");
|
||||
} finally {
|
||||
autoDiscoverMethod.releaseConnection();
|
||||
}
|
||||
|
@ -125,13 +125,13 @@ public final class DavGatewayHttpClientFacade {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update http client configuration (proxy)
|
||||
* Set http client current host configuration.
|
||||
*
|
||||
* @param httpClient current Http client
|
||||
* @param url target url
|
||||
* @throws DavMailException on error
|
||||
*/
|
||||
public static void configureClient(HttpClient httpClient, String url) throws DavMailException {
|
||||
public static void setClientHost(HttpClient httpClient, String url) throws DavMailException {
|
||||
try {
|
||||
HostConfiguration hostConfig = httpClient.getHostConfiguration();
|
||||
URI httpURI = new URI(url, true);
|
||||
@ -139,6 +139,17 @@ public final class DavGatewayHttpClientFacade {
|
||||
} catch (URIException e) {
|
||||
throw new DavMailException("LOG_INVALID_URL", url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update http client configuration (proxy)
|
||||
*
|
||||
* @param httpClient current Http client
|
||||
* @param url target url
|
||||
* @throws DavMailException on error
|
||||
*/
|
||||
public static void configureClient(HttpClient httpClient, String url) throws DavMailException {
|
||||
setClientHost(httpClient, url);
|
||||
|
||||
synchronized (LOCK) {
|
||||
httpClient.setHttpConnectionManager(multiThreadedHttpConnectionManager);
|
||||
|
Loading…
Reference in New Issue
Block a user