Search in sources :

Example 1 with WebProxy

use of microsoft.exchange.webservices.data.core.WebProxy 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;
}
Also used : IAuthenticationResult(com.microsoft.aad.msal4j.IAuthenticationResult) ClientCredentialFactory(com.microsoft.aad.msal4j.ClientCredentialFactory) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) ClientCredentialParameters(com.microsoft.aad.msal4j.ClientCredentialParameters) WebProxy(microsoft.exchange.webservices.data.core.WebProxy) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ServiceVersionException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceResponseException(microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) WebProxyCredentials(microsoft.exchange.webservices.data.credential.WebProxyCredentials) ImpersonatedUserId(microsoft.exchange.webservices.data.misc.ImpersonatedUserId) ExchangeCredentials(microsoft.exchange.webservices.data.credential.ExchangeCredentials) WebCredentials(microsoft.exchange.webservices.data.credential.WebCredentials)

Aggregations

ClientCredentialFactory (com.microsoft.aad.msal4j.ClientCredentialFactory)1 ClientCredentialParameters (com.microsoft.aad.msal4j.ClientCredentialParameters)1 IAuthenticationResult (com.microsoft.aad.msal4j.IAuthenticationResult)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ExchangeService (microsoft.exchange.webservices.data.core.ExchangeService)1 WebProxy (microsoft.exchange.webservices.data.core.WebProxy)1 ServiceLocalException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException)1 ServiceVersionException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException)1 ServiceResponseException (microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException)1 ExchangeCredentials (microsoft.exchange.webservices.data.credential.ExchangeCredentials)1 WebCredentials (microsoft.exchange.webservices.data.credential.WebCredentials)1 WebProxyCredentials (microsoft.exchange.webservices.data.credential.WebProxyCredentials)1 ImpersonatedUserId (microsoft.exchange.webservices.data.misc.ImpersonatedUserId)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 CredentialFactory (nl.nn.adapterframework.util.CredentialFactory)1