khadjolees
khadjolees

Reputation: 11

Need help replacing this method since CoreConnectionPNames is deprecated (org.apache.http.params)

I am using this method to set CONNECTION_TIMEOUT and SO_TIMEOUT

public void setConfig(ContentType contentType, Integer timeout) {
            setConfig(SerenityRest.config()
                    .sslConfig(new SSLConfig().allowAllHostnames().relaxedHTTPSValidation())
                    .httpClient(HttpClientConfig.httpClientConfig()
                    .setParam(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout)
                    .setParam(CoreConnectionPNames.SO_TIMEOUT, timeout)))
                    .addHeader(HttpHeaders.CONTENT_TYPE, contentType.toString());
        }

Upvotes: 1

Views: 593

Answers (2)

Moto Mane
Moto Mane

Reputation: 1

Since 4.0 the org.apache.http.params.CoreConnectionPNames is deprecated and and the class has been removed, so you can't use the built-in constants anymore. if you have(4.3) use configuration classes provided 'org.apache.http.config' and 'org.apache.http.client.config'

You can re-write your code as given below:

public void setConfig(ContentType contentType, Integer timeout) {
        RequestConfig requestConfig = RequestConfig.custom()
        .setSocketTimeout(timeout)
        .setConnectTimeout(timeout)
        .setConnectionRequestTimeout(timeout)
        .build();

        HttpClientConfig httpClientFactory = HttpClientConfig.httpClientConfig()
                    .httpClientFactory(() -> HttpClientBuilder.create()
                            .setDefaultRequestConfig(requestConfig)
                            .build());

        setConfig(SerenityRest.config()
                    .sslConfig(new SSLConfig().allowAllHostnames().relaxedHTTPSValidation())
                    .httpClient(httpClientFactory)
                    .addHeader(HttpHeaders.CONTENT_TYPE, contentType.toString());
        }

Please accept the answer if it works for you. Thanks

Upvotes: 0

JoshDM
JoshDM

Reputation: 5072

org.apache.http.params.CoreConnectionPNames is deprecated and the class was removed. This prevents you from using the pre-made constants.

According to Android Developer Reference for CoreConnectionPNames:

  • The String value for SO_TIMEOUT was "http.socket.timeout".
    • according to the JavaDocs, replacement guidance references java.net.SocketOptions.SO_TIMEOUT, but this is an int so this may not be proper guidance
  • The String value for CONNECTION_TIMEOUT was "http.connection.timeout".
    • there is no replacement guidance

Substituting these for the constants or creating your own constants can replace the original constants; however there is no guarantee these settings will be recognized going forward. This SO answer should provide guidance on how to proceed forward modernizing the timeout.

Broad guidance is to swap to applying org.apache.http.client.config.RequestConfig.

RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();

Upvotes: 0

Related Questions