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
108 lines (94 loc) · 3.84 KB
/
PiwikTracker.java
File metadata and controls
108 lines (94 loc) · 3.84 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
/*
* 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 java.io.IOException;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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.DefaultHttpClient;
/**
* 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 final UriBuilder uriBuilder;
/**
* 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){
uriBuilder = UriBuilder.fromPath(hostUrl);
}
/**
* 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());
return client.execute(get);
}
/**
* 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));
return client.execute(post);
}
/**
* Get a HTTP client.
* @return a HTTP client
*/
protected HttpClient getHttpClient(){
return new DefaultHttpClient();
}
}