Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ensure Telegram API Error Exception details are not being masked #1453

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Jetty HttpClient implementation of TelegramClient
  • Loading branch information
valkuc committed Sep 7, 2024
commit 7a245fd14616c20eee59aeffb5d8f84071650202
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module>telegrambots-longpolling</module>
<module>telegrambots-webhook</module>
<module>telegrambots-client</module>
<module>telegrambots-client-jetty-adapter</module>
<module>telegrambots-springboot-longpolling-starter</module>
<module>telegrambots-springboot-webhook-starter</module>
<module>telegrambots-extensions</module>
Expand Down
105 changes: 105 additions & 0 deletions telegrambots-client-jetty-adapter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.9.1</version>
</parent>

<name>Telegram Bots Client Jetty HttpClient adapter</name>
<url>https://github.com/rubenlagus/TelegramBots</url>
<description>Use Jetty HttpClient instead of OkHttp to perform API calls</description>

<artifactId>telegrambots-client-jetty-adapter</artifactId>
<packaging>jar</packaging>

<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<jetty.version>12.0.12</jetty.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-client</artifactId>
<version>${project.parent.version}</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.telegram.telegrambots.client.jetty;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.jetty.client.*;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.MultiPart;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.media.InputMedia;
import org.telegram.telegrambots.meta.api.objects.media.paid.InputPaidMedia;
import org.telegram.telegrambots.meta.api.objects.stickers.InputSticker;

import java.io.IOException;
import java.util.List;

/**
* @author Valeriy Kucherenko
* @since 05.09.2024
*/
public class JettyMultipartBuilder {
private final MultiPartRequestContent multiPart = new MultiPartRequestContent();
private final ObjectMapper mapper;

public JettyMultipartBuilder(ObjectMapper mapper) {
this.mapper = mapper;
}

public MultiPartRequestContent build() {
multiPart.close();
return multiPart;
}

/**
* Add field to the builder if value is not null
* @param fieldName the field name to add to the multipart
* @param value the nullable value to add
* @return the builder
*/
public JettyMultipartBuilder addPart(String fieldName, String value) {
if (value != null) {
multiPart.addPart(new MultiPart.ContentSourcePart(fieldName, null, HttpFields.EMPTY, new StringRequestContent(value)));
}
return this;
}

/**
* Add field to the builder if value is not null. The value is converted using toString()
* @param fieldName the field name to add to the multipart
* @param value the nullable value to add
* @return the builder
*/
public JettyMultipartBuilder addPart(String fieldName, Object value) {
if (value != null) {
this.addPart(fieldName, value.toString());
}
return this;
}

/**
* Add field to the builder if value is not null. The value is converted using ObjectMapper.writeValueAsString()
* @param fieldName the field name to add to the multipart
* @param value the nullable value to add
* @return the builder
*/
public JettyMultipartBuilder addJsonPart(String fieldName, Object value) throws JsonProcessingException {
if (value != null) {
this.addPart(fieldName, mapper.writeValueAsString(value));
}
return this;
}

public JettyMultipartBuilder addInputFile(String fileField, InputFile file, boolean addField) throws IOException {
if (file == null) {
return this;
}

if (file.isNew()) {
Request.Content body = null;
if (file.getNewMediaFile() != null) {
body = new PathRequestContent("application/octet-stream", file.getNewMediaFile().toPath());
} else if (file.getNewMediaStream() != null) {
body = new InputStreamRequestContent("application/octet-stream", file.getNewMediaStream());
}
if (body != null) {
multiPart.addPart(new MultiPart.ContentSourcePart(file.getMediaName(), file.getMediaName(), HttpFields.EMPTY, body));
}
}

if (addField) {
this.addPart(fileField, file.getAttachName());
}

return this;
}

public JettyMultipartBuilder addMedia(InputMedia media) throws IOException {
if (media == null) {
return this;
}

if (media.isNewMedia()) {
Request.Content body = null;
if (media.getNewMediaFile() != null) {
body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath());
} else if (media.getNewMediaStream() != null) {
body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream());
}
if (body != null) {
multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body));
}
}

return this;
}

public JettyMultipartBuilder addMedia(InputPaidMedia media) throws IOException {
if (media == null) {
return this;
}

if (media.isNewMedia()) {
Request.Content body = null;
if (media.getNewMediaFile() != null) {
body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath());
} else if (media.getNewMediaStream() != null) {
body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream());
}
if (body != null) {
multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body));
}
}

return this;
}

public JettyMultipartBuilder addInputStickers(String stickersField, List<InputSticker> stickers) throws IOException {
for (InputSticker sticker : stickers) {
addInputFile(null, sticker.getSticker(), false);
}

addJsonPart(stickersField, stickers);

return this;
}
}
Loading