use of microsoft.exchange.webservices.data.misc.ImpersonatedUserId in project iaf by ibissource.
the class ExchangeFileSystem method createConnection.
@Override
protected ExchangeService createConnection() throws FileSystemException {
ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
if (client != null) {
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(SCOPE)).build();
CompletableFuture<IAuthenticationResult> future = client.acquireToken(clientCredentialParam);
try {
String token = future.get().accessToken();
// use OAuth Bearer token authentication
exchangeService.getHttpHeaders().put("Authorization", "Bearer " + token);
} catch (Exception e) {
throw new FileSystemException("Could not generate access token!", e);
}
exchangeService.setImpersonatedUserId(new ImpersonatedUserId(ConnectingIdType.SmtpAddress, getMailAddress()));
exchangeService.getHttpHeaders().put("X-AnchorMailbox", getMailAddress());
} else {
CredentialFactory cf = getCredentials();
// use deprecated Basic Authentication. Support will end 2021-Q3!
log.warn("Using deprecated Basic Authentication method for authentication to Exchange Web Services");
ExchangeCredentials credentials = new WebCredentials(cf.getUsername(), cf.getPassword());
exchangeService.setCredentials(credentials);
}
if (StringUtils.isNotEmpty(getProxyHost()) && (StringUtils.isNotEmpty(getProxyAuthAlias()) || StringUtils.isNotEmpty(getProxyUsername()) || StringUtils.isNotEmpty(getProxyPassword()))) {
CredentialFactory proxyCf = new CredentialFactory(getProxyAuthAlias(), getProxyUsername(), getProxyPassword());
WebProxyCredentials webProxyCredentials = new WebProxyCredentials(proxyCf.getUsername(), proxyCf.getPassword(), getProxyDomain());
WebProxy webProxy = new WebProxy(getProxyHost(), getProxyPort(), webProxyCredentials);
exchangeService.setWebProxy(webProxy);
}
RedirectionUrlCallback redirectionUrlCallback = new RedirectionUrlCallback() {
@Override
public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) {
if (isValidateAllRedirectUrls()) {
log.debug("validated redirection url [" + redirectionUrl + "]");
return true;
}
log.debug("did not validate redirection url [" + redirectionUrl + "]");
return super.autodiscoverRedirectionUrlValidationCallback(redirectionUrl);
}
};
if (StringUtils.isEmpty(getUrl())) {
log.debug("performing autodiscovery for [" + getMailAddress() + "]");
try {
exchangeService.autodiscoverUrl(getMailAddress(), redirectionUrlCallback);
// TODO call setUrl() here to avoid repeated autodiscovery
} catch (Exception e) {
throw new FileSystemException("cannot autodiscover for [" + getMailAddress() + "]", e);
}
} else {
try {
exchangeService.setUrl(new URI(getUrl()));
} catch (URISyntaxException e) {
throw new FileSystemException("cannot set URL [" + getUrl() + "]", e);
}
}
log.debug("using url [" + exchangeService.getUrl() + "]");
return exchangeService;
}
Aggregations