quartzPro/.svn/pristine/8f/8f6523d16c00dc8d8bd7fa2b812...

127 lines
4.6 KiB
Plaintext

package com.ifish.jpush.report;
import java.net.URLEncoder;
import java.util.regex.Pattern;
import com.ifish.jpush.common.ClientConfig;
import com.ifish.jpush.common.ServiceHelper;
import com.ifish.jpush.common.TimeUnit;
import com.ifish.jpush.common.connection.HttpProxy;
import com.ifish.jpush.common.connection.IHttpClient;
import com.ifish.jpush.common.connection.NativeHttpClient;
import com.ifish.jpush.common.resp.APIConnectionException;
import com.ifish.jpush.common.resp.APIRequestException;
import com.ifish.jpush.common.resp.BaseResult;
import com.ifish.jpush.common.resp.ResponseWrapper;
import com.ifish.jpush.utils.StringUtils;
public class ReportClient {
private final NativeHttpClient _httpClient;
private String _hostName;
private String _receivePath;
private String _userPath;
private String _messagePath;
public ReportClient(String masterSecret, String appKey) {
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null);
}
public ReportClient(String masterSecret, String appKey, int maxRetryTimes) {
this(masterSecret, appKey, maxRetryTimes, null);
}
public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance());
}
public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
ServiceHelper.checkBasic(appKey, masterSecret);
_hostName = (String) conf.get(ClientConfig.REPORT_HOST_NAME);
_receivePath = (String) conf.get(ClientConfig.REPORT_RECEIVE_PATH);
_userPath = (String) conf.get(ClientConfig.REPORT_USER_PATH);
_messagePath = (String) conf.get(ClientConfig.REPORT_MESSAGE_PATH);
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
_httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
}
public ReceivedsResult getReceiveds(String[] msgIdArray)
throws APIConnectionException, APIRequestException {
return getReceiveds(StringUtils.arrayToString(msgIdArray));
}
public ReceivedsResult getReceiveds(String msgIds)
throws APIConnectionException, APIRequestException {
checkMsgids(msgIds);
String url = _hostName + _receivePath + "?msg_ids=" + msgIds;
ResponseWrapper response = _httpClient.sendGet(url);
return ReceivedsResult.fromResponse(response);
}
public MessagesResult getMessages(String msgIds)
throws APIConnectionException, APIRequestException {
checkMsgids(msgIds);
String url = _hostName + _messagePath + "?msg_ids=" + msgIds;
ResponseWrapper response = _httpClient.sendGet(url);
return MessagesResult.fromResponse(response);
}
public UsersResult getUsers(TimeUnit timeUnit, String start, int duration)
throws APIConnectionException, APIRequestException {
String startEncoded = null;
try {
startEncoded = URLEncoder.encode(start, "utf-8");
} catch (Exception e) {
}
String url = _hostName + _userPath
+ "?time_unit=" + timeUnit.toString()
+ "&start=" + startEncoded + "&duration=" + duration;
ResponseWrapper response = _httpClient.sendGet(url);
return BaseResult.fromResponse(response, UsersResult.class);
}
private final static Pattern MSGID_PATTERNS = Pattern.compile("[^0-9, ]");
public static void checkMsgids(String msgIds) {
if (StringUtils.isTrimedEmpty(msgIds)) {
throw new IllegalArgumentException("msgIds param is required.");
}
if (MSGID_PATTERNS.matcher(msgIds).find()) {
throw new IllegalArgumentException("msgIds param format is incorrect. "
+ "It should be msg_id (number) which response from JPush Push API. "
+ "If there are many, use ',' as interval. ");
}
msgIds = msgIds.trim();
if (msgIds.endsWith(",")) {
msgIds = msgIds.substring(0, msgIds.length() - 1);
}
String[] splits = msgIds.split(",");
try {
for (String s : splits) {
s = s.trim();
if (!StringUtils.isEmpty(s)) {
Long.parseLong(s);
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Every msg_id should be valid Long number which splits by ','");
}
}
}