Skip to content

Commit df02bb4

Browse files
valkucrubenlagus
authored andcommitted
Jetty HttpClient implementation of TelegramClient
1 parent c00e48a commit df02bb4

File tree

10 files changed

+1540
-17
lines changed

10 files changed

+1540
-17
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>telegrambots-longpolling</module>
1515
<module>telegrambots-webhook</module>
1616
<module>telegrambots-client</module>
17+
<module>telegrambots-client-jetty-adapter</module>
1718
<module>telegrambots-springboot-longpolling-starter</module>
1819
<module>telegrambots-springboot-webhook-starter</module>
1920
<module>telegrambots-extensions</module>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.telegram</groupId>
9+
<artifactId>Bots</artifactId>
10+
<version>7.9.1</version>
11+
</parent>
12+
13+
<name>Telegram Bots Client Jetty HttpClient adapter</name>
14+
<url>https://github.com/rubenlagus/TelegramBots</url>
15+
<description>Use Jetty HttpClient instead of OkHttp to perform API calls</description>
16+
17+
<artifactId>telegrambots-client-jetty-adapter</artifactId>
18+
<packaging>jar</packaging>
19+
20+
<properties>
21+
<java.version>17</java.version>
22+
<maven.compiler.source>${java.version}</maven.compiler.source>
23+
<maven.compiler.target>${java.version}</maven.compiler.target>
24+
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
27+
28+
<jetty.version>12.0.12</jetty.version>
29+
</properties>
30+
31+
<dependencyManagement>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.eclipse.jetty</groupId>
35+
<artifactId>jetty-client</artifactId>
36+
<version>${jetty.version}</version>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.telegram</groupId>
44+
<artifactId>telegrambots-client</artifactId>
45+
<version>${project.parent.version}</version>
46+
<exclusions>
47+
<exclusion>
48+
<groupId>com.squareup.okhttp3</groupId>
49+
<artifactId>okhttp</artifactId>
50+
</exclusion>
51+
</exclusions>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.eclipse.jetty</groupId>
55+
<artifactId>jetty-client</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>commons-io</groupId>
59+
<artifactId>commons-io</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.projectlombok</groupId>
63+
<artifactId>lombok</artifactId>
64+
<scope>provided</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>com.squareup.okhttp3</groupId>
69+
<artifactId>mockwebserver</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<directory>${project.basedir}/target</directory>
76+
<outputDirectory>${project.build.directory}/classes</outputDirectory>
77+
<finalName>${project.artifactId}-${project.version}</finalName>
78+
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
79+
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
80+
<plugins>
81+
<plugin>
82+
<artifactId>maven-clean-plugin</artifactId>
83+
</plugin>
84+
<plugin>
85+
<artifactId>maven-assembly-plugin</artifactId>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-source-plugin</artifactId>
90+
</plugin>
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-dependency-plugin</artifactId>
94+
</plugin>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-surefire-plugin</artifactId>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-javadoc-plugin</artifactId>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package org.telegram.telegrambots.client.jetty;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.eclipse.jetty.client.*;
6+
import org.eclipse.jetty.http.HttpFields;
7+
import org.eclipse.jetty.http.MultiPart;
8+
import org.telegram.telegrambots.meta.api.objects.InputFile;
9+
import org.telegram.telegrambots.meta.api.objects.media.InputMedia;
10+
import org.telegram.telegrambots.meta.api.objects.media.paid.InputPaidMedia;
11+
import org.telegram.telegrambots.meta.api.objects.stickers.InputSticker;
12+
13+
import java.io.IOException;
14+
import java.util.List;
15+
16+
/**
17+
* @author Valeriy Kucherenko
18+
* @since 05.09.2024
19+
*/
20+
public class JettyMultipartBuilder {
21+
private final MultiPartRequestContent multiPart = new MultiPartRequestContent();
22+
private final ObjectMapper mapper;
23+
24+
public JettyMultipartBuilder(ObjectMapper mapper) {
25+
this.mapper = mapper;
26+
}
27+
28+
public MultiPartRequestContent build() {
29+
multiPart.close();
30+
return multiPart;
31+
}
32+
33+
/**
34+
* Add field to the builder if value is not null
35+
* @param fieldName the field name to add to the multipart
36+
* @param value the nullable value to add
37+
* @return the builder
38+
*/
39+
public JettyMultipartBuilder addPart(String fieldName, String value) {
40+
if (value != null) {
41+
multiPart.addPart(new MultiPart.ContentSourcePart(fieldName, null, HttpFields.EMPTY, new StringRequestContent(value)));
42+
}
43+
return this;
44+
}
45+
46+
/**
47+
* Add field to the builder if value is not null. The value is converted using toString()
48+
* @param fieldName the field name to add to the multipart
49+
* @param value the nullable value to add
50+
* @return the builder
51+
*/
52+
public JettyMultipartBuilder addPart(String fieldName, Object value) {
53+
if (value != null) {
54+
this.addPart(fieldName, value.toString());
55+
}
56+
return this;
57+
}
58+
59+
/**
60+
* Add field to the builder if value is not null. The value is converted using ObjectMapper.writeValueAsString()
61+
* @param fieldName the field name to add to the multipart
62+
* @param value the nullable value to add
63+
* @return the builder
64+
*/
65+
public JettyMultipartBuilder addJsonPart(String fieldName, Object value) throws JsonProcessingException {
66+
if (value != null) {
67+
this.addPart(fieldName, mapper.writeValueAsString(value));
68+
}
69+
return this;
70+
}
71+
72+
public JettyMultipartBuilder addInputFile(String fileField, InputFile file, boolean addField) throws IOException {
73+
if (file == null) {
74+
return this;
75+
}
76+
77+
if (file.isNew()) {
78+
Request.Content body = null;
79+
if (file.getNewMediaFile() != null) {
80+
body = new PathRequestContent("application/octet-stream", file.getNewMediaFile().toPath());
81+
} else if (file.getNewMediaStream() != null) {
82+
body = new InputStreamRequestContent("application/octet-stream", file.getNewMediaStream());
83+
}
84+
if (body != null) {
85+
multiPart.addPart(new MultiPart.ContentSourcePart(file.getMediaName(), file.getMediaName(), HttpFields.EMPTY, body));
86+
}
87+
}
88+
89+
if (addField) {
90+
this.addPart(fileField, file.getAttachName());
91+
}
92+
93+
return this;
94+
}
95+
96+
public JettyMultipartBuilder addMedia(InputMedia media) throws IOException {
97+
if (media == null) {
98+
return this;
99+
}
100+
101+
if (media.isNewMedia()) {
102+
Request.Content body = null;
103+
if (media.getNewMediaFile() != null) {
104+
body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath());
105+
} else if (media.getNewMediaStream() != null) {
106+
body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream());
107+
}
108+
if (body != null) {
109+
multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body));
110+
}
111+
}
112+
113+
return this;
114+
}
115+
116+
public JettyMultipartBuilder addMedia(InputPaidMedia media) throws IOException {
117+
if (media == null) {
118+
return this;
119+
}
120+
121+
if (media.isNewMedia()) {
122+
Request.Content body = null;
123+
if (media.getNewMediaFile() != null) {
124+
body = new PathRequestContent("application/octet-stream", media.getNewMediaFile().toPath());
125+
} else if (media.getNewMediaStream() != null) {
126+
body = new InputStreamRequestContent("application/octet-stream", media.getNewMediaStream());
127+
}
128+
if (body != null) {
129+
multiPart.addPart(new MultiPart.ContentSourcePart(media.getMediaName(), media.getMediaName(), HttpFields.EMPTY, body));
130+
}
131+
}
132+
133+
return this;
134+
}
135+
136+
public JettyMultipartBuilder addInputStickers(String stickersField, List<InputSticker> stickers) throws IOException {
137+
for (InputSticker sticker : stickers) {
138+
addInputFile(null, sticker.getSticker(), false);
139+
}
140+
141+
addJsonPart(stickersField, stickers);
142+
143+
return this;
144+
}
145+
}

0 commit comments

Comments
 (0)