Remove Guice

This commit is contained in:
Ruben Bermudez 2020-11-03 02:57:36 +00:00
parent fb5626de7a
commit ab00b182e8
19 changed files with 76 additions and 267 deletions

View File

@ -7,6 +7,7 @@
6. When registering a Webhook Bot, a SetWebhook object must be provided.
7. When using Webhook with Spring, extends class SpringWebhookBot instead of WebhookBot
8. New Async methods returning CompletableFutures.
9. No more Guice to define custom class
### <a id="4.9.2"></a>4.9.2 ###
1. Bug fixing: #792, #801, #804, #810, #812, #813, #820 and #814

View File

@ -81,11 +81,8 @@ Running the bot is just like running the regular Telegram bots. Create a Java cl
```java
public class Application {
public static void main(String[] args) {
// Initializes dependencies necessary for the base bot - Guice
ApiContextInitializer.init();
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi();
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
try {
// Register your newly created AbilityBot

View File

@ -6,7 +6,6 @@ import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
@ -24,7 +23,7 @@ public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingB
}
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
this(chatIdConverter, ApiContext.getInstance(DefaultBotOptions.class));
this(chatIdConverter, new DefaultBotOptions());
}
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions){

View File

@ -1,7 +1,6 @@
package org.telegram.telegrambots.extensions.bots.commandbot;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
@ -29,7 +28,7 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
*
*/
public TelegramLongPollingCommandBot() {
this(ApiContext.getInstance(DefaultBotOptions.class));
this(new DefaultBotOptions());
}
/**

View File

@ -69,7 +69,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<guice.version>4.2.3</guice.version>
<jackson.version>2.11.3</jackson.version>
<jacksonanotation.version>2.11.3</jacksonanotation.version>
<json.version>20180813</json.version>
@ -77,17 +76,6 @@
</properties>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>

View File

@ -1,67 +0,0 @@
package org.telegram.telegrambots.meta;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
/**
* @author Ruben Bermudez
* @version 1.0
*/
public class ApiContext {
private static final Logger log = LoggerFactory.getLogger(ApiContext.class);
private static final Object lock = new Object();
private static Injector INJECTOR;
private static Map<Class, Class> bindings = new HashMap<>();
private static Map<Class, Class> singletonBindings = new HashMap<>();
public static <T> T getInstance(Class<T> type) {
return getInjector().getInstance(type);
}
public static <T, S extends T> void register(Class<T> type, Class<S> implementation) {
if (bindings.containsKey(type)) {
log.debug(MessageFormat.format("Class {0} already registered", type.getName()));
}
bindings.put(type, implementation);
}
public static <T, S extends T> void registerSingleton(Class<T> type, Class<S> implementation) {
if (singletonBindings.containsKey(type)) {
log.debug(MessageFormat.format("Class {0} already registered", type.getName()));
}
singletonBindings.put(type, implementation);
}
private static Injector getInjector() {
if (INJECTOR == null) {
synchronized (lock) {
if (INJECTOR == null) {
INJECTOR = Guice.createInjector(new ApiModule());
}
}
}
return INJECTOR;
}
@SuppressWarnings("unchecked")
private static class ApiModule extends AbstractModule {
@Override
protected void configure() {
for (Map.Entry<Class, Class> binding : bindings.entrySet()) {
bind(binding.getKey()).to(binding.getValue());
}
for (Map.Entry<Class, Class> binding : singletonBindings.entrySet()) {
bind(binding.getKey()).to(binding.getValue()).in(Singleton.class);
}
}
}
}

View File

@ -2,13 +2,12 @@ package org.telegram.telegrambots.meta;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;
import java.text.MessageFormat;
import java.lang.reflect.InvocationTargetException;
/**
* @author Ruben Bermudez
@ -17,110 +16,53 @@ import java.text.MessageFormat;
*/
public class TelegramBotsApi {
private static final String webhookUrlFormat = "{0}callback/";
Class<? extends BotSession> botSessionClass;
private boolean useWebhook; ///< True to enable webhook usage
private Webhook webhook; ///< Webhook instance
private String externalUrl; ///< External url of the bots
private String pathToCertificate; ///< Path to public key certificate
/**
*
*/
public TelegramBotsApi() {
public TelegramBotsApi(Class<? extends BotSession> botSessionClass) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
this.botSessionClass = botSessionClass;
}
/**
* Creates an HTTP server to receive webhook request
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
* @param webhook Webhook class
*
* @implSpec This option requires externally handled HTTPS support (i.e. via proxy)
* @implSpec This option may require externally handled HTTPS support (i.e. via proxy)
*/
public TelegramBotsApi(String externalUrl, String internalUrl) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
public TelegramBotsApi(Class<? extends BotSession> botSessionClass, Webhook webhook) throws TelegramApiException {
if (botSessionClass == null) {
throw new TelegramApiException("Parameter botSessionClass can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
if (webhook == null) {
throw new TelegramApiException("Parameter webhook can not be null or empty");
}
this.botSessionClass = botSessionClass;
this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.startServer();
}
/**
* Creates an HTTPS server to receive webhook request
* @param keyStore KeyStore for the server
* @param keyStorePassword Key store password for the server
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
}
if (keyStore == null || keyStore.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
}
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
}
this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.setKeyStore(keyStore, keyStorePassword);
webhook.startServer();
}
/**
* Creates an HTTPS server with self-signed certificate to receive webhook request
* @param keyStore KeyStore for the server
* @param keyStorePassword Key store password for the server
* @param externalUrl External base url for the webhook
* @param internalUrl Internal base url for the webhook
* @param pathToCertificate Full path until .pem public certificate keys
*/
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl, String pathToCertificate) throws TelegramApiRequestException {
if (externalUrl == null || externalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter externalUrl can not be null or empty");
}
if (internalUrl == null || internalUrl.isEmpty()) {
throw new TelegramApiRequestException("Parameter internalUrl can not be null or empty");
}
if (keyStore == null || keyStore.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStore can not be null or empty");
}
if (keyStorePassword == null || keyStorePassword.isEmpty()) {
throw new TelegramApiRequestException("Parameter keyStorePassword can not be null or empty");
}
if (pathToCertificate == null || pathToCertificate.isEmpty()) {
throw new TelegramApiRequestException("Parameter pathToCertificate can not be null or empty");
}
this.useWebhook = true;
this.externalUrl = fixExternalUrl(externalUrl);
this.pathToCertificate = pathToCertificate;
webhook = ApiContext.getInstance(Webhook.class);
webhook.setInternalUrl(internalUrl);
webhook.setKeyStore(keyStore, keyStorePassword);
webhook.startServer();
this.webhook = webhook;
this.webhook.startServer();
}
/**
* Register a bot. The Bot Session is started immediately, and may be disconnected by calling close.
* @param bot the bot to register
*/
public BotSession registerBot(LongPollingBot bot) throws TelegramApiRequestException {
public BotSession registerBot(LongPollingBot bot) throws TelegramApiException {
bot.onRegister();
bot.clearWebhook();
BotSession session = ApiContext.getInstance(BotSession.class);
BotSession session;
try {
session = botSessionClass.getConstructor().newInstance();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new TelegramApiException(e);
}
session.setToken(bot.getBotToken());
session.setOptions(bot.getOptions());
session.setCallback(bot);
@ -140,11 +82,4 @@ public class TelegramBotsApi {
bot.setWebhook(setWebhook);
}
}
private static String fixExternalUrl(String externalUrl) {
if (externalUrl != null && !externalUrl.endsWith("/")) {
externalUrl = externalUrl + "/";
}
return MessageFormat.format(webhookUrlFormat, externalUrl);
}
}

View File

@ -1,6 +1,6 @@
package org.telegram.telegrambots.meta.generics;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
/**
* @author Ruben Bermudez
@ -8,8 +8,8 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
* Webhook interface
*/
public interface Webhook {
void startServer() throws TelegramApiRequestException;
void startServer() throws TelegramApiException;
void registerWebhook(WebhookBot callback);
void setInternalUrl(String internalUrl);
void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiRequestException;
void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiException;
}

View File

@ -54,7 +54,7 @@ class TestDeserialization {
"\"message\":{\"message_id\":97,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154360,\"video_note\":{\"duration\":3,\"length\":240,\"thumb\":{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":6852,\"width\":240,\"height\":240},\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":100544}}},{\"update_id\":79995152,\n" +
"\"message\":{\"message_id\":98,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154363,\"voice\":{\"duration\":1,\"mime_type\":\"audio/ogg\",\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":5198}}},{\"update_id\":79995153,\n" +
"\"message\":{\"message_id\":99,\"from\":{\"id\":12345678,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"it\"},\"chat\":{\"id\":12345678,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"type\":\"private\"},\"date\":1604154371,\"photo\":[{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":14395,\"width\":180,\"height\":320},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":52852,\"width\":450,\"height\":800},{\"file_id\":\"FILEID\",\"file_unique_id\":\"FILEID\",\"file_size\":84493,\"width\":720,\"height\":1280}]}},{\"update_id\":79995154,\n" +
"\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-110011556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";
"\"message\":{\"message_id\":6,\"from\":{\"id\":1234567,\"is_bot\":false,\"first_name\":\"MyFirstName\",\"username\":\"MyUsername\",\"language_code\":\"en\"},\"chat\":{\"id\":-1556359722345678,\"title\":\"test group\",\"type\":\"supergroup\"},\"date\":1604163105,\"new_chat_members\":[{\"id\":123455678,\"is_bot\":true,\"first_name\":\"Testing\",\"username\":\"TestingBot\"}]}}]}";
ArrayList<Update> response = new GetUpdates().deserializeResponse(updateText);

View File

@ -1,28 +1,33 @@
package org.telegram.telegrambots.meta.test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.test.base.TestBase;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.Webhook;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
/**
* @author Ruben Bermudez
* @version 1.0
*/
class TestTelegramApi extends TestBase {
class TestTelegramApi {
@Test
void TestTelegramApiMustBeInitializableForLongPolling() {
new TelegramBotsApi();
private Webhook webhook;
@BeforeEach
public void setUp() {
webhook = mock(Webhook.class);
}
@Test
void TestTelegramApiMustBeInitializableForWebhookWithoutSecureSupport() {
void TestTelegramApiMustBeInitializableForLongPolling() {
try {
new TelegramBotsApi("externalUrl", "internalUrl");
} catch (TelegramApiRequestException e) {
new TelegramBotsApi(BotSession.class);
} catch (TelegramApiException e) {
fail();
}
}
@ -30,17 +35,8 @@ class TestTelegramApi extends TestBase {
@Test
void TestTelegramApiMustBeInitializableForWebhook() {
try {
new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl");
} catch (TelegramApiRequestException e) {
fail();
}
}
@Test
void TestTelegramApiMustBeInitializableForWebhookWithSelfSignedCertificate() {
try {
new TelegramBotsApi("keyStore", "keyStorePassword", "externalUrl", "internalUrl", "selfSignedPath");
} catch (TelegramApiRequestException e) {
new TelegramBotsApi(BotSession.class, webhook);
} catch (TelegramApiException e) {
fail();
}
}

View File

@ -1,21 +0,0 @@
package org.telegram.telegrambots.meta.test.base;
import org.junit.jupiter.api.BeforeAll;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.test.fakes.FakeBotSession;
import org.telegram.telegrambots.meta.test.fakes.FakeWebhook;
/**
* @author Ruben Bermudez
* @version 1.0
*/
public abstract class TestBase {
@BeforeAll
public static void beforeClass() {
ApiContext.register(BotSession.class, FakeBotSession.class);
ApiContext.register(Webhook.class, FakeWebhook.class);
}
}

View File

@ -6,7 +6,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import java.util.Collections;
import java.util.List;
@ -19,8 +21,8 @@ public class TelegramBotStarterConfiguration {
@Bean
@ConditionalOnMissingBean(TelegramBotsApi.class)
public TelegramBotsApi telegramBotsApi() {
return new TelegramBotsApi();
public TelegramBotsApi telegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(DefaultBotSession.class);
}
@Bean

View File

@ -8,13 +8,18 @@ import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
class TestTelegramBotStarterRegistrationHooks {
@ -29,7 +34,7 @@ class TestTelegramBotStarterRegistrationHooks {
TelegramBotStarterConfiguration.class));
@Test
void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiRequestException {
void longPollingBotWithAnnotatedMethodshouldBeCalled() throws TelegramApiException {
when(mockTelegramBotsApi.registerBot(any(LongPollingBot.class))).thenReturn(someBotSession);

View File

@ -97,6 +97,11 @@
<artifactId>telegrambots-meta</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>

View File

@ -1,22 +0,0 @@
package org.telegram.telegrambots;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import org.telegram.telegrambots.updatesreceivers.DefaultWebhook;
/**
* @author Ruben Bermudez
* @version 1.0
* Initialization of ApiContext
*/
public final class ApiContextInitializer {
private ApiContextInitializer() {
}
public static void init() {
ApiContext.register(BotSession.class, DefaultBotSession.class);
ApiContext.register(Webhook.class, DefaultWebhook.class);
}
}

View File

@ -1,6 +1,5 @@
package org.telegram.telegrambots.bots;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.util.WebhookUtils;
@ -13,7 +12,7 @@ import org.telegram.telegrambots.util.WebhookUtils;
*/
public abstract class TelegramLongPollingBot extends DefaultAbsSender implements LongPollingBot {
public TelegramLongPollingBot() {
this(ApiContext.getInstance(DefaultBotOptions.class));
this(new DefaultBotOptions());
}
public TelegramLongPollingBot(DefaultBotOptions options) {

View File

@ -1,6 +1,5 @@
package org.telegram.telegrambots.bots;
import org.telegram.telegrambots.meta.ApiContext;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.WebhookBot;
@ -15,7 +14,7 @@ import org.telegram.telegrambots.util.WebhookUtils;
@SuppressWarnings("WeakerAccess")
public abstract class TelegramWebhookBot extends DefaultAbsSender implements WebhookBot {
public TelegramWebhookBot() {
this(ApiContext.getInstance(DefaultBotOptions.class));
this(new DefaultBotOptions());
}
public TelegramWebhookBot(DefaultBotOptions options) {

View File

@ -1,7 +1,6 @@
package org.telegram.telegrambots.updatesreceivers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@ -61,7 +60,6 @@ public class DefaultBotSession implements BotSession {
private DefaultBotOptions options;
private UpdatesSupplier updatesSupplier;
@Inject
public DefaultBotSession() {
}

View File

@ -1,14 +1,12 @@
package org.telegram.telegrambots.updatesreceivers;
import com.google.inject.Inject;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.Webhook;
import org.telegram.telegrambots.meta.generics.WebhookBot;
@ -19,8 +17,7 @@ import java.net.URI;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Webhook to receive updates
* @date 20 of June of 2015
* Webhook to receive updates
*/
public class DefaultWebhook implements Webhook {
private String keystoreServerFile;
@ -29,8 +26,7 @@ public class DefaultWebhook implements Webhook {
private final RestApi restApi;
@Inject
public DefaultWebhook() throws TelegramApiRequestException {
public DefaultWebhook() {
this.restApi = new RestApi();
}
@ -38,7 +34,7 @@ public class DefaultWebhook implements Webhook {
this.internalUrl = internalUrl;
}
public void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiRequestException {
public void setKeyStore(String keyStore, String keyStorePassword) throws TelegramApiException {
this.keystoreServerFile = keyStore;
this.keystoreServerPwd = keyStorePassword;
validateServerKeystoreFile(keyStore);
@ -48,7 +44,7 @@ public class DefaultWebhook implements Webhook {
restApi.registerCallback(callback);
}
public void startServer() throws TelegramApiRequestException {
public void startServer() throws TelegramApiException {
ResourceConfig rc = new ResourceConfig();
rc.register(restApi);
rc.register(JacksonFeature.class);
@ -71,7 +67,7 @@ public class DefaultWebhook implements Webhook {
try {
grizzlyServer.start();
} catch (IOException e) {
throw new TelegramApiRequestException("Error starting webhook server", e);
throw new TelegramApiException("Error starting webhook server", e);
}
}
@ -79,10 +75,10 @@ public class DefaultWebhook implements Webhook {
return URI.create(internalUrl);
}
private static void validateServerKeystoreFile(String keyStore) throws TelegramApiRequestException {
private static void validateServerKeystoreFile(String keyStore) throws TelegramApiException {
File file = new File(keyStore);
if (!file.exists() || !file.canRead()) {
throw new TelegramApiRequestException("Can't find or access server keystore file.");
throw new TelegramApiException("Can't find or access server keystore file.");
}
}
}