All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.enhancedmule.tools.anypoint.authentication.AuthenticationProvider Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha4
Show newest version
/*
 * Copyright (c) Aeontronix 2020
 */

package com.aeontronix.enhancedmule.tools.anypoint.authentication;

import com.aeontronix.commons.StringUtils;
import com.aeontronix.enhancedmule.tools.util.*;
import com.aeontronix.restclient.auth.AuthenticationHandler;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;

public abstract class AuthenticationProvider implements AuthenticationHandler {
    public abstract AnypointAccessToken getBearerToken(HttpHelper httpHelper) throws HttpException;

    public EMHttpClient createHttpClient() {
        return createHttpClient(null, null, null);
    }

    public EMHttpClient createHttpClient(HttpHost proxyHost, String proxyUsername, String proxyPassword) {
        HttpClientBuilder builder = HttpClients.custom().disableCookieManagement();
        if (proxyHost != null) {
            DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
            builder = builder.setRoutePlanner(routePlanner);
            if (StringUtils.isNotEmpty(proxyUsername) && StringUtils.isNotEmpty(proxyPassword)) {
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(new AuthScope(proxyHost), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
                builder = builder.setDefaultCredentialsProvider(credsProvider);
            }
        }
        return new EMHttpClientDefaultImpl(builder.build());
    }

    public abstract String filterSecret(String resStr);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy