Skip to content

Commit acccafc

Browse files
authored
migrate httpClient to okhttp in the currency plugin (#108)
1 parent 3e4ceee commit acccafc

File tree

8 files changed

+47
-38
lines changed

8 files changed

+47
-38
lines changed

rebot-plugins/rebot-brazil-postalcode-plugin/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<groupId>io.quarkus</groupId>
100100
<artifactId>quarkus-maven-plugin</artifactId>
101101
<version>${io.quarkus.version}</version>
102+
<configuration>
103+
<noDeps>true</noDeps>
104+
</configuration>
102105
<executions>
103106
<execution>
104107
<goals>

rebot-plugins/rebot-currency-plugin/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
</dependency>
7575

7676
<dependency>
77-
<groupId>org.apache.httpcomponents</groupId>
78-
<artifactId>httpclient</artifactId>
77+
<groupId>com.squareup.okhttp3</groupId>
78+
<artifactId>okhttp</artifactId>
7979
</dependency>
8080

8181
<dependency>

rebot-plugins/rebot-currency-plugin/src/main/java/xyz/rebasing/rebot/plugin/currency/ecb/ECBClient.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package xyz.rebasing.rebot.plugin.currency.ecb;
2525

26+
import java.io.InputStream;
2627
import java.lang.invoke.MethodHandles;
2728

2829
import javax.enterprise.context.ApplicationScoped;
@@ -33,12 +34,9 @@
3334
import com.github.benmanes.caffeine.cache.Cache;
3435
import com.github.benmanes.caffeine.cache.Caffeine;
3536
import io.quarkus.scheduler.Scheduled;
36-
import org.apache.http.HttpResponse;
37-
import org.apache.http.client.config.CookieSpecs;
38-
import org.apache.http.client.config.RequestConfig;
39-
import org.apache.http.client.methods.HttpGet;
40-
import org.apache.http.impl.client.CloseableHttpClient;
41-
import org.apache.http.impl.client.HttpClients;
37+
import okhttp3.OkHttpClient;
38+
import okhttp3.Request;
39+
import okhttp3.Response;
4240
import org.jboss.logging.Logger;
4341
import xyz.rebasing.rebot.service.persistence.repository.EcbRepository;
4442

@@ -63,35 +61,39 @@ public Cache<String, Object> cache() {
6361

6462
@Scheduled(every = "12h", delay = 60)
6563
public void getAndPersistDailyCurrencies() {
66-
try {
64+
OkHttpClient client = new OkHttpClient.Builder()
65+
.build();
66+
Request request = new Request.Builder()
67+
.url(ECB_XML_ADDRESS)
68+
.get()
69+
.build();
70+
71+
try (Response response = client.newCall(request).execute()) {
6772
log.debugv("Parsing currencies from {0}", ECB_XML_ADDRESS);
6873

6974
SAXParserFactory saxParser = SAXParserFactory.newInstance();
7075
saxParser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
7176
saxParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
7277

73-
HttpGet httpReq = new HttpGet(ECB_XML_ADDRESS);
74-
HttpResponse response = client().execute(httpReq);
75-
76-
saxParser.newSAXParser().parse(response.getEntity().getContent(), handler);
78+
try (InputStream input = response.body().byteStream()) {
79+
if (input != null) {
80+
saxParser.newSAXParser().parse(response.body().byteStream(), handler);
81+
} else {
82+
throw new NullPointerException("Response is null");
83+
}
84+
}
7785

7886
repository.persist(handler.cubes());
7987
c.cleanUp();
8088
handler.cubes().getCubes().forEach(cube -> {
8189
c.put(cube.getCurrency(), cube);
8290
c.put("time", handler.cubes().getTime());
8391
});
92+
8493
} catch (final Exception e) {
8594
log.errorv("Error to retrieve currency rates from {0} - message: {1}", ECB_XML_ADDRESS, e.getMessage());
8695
} finally {
8796
handler.clean();
8897
}
8998
}
90-
91-
private CloseableHttpClient client() {
92-
RequestConfig config = RequestConfig.custom()
93-
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
94-
.build();
95-
return HttpClients.custom().setDefaultRequestConfig(config).build();
96-
}
9799
}

rebot-plugins/rebot-currency-plugin/src/test/java/xyz/rebasing/rebot/plugin/currency/test/SaxParserTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2017 Rebasing.xyz ReBot
4+
* Copyright (c) 2017 Rebasing.xyz ReBot
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy of
77
* this software and associated documentation files (the "Software"), to deal in
@@ -29,12 +29,9 @@
2929
import javax.xml.parsers.SAXParser;
3030
import javax.xml.parsers.SAXParserFactory;
3131

32-
import org.apache.http.HttpResponse;
33-
import org.apache.http.client.config.CookieSpecs;
34-
import org.apache.http.client.config.RequestConfig;
35-
import org.apache.http.client.methods.HttpGet;
36-
import org.apache.http.impl.client.CloseableHttpClient;
37-
import org.apache.http.impl.client.HttpClients;
32+
import okhttp3.OkHttpClient;
33+
import okhttp3.Request;
34+
import okhttp3.Response;
3835
import org.junit.Assert;
3936
import org.junit.Before;
4037
import org.junit.Ignore;
@@ -47,15 +44,19 @@
4744
public class SaxParserTest {
4845

4946
public static final String ECB_XML_ADDRESS = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
50-
5147
private EcbSaxHandler handler = new EcbSaxHandler();
5248

5349
@Before
5450
public void populateCubes() throws ParserConfigurationException, SAXException, IOException {
5551
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
56-
HttpGet httpReq = new HttpGet(ECB_XML_ADDRESS);
57-
HttpResponse response = client().execute(httpReq);
58-
saxParser.parse(response.getEntity().getContent(), handler);
52+
OkHttpClient client = new OkHttpClient.Builder()
53+
.build();
54+
Request request = new Request.Builder()
55+
.url(ECB_XML_ADDRESS)
56+
.get()
57+
.build();
58+
Response response = client.newCall(request).execute();
59+
saxParser.parse(response.body().string(), handler);
5960
}
6061

6162
@Test
@@ -83,11 +84,4 @@ public void testAvailableCurrencies() {
8384
AvailableCurrencies.valueOf(cube.getCurrency());
8485
});
8586
}
86-
87-
private CloseableHttpClient client() {
88-
RequestConfig config = RequestConfig.custom()
89-
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
90-
.build();
91-
return HttpClients.custom().setDefaultRequestConfig(config).build();
92-
}
9387
}

