forked from matomo-org/matomo-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiwikTracker.java
More file actions
172 lines (149 loc) · 6.08 KB
/
PiwikTracker.java
File metadata and controls
172 lines (149 loc) · 6.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Piwik Java Tracker
*
* @link https://github.com/piwik/piwik-java-tracker
* @license https://github.com/piwik/piwik-java-tracker/blob/master/LICENSE BSD-3 Clause
*/
package org.piwik.java.tracking;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
/**
* A class that sends {@link PiwikRequest}s to a specified Piwik server.
* @author brettcsorba
*/
public class PiwikTracker{
private static final String AUTH_TOKEN = "token_auth";
private static final String REQUESTS = "requests";
private static final int DEFAULT_TIMEOUT = 5000;
private final UriBuilder uriBuilder;
private final int timeout;
private String proxyHost = null;
private int proxyPort = 0;
/**
* Creates a tracker that will send {@link PiwikRequest}s to the specified
* Tracking HTTP API endpoint.
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-piwik-domain.tld/piwik.php</strong>.
*/
public PiwikTracker(String hostUrl){
this(hostUrl, DEFAULT_TIMEOUT);
}
/**
* Creates a tracker that will send {@link PiwikRequest}s to the specified
* Tracking HTTP API endpoint.
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-piwik-domain.tld/piwik.php</strong>.
* @param timeout the timeout of the sent request in milliseconds
*/
public PiwikTracker(String hostUrl, int timeout){
uriBuilder = UriBuilder.fromPath(hostUrl);
this.timeout = timeout;
}
/**
* Creates a tracker that will send {@link PiwikRequest}s to the specified
* Tracking HTTP API endpoint via the provided proxy
*
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-piwik-domain.tld/piwik.php</strong>.
* @param proxyHost url endpoint for the proxy
* @param proxyPort proxy server port number
*/
public PiwikTracker(String hostUrl, String proxyHost, int proxyPort){
this(hostUrl, proxyHost, proxyPort, DEFAULT_TIMEOUT);
}
public PiwikTracker(String hostUrl, String proxyHost, int proxyPort, int timeout){
uriBuilder = UriBuilder.fromPath(hostUrl);
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.timeout = timeout;
}
/**
* Send a request.
* @param request request to send
* @return the response from this request
* @throws IOException thrown if there was a problem with this connection
*/
public HttpResponse sendRequest(PiwikRequest request) throws IOException{
HttpClient client = getHttpClient();
uriBuilder.replaceQuery(request.getUrlEncodedQueryString());
HttpGet get = new HttpGet(uriBuilder.build());
try {
return client.execute(get);
} finally {
get.releaseConnection();
}
}
/**
* Send multiple requests in a single HTTP call. More efficient than sending
* several individual requests.
* @param requests the requests to send
* @return the response from these requests
* @throws IOException thrown if there was a problem with this connection
*/
public HttpResponse sendBulkRequest(Iterable<PiwikRequest> requests) throws IOException{
return sendBulkRequest(requests, null);
}
/**
* Send multiple requests in a single HTTP call. More efficient than sending
* several individual requests. Specify the AuthToken if parameters that require
* an auth token is used.
* @param requests the requests to send
* @param authToken specify if any of the parameters use require AuthToken
* @return the response from these requests
* @throws IOException thrown if there was a problem with this connection
*/
public HttpResponse sendBulkRequest(Iterable<PiwikRequest> requests, String authToken) throws IOException{
if (authToken != null && authToken.length() != PiwikRequest.AUTH_TOKEN_LENGTH){
throw new IllegalArgumentException(authToken+" is not "+PiwikRequest.AUTH_TOKEN_LENGTH+" characters long.");
}
JsonObjectBuilder ob = Json.createObjectBuilder();
JsonArrayBuilder ab = Json.createArrayBuilder();
for (PiwikRequest request : requests){
ab.add("?"+request.getQueryString());
}
ob.add(REQUESTS, ab);
if (authToken != null){
ob.add(AUTH_TOKEN, authToken);
}
HttpClient client = getHttpClient();
HttpPost post = new HttpPost(uriBuilder.build());
post.setEntity(new StringEntity(ob.build().toString(),
ContentType.APPLICATION_JSON));
try {
return client.execute(post);
} finally {
post.releaseConnection();
}
}
/**
* Get a HTTP client. With proxy if a proxy is provided in the constructor.
* @return a HTTP client
*/
protected HttpClient getHttpClient(){
HttpClientBuilder builder = HttpClientBuilder.create();
if(proxyHost != null && proxyPort != 0) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
builder.setRoutePlanner(routePlanner);
}
RequestConfig.Builder config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout);
builder.setDefaultRequestConfig(config.build());
return builder.build();
}
}