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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
//
// RequestHandler.java
// Adjust
//
// Created by Christian Wellenbrock on 2013-06-25.
// Copyright (c) 2013 adjust GmbH. All rights reserved.
// See the file MIT-LICENSE for copying permission.
//
package com.adjust.sdk;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.NameValuePair;
import ch.boye.httpclientandroidlib.client.ClientProtocolException;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.entity.UrlEncodedFormEntity;
import ch.boye.httpclientandroidlib.client.methods.HttpPost;
import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest;
import ch.boye.httpclientandroidlib.client.utils.URLEncodedUtils;
import ch.boye.httpclientandroidlib.message.BasicNameValuePair;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class RequestHandler extends HandlerThread implements IRequestHandler {
private InternalHandler internalHandler;
private IPackageHandler packageHandler;
private HttpClient httpClient;
private ILogger logger;
public RequestHandler(IPackageHandler packageHandler) {
super(Constants.LOGTAG, MIN_PRIORITY);
setDaemon(true);
start();
this.logger = AdjustFactory.getLogger();
this.internalHandler = new InternalHandler(getLooper(), this);
init(packageHandler);
Message message = Message.obtain();
message.arg1 = InternalHandler.INIT;
internalHandler.sendMessage(message);
}
@Override
public void init(IPackageHandler packageHandler) {
this.packageHandler = packageHandler;
}
@Override
public void sendPackage(ActivityPackage pack) {
Message message = Message.obtain();
message.arg1 = InternalHandler.SEND;
message.obj = pack;
internalHandler.sendMessage(message);
}
@Override
public void sendClickPackage(ActivityPackage clickPackage) {
Message message = Message.obtain();
message.arg1 = InternalHandler.SEND_CLICK;
message.obj = clickPackage;
internalHandler.sendMessage(message);
}
private static final class InternalHandler extends Handler {
private static final int INIT = 72401;
private static final int SEND = 72400;
private static final int SEND_CLICK = 72402;
private final WeakReference<RequestHandler> requestHandlerReference;
protected InternalHandler(Looper looper, RequestHandler requestHandler) {
super(looper);
this.requestHandlerReference = new WeakReference<RequestHandler>(requestHandler);
}
@Override
public void handleMessage(Message message) {
super.handleMessage(message);
RequestHandler requestHandler = requestHandlerReference.get();
if (null == requestHandler) {
return;
}
switch (message.arg1) {
case INIT:
requestHandler.initInternal();
break;
case SEND:
ActivityPackage activityPackage = (ActivityPackage) message.obj;
requestHandler.sendInternal(activityPackage, true);
break;
case SEND_CLICK:
ActivityPackage clickPackage = (ActivityPackage) message.obj;
requestHandler.sendInternal(clickPackage, false);
break;
}
}
}
private void initInternal() {
httpClient = Util.getHttpClient();
}
private void sendInternal(ActivityPackage activityPackage, boolean sendToPackageHandler) {
try {
HttpUriRequest request = getRequest(activityPackage);
HttpResponse response = httpClient.execute(request);
requestFinished(response, sendToPackageHandler);
} catch (UnsupportedEncodingException e) {
sendNextPackage(activityPackage, "Failed to encode parameters", e, sendToPackageHandler);
} catch (ClientProtocolException e) {
closePackage(activityPackage, "Client protocol error", e, sendToPackageHandler);
} catch (SocketTimeoutException e) {
closePackage(activityPackage, "Request timed out", e, sendToPackageHandler);
} catch (IOException e) {
closePackage(activityPackage, "Request failed", e, sendToPackageHandler);
} catch (Throwable e) {
sendNextPackage(activityPackage, "Runtime exception", e, sendToPackageHandler);
}
}
private void requestFinished(HttpResponse response, boolean sendToPackageHandler) {
JSONObject jsonResponse = Util.parseJsonResponse(response, logger);
if (jsonResponse == null) {
if (sendToPackageHandler) {
packageHandler.closeFirstPackage();
}
return;
}
packageHandler.finishedTrackingActivity(jsonResponse);
if (sendToPackageHandler) {
packageHandler.sendNextPackage();
}
}
// close current package because it failed
private void closePackage(ActivityPackage activityPackage, String message, Throwable throwable, boolean sendToPackageHandler) {
final String packageMessage = activityPackage.getFailureMessage();
final String handlerMessage = packageHandler.getFailureMessage();
final String reasonString = getReasonString(message, throwable);
logger.error("%s. (%s) %s", packageMessage, reasonString, handlerMessage);
if (sendToPackageHandler) {
packageHandler.closeFirstPackage();
}
}
// send next package because the current package failed
private void sendNextPackage(ActivityPackage activityPackage, String message, Throwable throwable, boolean sendToPackageHandler) {
final String failureMessage = activityPackage.getFailureMessage();
final String reasonString = getReasonString(message, throwable);
logger.error("%s. (%s)", failureMessage, reasonString);
if (sendToPackageHandler) {
packageHandler.sendNextPackage();
}
}
private String getReasonString(String message, Throwable throwable) {
if (throwable != null) {
return String.format("%s: %s", message, throwable);
} else {
return String.format("%s", message);
}
}
private HttpUriRequest getRequest(ActivityPackage activityPackage) throws UnsupportedEncodingException {
String url = Constants.BASE_URL + activityPackage.getPath();
HttpPost request = new HttpPost(url);
String language = Locale.getDefault().getLanguage();
request.addHeader("Client-SDK", activityPackage.getClientSdk());
request.addHeader("Accept-Language", language);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : activityPackage.getParameters().entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pairs.add(pair);
}
long now = System.currentTimeMillis();
String dateString = Util.dateFormat(now);
NameValuePair sentAtPair = new BasicNameValuePair("sent_at", dateString);
pairs.add(sentAtPair);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
request.setEntity(entity);
return request;
}
}
|