Search in sources :

Example 1 with CIProxyConfiguration

use of com.hp.octane.integrations.dto.configuration.CIProxyConfiguration in project octane-gitlab-service by MicroFocus.

the class OctaneServices method getProxyConfiguration.

// @Override
// public File getAllowedOctaneStorage() {
// File sdkStorage = new File("sdk_storage");
// boolean available = sdkStorage.exists();
// if (!available) {
// available = sdkStorage.mkdirs();
// }
// return available ? sdkStorage : null;
// }
@Override
public CIProxyConfiguration getProxyConfiguration(URL targetUrl) {
    try {
        CIProxyConfiguration result = null;
        if (ProxyHelper.isProxyNeeded(applicationSettings, targetUrl)) {
            log.debug("proxy is required for host " + targetUrl);
            ConfigStructure config = applicationSettings.getConfig();
            String protocol = targetUrl.getProtocol();
            URL proxyUrl = new URL(config.getProxyField(protocol, "proxyUrl"));
            String proxyPassword = config.getProxyField(protocol, "proxyPassword");
            if (proxyPassword != null && proxyPassword.startsWith(PREFIX)) {
                try {
                    proxyPassword = PasswordEncryption.decrypt(proxyPassword.substring(PREFIX.length()));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            result = dtoFactory.newDTO(CIProxyConfiguration.class).setHost(proxyUrl.getHost()).setPort(proxyUrl.getPort()).setUsername(config.getProxyField(protocol, "proxyUser")).setPassword(proxyPassword);
        }
        return result;
    } catch (Exception e) {
        log.warn("Failed to return the proxy configuration, using null as default.", e);
        return null;
    }
}
Also used : CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) ConfigStructure(com.microfocus.octane.gitlab.model.ConfigStructure) URL(java.net.URL) PermissionException(com.hp.octane.integrations.exceptions.PermissionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 2 with CIProxyConfiguration

use of com.hp.octane.integrations.dto.configuration.CIProxyConfiguration in project octane-ci-java-sdk by MicroFocus.

the class GeneralRestClient method createHttpContext.

private HttpClientContext createHttpContext(String requestUrl, int requestTimeoutSec) {
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(new BasicCookieStore());
    // prepare request config
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD);
    // configure proxy if needed
    CIProxyConfiguration proxyConfiguration = CIPluginSDKUtils.getProxyConfiguration(requestUrl, null);
    if (proxyConfiguration != null) {
        HttpHost proxyHost = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort());
        if (proxyConfiguration.getUsername() != null && !proxyConfiguration.getUsername().isEmpty()) {
            AuthScope authScope = new AuthScope(proxyHost);
            Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            CredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
            credentialsProvider.setCredentials(authScope, credentials);
            context.setCredentialsProvider(credentialsProvider);
        }
        requestConfigBuilder.setProxy(proxyHost);
    }
    // set timeout if needed
    if (requestTimeoutSec > 0) {
        int timeoutMs = requestTimeoutSec * 1000;
        requestConfigBuilder.setConnectTimeout(timeoutMs).setConnectionRequestTimeout(timeoutMs).setSocketTimeout(timeoutMs);
    }
    context.setRequestConfig(requestConfigBuilder.build());
    authentication.onCreateContext(context);
    return context;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with CIProxyConfiguration

use of com.hp.octane.integrations.dto.configuration.CIProxyConfiguration in project octane-ci-java-sdk by MicroFocus.

the class OctaneRestClientImpl method createHttpContext.

private HttpClientContext createHttpContext(String requestUrl, int requestTimeoutSec, boolean isLoginRequest) {
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(new BasicCookieStore());
    // add security token if needed
    if (!isLoginRequest) {
        context.getCookieStore().addCookie(LWSSO_TOKEN);
    }
    // prepare request config
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD);
    // configure proxy if needed
    CIProxyConfiguration proxyConfiguration = CIPluginSDKUtils.getProxyConfiguration(requestUrl, configurer);
    if (proxyConfiguration != null) {
        logger.debug(configurer.octaneConfiguration.getLocationForLog() + "proxy will be used with the following setup: " + proxyConfiguration);
        HttpHost proxyHost = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort());
        if (proxyConfiguration.getUsername() != null && !proxyConfiguration.getUsername().isEmpty()) {
            AuthScope authScope = new AuthScope(proxyHost);
            Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            CredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
            credentialsProvider.setCredentials(authScope, credentials);
            context.setCredentialsProvider(credentialsProvider);
        }
        requestConfigBuilder.setProxy(proxyHost);
    }
    // set timeout if needed
    if (requestTimeoutSec > 0) {
        int timeoutMs = requestTimeoutSec * 1000;
        requestConfigBuilder.setConnectTimeout(timeoutMs).setConnectionRequestTimeout(timeoutMs).setSocketTimeout(timeoutMs);
    }
    context.setRequestConfig(requestConfigBuilder.build());
    return context;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 4 with CIProxyConfiguration

use of com.hp.octane.integrations.dto.configuration.CIProxyConfiguration in project octane-ci-java-sdk by MicroFocus.

the class FODConnector method initConnection.

public void initConnection(OctaneSDK.SDKServicesConfigurer configurer) {
    logger.debug("init FOD connector");
    try {
        CIProxyConfiguration proxyConfiguration = configurer.pluginServices.getProxyConfiguration(new URL(this.fodConfig.authURL));
        if (proxyConfiguration != null) {
            logger.warn("FOD connection needs proxy");
            HttpClientBuilder clientBuilder = HttpClients.custom();
            String proxyHost = proxyConfiguration.getHost();
            Integer proxyPortNumber = proxyConfiguration.getPort();
            clientBuilder.setProxy(new HttpHost(proxyHost, proxyPortNumber));
            httpClient = clientBuilder.build();
        } else {
            logger.warn("FOD connection does not need proxy");
            httpClient = HttpClients.createDefault();
        }
        getAccessToken();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}
Also used : CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URL(java.net.URL)

Example 5 with CIProxyConfiguration

use of com.hp.octane.integrations.dto.configuration.CIProxyConfiguration in project octane-ci-java-sdk by MicroFocus.

the class CIPluginSDKUtils method getProxyConfiguration.

public static CIProxyConfiguration getProxyConfiguration(String url, OctaneSDK.SDKServicesConfigurer configurer) {
    Function<URL, CIProxyConfiguration> proxySupplier;
    if (configurer != null) {
        proxySupplier = configurer.pluginServices::getProxyConfiguration;
    } else if (OctaneSDK.hasClients()) {
        proxySupplier = OctaneSDK.getClients().get(0).getRestService().getProxySupplier();
    } else {
        return null;
    }
    URL octaneUrl = CIPluginSDKUtils.parseURL(url);
    return proxySupplier.apply(octaneUrl);
}
Also used : CIProxyConfiguration(com.hp.octane.integrations.dto.configuration.CIProxyConfiguration) URL(java.net.URL)

Aggregations

CIProxyConfiguration (com.hp.octane.integrations.dto.configuration.CIProxyConfiguration)5 URL (java.net.URL)3 HttpHost (org.apache.http.HttpHost)3 AuthScope (org.apache.http.auth.AuthScope)2 Credentials (org.apache.http.auth.Credentials)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 CredentialsProvider (org.apache.http.client.CredentialsProvider)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)2 PermissionException (com.hp.octane.integrations.exceptions.PermissionException)1 ConfigStructure (com.microfocus.octane.gitlab.model.ConfigStructure)1 MalformedURLException (java.net.MalformedURLException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 SystemDefaultCredentialsProvider (org.apache.http.impl.client.SystemDefaultCredentialsProvider)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1