diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index 758c8346..400ab4b9 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -27,6 +27,7 @@ import org.telegram.telegrambots.api.objects.media.InputMedia; import org.telegram.telegrambots.exceptions.TelegramApiException; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.exceptions.TelegramApiValidationException; +import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.updateshandlers.DownloadFileCallback; import org.telegram.telegrambots.updateshandlers.SentCallback; @@ -63,7 +64,7 @@ public abstract class DefaultAbsSender extends AbsSender { this.exe = Executors.newFixedThreadPool(options.getMaxThreads()); this.options = options; - httpclient = createHttpClient(); + httpclient = TelegramHttpClientBuilder.build(options); requestConfig = options.getRequestConfig(); @@ -85,29 +86,6 @@ public abstract class DefaultAbsSender extends AbsSender { return options; } - protected CloseableHttpClient createHttpClient() { - CloseableHttpClient localClient = null; - - if (options.getCredentialsProvider() != null) { - localClient = HttpClientBuilder.create() - .setProxy(options.getHttpProxy()) - .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) - .setDefaultCredentialsProvider(options.getCredentialsProvider()) - .setSSLHostnameVerifier(new NoopHostnameVerifier()) - .setConnectionTimeToLive(70, TimeUnit.SECONDS) - .setMaxConnTotal(100) - .build(); - } else { - localClient = HttpClientBuilder.create() - .setSSLHostnameVerifier(new NoopHostnameVerifier()) - .setConnectionTimeToLive(70, TimeUnit.SECONDS) - .setMaxConnTotal(100) - .build(); - } - - return localClient; - } - // Send Requests public final java.io.File downloadFile(String filePath) throws TelegramApiException { diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 4550a903..25b99dfc 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -14,6 +14,7 @@ import org.telegram.telegrambots.ApiConstants; import org.telegram.telegrambots.ApiContext; import org.telegram.telegrambots.api.methods.updates.SetWebhook; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.generics.LongPollingBot; import java.io.IOException; @@ -36,7 +37,7 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements @Override public void clearWebhook() throws TelegramApiRequestException { - try (CloseableHttpClient httpclient = this.createHttpClient()) { + try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) { String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH; HttpGet httpGet = new HttpGet(url); httpGet.setConfig(getOptions().getRequestConfig()); diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java index 9c345134..9b2ff0ec 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramWebhookBot.java @@ -17,6 +17,7 @@ import org.telegram.telegrambots.ApiConstants; import org.telegram.telegrambots.ApiContext; import org.telegram.telegrambots.api.methods.updates.SetWebhook; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.generics.WebhookBot; import java.io.File; @@ -44,7 +45,7 @@ public abstract class TelegramWebhookBot extends DefaultAbsSender implements Web @Override public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException { - try (CloseableHttpClient httpclient = this.createHttpClient()) { + try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) { String requestUrl = getBaseUrl() + SetWebhook.PATH; HttpPost httppost = new HttpPost(requestUrl); diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/facilities/TelegramHttpClientBuilder.java b/telegrambots/src/main/java/org/telegram/telegrambots/facilities/TelegramHttpClientBuilder.java new file mode 100644 index 00000000..fb911c04 --- /dev/null +++ b/telegrambots/src/main/java/org/telegram/telegrambots/facilities/TelegramHttpClientBuilder.java @@ -0,0 +1,37 @@ +package org.telegram.telegrambots.facilities; + +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.telegram.telegrambots.bots.DefaultBotOptions; + +import java.util.concurrent.TimeUnit; + +/** + * Created by bvn13 on 17.04.2018. + */ +public class TelegramHttpClientBuilder { + + public static CloseableHttpClient build(DefaultBotOptions options) { + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() + .setSSLHostnameVerifier(new NoopHostnameVerifier()) + .setConnectionTimeToLive(70, TimeUnit.SECONDS) + .setMaxConnTotal(100); + + if (options.getHttpProxy() != null) { + + httpClientBuilder.setProxy(options.getHttpProxy()); + + if (options.getCredentialsProvider() != null) { + httpClientBuilder + .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) + .setDefaultCredentialsProvider(options.getCredentialsProvider()); + } + + } + + return httpClientBuilder.build(); + } + +} diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java index 190cf136..6e6b56b9 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java @@ -20,6 +20,7 @@ import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.generics.*; import org.telegram.telegrambots.logging.BotLogger; @@ -146,32 +147,9 @@ public class DefaultBotSession implements BotSession { this.lock = lock; } - protected CloseableHttpClient createHttpClient() { - CloseableHttpClient localClient = null; - - if (options.getCredentialsProvider() != null) { - localClient = HttpClientBuilder.create() - .setProxy(options.getHttpProxy()) - .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) - .setDefaultCredentialsProvider(options.getCredentialsProvider()) - .setSSLHostnameVerifier(new NoopHostnameVerifier()) - .setConnectionTimeToLive(70, TimeUnit.SECONDS) - .setMaxConnTotal(100) - .build(); - } else { - localClient = HttpClientBuilder.create() - .setSSLHostnameVerifier(new NoopHostnameVerifier()) - .setConnectionTimeToLive(70, TimeUnit.SECONDS) - .setMaxConnTotal(100) - .build(); - } - - return localClient; - } - @Override public synchronized void start() { - httpclient = createHttpClient(); + httpclient = TelegramHttpClientBuilder.build(options); requestConfig = options.getRequestConfig(); exponentialBackOff = options.getExponentialBackOff();