rebot-plugins/rebot-karma-plugin/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
<groupId>io.quarkus</groupId>
108108
<artifactId>quarkus-maven-plugin</artifactId>
109109
<version>${io.quarkus.version}</version>
110+
<configuration>
111+
<noDeps>true</noDeps>
112+
</configuration>
110113
<executions>
111114
<execution>
112115
<goals>

rebot-plugins/rebot-welcome-message-plugin/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
<groupId>io.quarkus</groupId>
127127
<artifactId>quarkus-maven-plugin</artifactId>
128128
<version>${io.quarkus.version}</version>
129+
<configuration>
130+
<noDeps>true</noDeps>
131+
</configuration>
129132
<executions>
130133
<execution>
131134
<goals>

rebot-services/rebot-persistence-service/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
<groupId>io.quarkus</groupId>
117117
<artifactId>quarkus-maven-plugin</artifactId>
118118
<version>${io.quarkus.version}</version>
119+
<configuration>
120+
<noDeps>true</noDeps>
121+
</configuration>
119122
<executions>
120123
<execution>
121124
<goals>

rebot-telegram/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<groupId>io.quarkus</groupId>
125125
<artifactId>quarkus-maven-plugin</artifactId>
126126
<version>${io.quarkus.version}</version>
127+
127128
<executions>
128129
<execution>
129130
<goals>

0 commit comments

Comments
 (0)