This repository was archived by the owner on Jul 10, 2024. It is now read-only.
forked from dstendardi/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApacheHttpClientAdapter.java
More file actions
67 lines (52 loc) · 2.3 KB
/
Copy pathApacheHttpClientAdapter.java
File metadata and controls
67 lines (52 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.snowplowanalytics.snowplow.tracker.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.snowplowanalytics.snowplow.tracker.Constants;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
public class ApacheHttpClientAdapter extends AbstractHttpClientAdapter {
private final Logger LOGGER = LoggerFactory.getLogger(ApacheHttpClientAdapter.class);
private final String uri;
private CloseableHttpClient httpClient;
public ApacheHttpClientAdapter(String uri, CloseableHttpClient httpClient, ObjectMapper objectMapper) {
super(objectMapper);
this.uri = uri;
this.httpClient = httpClient;
}
public int doGet(Map<String, Object> payload) {
HttpResponse httpResponse;
try {
URIBuilder uriBuilder = new URIBuilder(uri);
for (String key : payload.keySet()) {
String value = (String) payload.get(key);
uriBuilder.setParameter(key, value);
}
HttpGet httpGet = new HttpGet(uriBuilder.setPath("/i").build());
httpResponse = httpClient.execute(httpGet);
} catch (Exception e) {
throw new RuntimeException(e);
}
return httpResponse.getStatusLine().getStatusCode();
}
public int doPost(String payload) {
HttpResponse httpResponse;
try {
URIBuilder uriBuilder = new URIBuilder(uri);
HttpPost httpPost = new HttpPost(uriBuilder.setPath("/" + Constants.PROTOCOL_VENDOR + "/" + Constants.PROTOCOL_VERSION).build());
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
StringEntity params = new StringEntity(payload);
httpPost.setEntity(params);
httpResponse = httpClient.execute(httpPost);
LOGGER.debug(httpResponse.getStatusLine().toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
return httpResponse.getStatusLine().getStatusCode();
}
}