Skip to content

Commit 0853cc0

Browse files
committed
rewritten ping so that it's really independent on Apache HttpClient implementation
1 parent 4636ae8 commit 0853cc0

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

src/main/java/cz/jiripinkas/jsitemapgenerator/AbstractSitemapGenerator.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@
33
import cz.jiripinkas.jsitemapgenerator.exception.InvalidPriorityException;
44
import cz.jiripinkas.jsitemapgenerator.exception.InvalidUrlException;
55
import cz.jiripinkas.jsitemapgenerator.exception.WebmasterToolsException;
6-
import okhttp3.OkHttpClient;
7-
import okhttp3.Request;
8-
import okhttp3.Response;
9-
import org.apache.http.HttpEntity;
10-
import org.apache.http.client.methods.CloseableHttpResponse;
11-
import org.apache.http.client.methods.HttpGet;
12-
import org.apache.http.impl.client.CloseableHttpClient;
13-
import org.apache.http.util.EntityUtils;
14-
import org.springframework.http.ResponseEntity;
15-
import org.springframework.web.client.RestTemplate;
166

177
import java.io.*;
8+
import java.lang.reflect.Method;
189
import java.net.*;
1910
import java.nio.charset.StandardCharsets;
2011
import java.nio.file.Path;
@@ -239,31 +230,51 @@ public PingResponse ping(Ping ping) {
239230
ping(resourceUrl, sitemapUrl, searchEngine.getPrettyName());
240231
} else if(ping.getHttpClientType() == Ping.HttpClientType.REST_TEMPLATE) {
241232
String pingUrl = resourceUrl + sitemapUrl;
242-
RestTemplate restTemplate = (RestTemplate) ping.getHttpClientImplementation();
243-
ResponseEntity<String> responseEntity = restTemplate.getForEntity(pingUrl, String.class);
233+
org.springframework.web.client.RestTemplate restTemplate = (org.springframework.web.client.RestTemplate) ping.getHttpClientImplementation();
234+
org.springframework.http.ResponseEntity<String> responseEntity = restTemplate.getForEntity(pingUrl, String.class);
244235
if (responseEntity.getStatusCodeValue() != 200) {
245236
responseIsNot200 = true;
246237
}
247238
} else if(ping.getHttpClientType() == Ping.HttpClientType.OK_HTTP) {
248239
String pingUrl = resourceUrl + URLEncoder.encode(sitemapUrl, "UTF-8");
249-
OkHttpClient okHttpClient = (OkHttpClient) ping.getHttpClientImplementation();
250-
Request request = new Request.Builder().url(pingUrl).build();
251-
try (Response response = okHttpClient.newCall(request).execute()) {
240+
okhttp3.OkHttpClient okHttpClient = (okhttp3.OkHttpClient) ping.getHttpClientImplementation();
241+
okhttp3.Request request = new okhttp3.Request.Builder().url(pingUrl).build();
242+
try (okhttp3.Response response = okHttpClient.newCall(request).execute()) {
252243
if (!response.isSuccessful()) {
253244
responseIsNot200 = true;
254245
}
255246
}
256247
} else if(ping.getHttpClientType() == Ping.HttpClientType.APACHE_HTTP_CLIENT) {
257248
String pingUrl = resourceUrl + URLEncoder.encode(sitemapUrl, "UTF-8");
258-
CloseableHttpClient closeableHttpClient = (CloseableHttpClient) ping.getHttpClientImplementation();
259-
HttpGet httpGet = new HttpGet(pingUrl);
260-
try (CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet)) {
261-
HttpEntity httpEntity = httpResponse.getEntity();
262-
EntityUtils.consume(httpEntity);
263-
if(httpResponse.getStatusLine().getStatusCode() != 200) {
249+
Object httpGet = Class.forName("org.apache.http.client.methods.HttpGet").getDeclaredConstructor(String.class).newInstance(pingUrl);
250+
Method execute = Class.forName("org.apache.http.impl.client.CloseableHttpClient").getMethod("execute", Class.forName("org.apache.http.client.methods.HttpUriRequest"));
251+
Object httpResponse = null;
252+
try {
253+
httpResponse = execute.invoke(ping.getHttpClientImplementation(), httpGet);
254+
Method getEntity = Class.forName("org.apache.http.HttpResponse").getMethod("getEntity");
255+
Object httpEntity = getEntity.invoke(httpResponse);
256+
Method consume = Class.forName("org.apache.http.util.EntityUtils").getMethod("consume", Class.forName("org.apache.http.HttpEntity"));
257+
consume.invoke(null, httpEntity);
258+
Object getStatusLine = Class.forName("org.apache.http.HttpResponse").getMethod("getStatusLine").invoke(httpResponse);
259+
int getStatusCode = (Integer) Class.forName("org.apache.http.StatusLine").getMethod("getStatusCode").invoke(getStatusLine);
260+
if(getStatusCode != 200) {
264261
responseIsNot200 = true;
265262
}
263+
} finally {
264+
if(httpResponse != null) {
265+
Class.forName("java.io.Closeable").getMethod("close").invoke(httpResponse);
266+
}
266267
}
268+
// same code without reflection
269+
// org.apache.http.impl.client.CloseableHttpClient closeableHttpClient = (org.apache.http.impl.client.CloseableHttpClient) ping.getHttpClientImplementation();
270+
// org.apache.http.client.methods.HttpGet httpGet = new org.apache.http.client.methods.HttpGet(pingUrl);
271+
// try (org.apache.http.client.methods.CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet)) {
272+
// org.apache.http.HttpEntity httpEntity = httpResponse.getEntity();
273+
// org.apache.http.util.EntityUtils.consume(httpEntity);
274+
// if(httpResponse.getStatusLine().getStatusCode() != 200) {
275+
// responseIsNot200 = true;
276+
// }
277+
// }
267278
} else {
268279
throw new UnsupportedOperationException("Unknown HttpClientType!");
269280
}

src/test/java/cz/jiripinkas/jsitemapgenerator/AbstractSitemapGeneratorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package cz.jiripinkas.jsitemapgenerator;
22

3+
import cz.jiripinkas.jsitemapgenerator.generator.SitemapGenerator;
34
import cz.jiripinkas.jsitemapgenerator.generator.SitemapIndexGenerator;
5+
import org.apache.http.impl.client.CloseableHttpClient;
6+
import org.apache.http.impl.client.HttpClients;
47
import org.junit.jupiter.api.BeforeEach;
58
import org.junit.jupiter.api.Test;
69

10+
import java.io.IOException;
711
import java.time.LocalDateTime;
812

913
import static org.junit.jupiter.api.Assertions.*;
@@ -53,4 +57,17 @@ void getAbsoluteUrlBaseUrlCheck() {
5357
String absoluteUrl = sitemapIndexGenerator.getAbsoluteUrl(null);
5458
assertEquals("http://javalibs.com/", absoluteUrl);
5559
}
60+
61+
@Test
62+
void pingWithApacheHttpClient() throws IOException {
63+
try (CloseableHttpClient client = HttpClients.createDefault()) {
64+
Ping ping = Ping.builder()
65+
.engines(Ping.SearchEngine.GOOGLE, Ping.SearchEngine.BING)
66+
.httpClientApacheHttpClient(client)
67+
.build();
68+
SitemapGenerator.of("https://example.com")
69+
.ping(ping)
70+
.throwOnFailure();
71+
}
72+
}
5673
}

0 commit comments

Comments
 (0)