reworked proxy using

This commit is contained in:
Vyacheslav N. Boyko 2018-04-17 11:46:48 +03:00
parent b9c32c55d8
commit 28f80e1bcd
5 changed files with 45 additions and 50 deletions

View File

@ -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 {

View File

@ -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());

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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();