Search in sources :

Example 11 with Builder

use of org.apache.http.client.config.RequestConfig.Builder in project iaf by ibissource.

the class HttpSenderBase method configure.

@Override
public void configure() throws ConfigurationException {
    super.configure();
    /**
     * TODO find out if this really breaks proxy authentication or not.
     */
    // httpClientBuilder.disableAuthCaching();
    httpClientBuilder.disableAutomaticRetries();
    Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectTimeout(getTimeout());
    requestConfigBuilder.setConnectionRequestTimeout(getTimeout());
    requestConfigBuilder.setSocketTimeout(getTimeout());
    if (paramList != null) {
        paramList.configure();
        if (StringUtils.isNotEmpty(getUrlParam())) {
            urlParameter = paramList.findParameter(getUrlParam());
            if (urlParameter != null)
                addParameterToSkip(urlParameter.getName());
        }
        // Add all HeaderParams to paramIgnoreList
        StringTokenizer st = new StringTokenizer(getHeadersParams(), ",");
        while (st.hasMoreElements()) {
            addParameterToSkip(st.nextToken());
        }
    }
    if (StringUtils.isNotEmpty(getContentType())) {
        fullContentType = ContentType.parse(getContentType());
        if (fullContentType != null && fullContentType.getCharset() == null) {
            fullContentType = fullContentType.withCharset(getCharSet());
        }
    }
    if (getMaxConnections() <= 0) {
        throw new ConfigurationException(getLogPrefix() + "maxConnections is set to [" + getMaxConnections() + "], which is not enough for adequate operation");
    }
    try {
        if (urlParameter == null) {
            if (StringUtils.isEmpty(getUrl())) {
                throw new ConfigurationException(getLogPrefix() + "url must be specified, either as attribute, or as parameter");
            }
            staticUri = getURI(getUrl());
        }
    } catch (URISyntaxException e) {
        throw new ConfigurationException(getLogPrefix() + "cannot interpret url [" + getUrl() + "]", e);
    }
    AuthSSLContextFactory.verifyKeystoreConfiguration(this, this);
    if (StringUtils.isNotEmpty(getAuthAlias()) || StringUtils.isNotEmpty(getUsername())) {
        credentials = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
    } else {
        credentials = new CredentialFactory(getClientAuthAlias(), getClientId(), getClientSecret());
    }
    if (StringUtils.isNotEmpty(getTokenEndpoint()) && StringUtils.isEmpty(getClientAuthAlias()) && StringUtils.isEmpty(getClientId())) {
        throw new ConfigurationException("To obtain accessToken at tokenEndpoint [" + getTokenEndpoint() + "] a clientAuthAlias or ClientId and ClientSecret must be specified");
    }
    HttpHost proxy = null;
    CredentialFactory pcf = null;
    if (StringUtils.isNotEmpty(getProxyHost())) {
        proxy = new HttpHost(getProxyHost(), getProxyPort());
        pcf = new CredentialFactory(getProxyAuthAlias(), getProxyUsername(), getProxyPassword());
        requestConfigBuilder.setProxy(proxy);
        httpClientBuilder.setProxy(proxy);
    }
    setupAuthentication(credentials, pcf, proxy, requestConfigBuilder);
    if (StringUtils.isNotEmpty(getStyleSheetName())) {
        try {
            Resource stylesheet = Resource.getResource(this, getStyleSheetName());
            if (stylesheet == null) {
                throw new ConfigurationException(getLogPrefix() + "cannot find stylesheet [" + getStyleSheetName() + "]");
            }
            transformerPool = TransformerPool.getInstance(stylesheet);
        } catch (IOException e) {
            throw new ConfigurationException(getLogPrefix() + "cannot retrieve [" + getStyleSheetName() + "]", e);
        } catch (TransformerConfigurationException te) {
            throw new ConfigurationException(getLogPrefix() + "got error creating transformer from file [" + getStyleSheetName() + "]", te);
        }
    }
    httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
    if (areCookiesDisabled()) {
        httpClientBuilder.disableCookieManagement();
    }
    // The redirect strategy used to only redirect GET, DELETE and HEAD.
    httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        protected boolean isRedirectable(String method) {
            return isFollowRedirects();
        }
    });
}
Also used : StringTokenizer(java.util.StringTokenizer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) HttpHost(org.apache.http.HttpHost) Builder(org.apache.http.client.config.RequestConfig.Builder) URIBuilder(org.apache.http.client.utils.URIBuilder) RegistryBuilder(org.apache.http.config.RegistryBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Resource(nl.nn.adapterframework.core.Resource) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

Builder (org.apache.http.client.config.RequestConfig.Builder)11 IOException (java.io.IOException)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 RegistryBuilder (org.apache.http.config.RegistryBuilder)4 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)4 URISyntaxException (java.net.URISyntaxException)3 HttpHost (org.apache.http.HttpHost)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 HttpContext (org.apache.http.protocol.HttpContext)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)1 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)1 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)1 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)1 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 BufferedOutputStream (java.io.BufferedOutputStream)1