Skip to content

Commit ec0d932

Browse files
author
Javen
committed
Begin to add IM client - implement IM Message builder from/to
1 parent e6595c7 commit ec0d932

File tree

12 files changed

+578
-0
lines changed

12 files changed

+578
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package cn.jpush.api.im;
2+
3+
import cn.jpush.api.common.ServiceHelper;
4+
import cn.jpush.api.common.connection.HttpProxy;
5+
import cn.jpush.api.common.connection.IHttpClient;
6+
import cn.jpush.api.common.connection.NativeHttpClient;
7+
import cn.jpush.api.common.resp.APIConnectionException;
8+
import cn.jpush.api.common.resp.APIRequestException;
9+
import cn.jpush.api.common.resp.BaseResult;
10+
import cn.jpush.api.common.resp.ResponseWrapper;
11+
import cn.jpush.api.push.model.PushPayload;
12+
import cn.jpush.api.utils.Preconditions;
13+
import cn.jpush.api.utils.StringUtils;
14+
15+
import com.google.gson.JsonParseException;
16+
import com.google.gson.JsonParser;
17+
18+
/**
19+
* Entrance for sending Push.
20+
*
21+
* For the following parameters, you can set them by instance creation.
22+
* This action will override setting in PushPayload Optional.
23+
* * apnsProduction If not present, the default is true.
24+
* * timeToLive If not present, the default is 86400(s) (one day).
25+
*
26+
* Can be used directly.
27+
*/
28+
public class IMClient {
29+
public static final String HOST_NAME_SSL = "https://api.jpush.cn";
30+
public static final String PUSH_PATH = "/v3/push";
31+
public static final String PUSH_VALIDATE_PATH = "/v3/push/validate";
32+
33+
private final NativeHttpClient _httpClient;
34+
private JsonParser _jsonParser = new JsonParser();
35+
36+
// If not present, true by default.
37+
private boolean _apnsProduction = true;
38+
39+
// If not present, the default value is 86400(s) (one day)
40+
private long _timeToLive = 60 * 60 * 24;
41+
42+
private boolean _globalSettingEnabled = false;
43+
44+
private String _baseUrl;
45+
46+
/**
47+
* Create a Push Client.
48+
*
49+
* @param masterSecret API access secret of the appKey.
50+
* @param appKey The KEY of one application on JPush.
51+
*/
52+
public IMClient(String masterSecret, String appKey) {
53+
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
54+
}
55+
56+
public IMClient(String masterSecret, String appKey, int maxRetryTimes) {
57+
this(masterSecret, appKey, maxRetryTimes, null);
58+
}
59+
60+
/**
61+
* Create a Push Client with max retry times.
62+
*
63+
* @param masterSecret API access secret of the appKey.
64+
* @param appKey The KEY of one application on JPush.
65+
* @param maxRetryTimes max retry times
66+
*/
67+
public IMClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
68+
ServiceHelper.checkBasic(appKey, masterSecret);
69+
70+
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
71+
this._baseUrl = HOST_NAME_SSL;
72+
this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
73+
}
74+
75+
/**
76+
* Create a Push Client with global settings.
77+
*
78+
* If you want different settings from default globally, this constructor is what you needed.
79+
*
80+
* @param masterSecret API access secret of the appKey.
81+
* @param appKey The KEY of one application on JPush.
82+
* @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
83+
* @param timeToLive Global time_to_live setting. It will override PushPayload Options.
84+
*/
85+
public IMClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
86+
this(masterSecret, appKey);
87+
88+
this._apnsProduction = apnsProduction;
89+
this._timeToLive = timeToLive;
90+
this._globalSettingEnabled = true;
91+
}
92+
93+
public void setDefaults(boolean apnsProduction, long timeToLive) {
94+
this._apnsProduction = apnsProduction;
95+
this._timeToLive = timeToLive;
96+
this._globalSettingEnabled = true;
97+
}
98+
99+
public void setBaseUrl(String baseUrl) {
100+
this._baseUrl = baseUrl;
101+
}
102+
103+
public IMResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
104+
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
105+
106+
if (_globalSettingEnabled) {
107+
pushPayload.resetOptionsTimeToLive(_timeToLive);
108+
pushPayload.resetOptionsApnsProduction(_apnsProduction);
109+
}
110+
111+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, pushPayload.toString());
112+
113+
return BaseResult.fromResponse(response, IMResult.class);
114+
}
115+
116+
public IMResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
117+
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
118+
119+
if (_globalSettingEnabled) {
120+
pushPayload.resetOptionsTimeToLive(_timeToLive);
121+
pushPayload.resetOptionsApnsProduction(_apnsProduction);
122+
}
123+
124+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, pushPayload.toString());
125+
126+
return BaseResult.fromResponse(response, IMResult.class);
127+
}
128+
129+
public IMResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
130+
Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty");
131+
132+
try {
133+
_jsonParser.parse(payloadString);
134+
} catch (JsonParseException e) {
135+
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
136+
}
137+
138+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, payloadString);
139+
140+
return BaseResult.fromResponse(response, IMResult.class);
141+
}
142+
143+
public IMResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException {
144+
Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty");
145+
146+
try {
147+
_jsonParser.parse(payloadString);
148+
} catch (JsonParseException e) {
149+
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
150+
}
151+
152+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, payloadString);
153+
154+
return BaseResult.fromResponse(response, IMResult.class);
155+
}
156+
157+
158+
}
159+
160+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.jpush.api.im;
2+
3+
import cn.jpush.api.common.resp.BaseResult;
4+
5+
import com.google.gson.annotations.Expose;
6+
7+
public class IMResult extends BaseResult {
8+
9+
@Expose public long msg_id;
10+
@Expose public int sendno;
11+
12+
}
13+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.jpush.api.im.model;
2+
3+
public class GroupInfo {
4+
5+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package cn.jpush.api.im.model;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonParser;
10+
import com.google.gson.JsonSyntaxException;
11+
12+
public abstract class ImMessage {
13+
protected static final Logger LOG = LoggerFactory.getLogger(ImMessage.class);
14+
15+
private static final String KEY_MSG_TYPE = "msg_type";
16+
private static final int CURRENT_VERSION = 1;
17+
18+
protected static Gson _gson = new Gson();
19+
protected static JsonParser _jsonParser = new JsonParser();
20+
21+
protected int version;
22+
protected String targetType;
23+
protected String targetId;
24+
protected String targetName;
25+
protected String fromType;
26+
protected String fromId;
27+
protected String fromName;
28+
protected int createTime;
29+
protected String extras;
30+
protected MsgType msgType;
31+
32+
33+
protected ImMessage(String targetType, String targetId, String targetName,
34+
String fromType, String fromId, String fromName,
35+
MsgType msgType, String extras) {
36+
37+
this.version = CURRENT_VERSION;
38+
this.createTime = (int) (System.currentTimeMillis() / 1000);
39+
40+
this.targetType = targetType;
41+
this.targetId = targetId;
42+
this.targetName = targetName;
43+
44+
this.fromType = fromType;
45+
this.fromId = fromId;
46+
this.fromName = fromName;
47+
48+
this.msgType = msgType;
49+
this.extras = extras;
50+
}
51+
52+
public String toJson() {
53+
return _gson.toJson(this);
54+
}
55+
56+
57+
public static ImMessage fromJson(String json) throws Exception {
58+
MsgType type = null;
59+
JsonElement root = null;
60+
try {
61+
root = _jsonParser.parse(json);
62+
if (!root.isJsonObject()) {
63+
throw new Exception("The msg json root should be a JsonObject.");
64+
}
65+
JsonObject rootOjbect = root.getAsJsonObject();
66+
Object typeObject = rootOjbect.get(KEY_MSG_TYPE);
67+
if (null == typeObject) {
68+
throw new Exception("Invalid IM msg json - msg_type is required.");
69+
}
70+
71+
String typeString = rootOjbect.get(KEY_MSG_TYPE).getAsString();
72+
type = MsgType.valueOf(typeString);
73+
if (null == type) {
74+
throw new Exception("Invalid IM message - unknown msg_type - " + typeString);
75+
}
76+
77+
} catch (JsonSyntaxException e) {
78+
throw new Exception("Invalid json");
79+
}
80+
81+
switch (type) {
82+
case text:
83+
return _gson.fromJson(root, TextMessage.class);
84+
case voice:
85+
return _gson.fromJson(root, VoiceMessage.class);
86+
case image:
87+
return _gson.fromJson(root, ImageMessage.class);
88+
default:
89+
new Exception("Unknown IM message type.");
90+
return null;
91+
}
92+
}
93+
94+
95+
protected abstract static class Builder<T extends ImMessage, B extends Builder<T, B>> {
96+
private B theBuilder;
97+
98+
protected String targetType;
99+
protected String targetId;
100+
protected String targetName;
101+
protected String fromType;
102+
protected String fromId;
103+
protected String fromName;
104+
protected String extras;
105+
protected MsgType msgType;
106+
107+
public Builder() {
108+
this.theBuilder = getThis();
109+
}
110+
111+
public B setTarget(String type, String id, String name) {
112+
this.targetType = type;
113+
this.targetId = id;
114+
this.targetName = name;
115+
return theBuilder;
116+
}
117+
118+
public B setFrom(String type, String id, String name) {
119+
this.fromType = type;
120+
this.fromName = name;
121+
this.fromId = id;
122+
return theBuilder;
123+
}
124+
125+
public B setExtras(String extras) {
126+
this.extras = extras;
127+
return theBuilder;
128+
}
129+
130+
public abstract T build();
131+
protected abstract B getThis();
132+
}
133+
134+
135+
136+
137+
138+
public enum MsgType {
139+
text,
140+
voice,
141+
image
142+
}
143+
144+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.jpush.api.im.model;
2+
3+
public class ImageMessage extends MediaMessage {
4+
protected int width;
5+
protected int height;
6+
protected String imgLink;
7+
8+
9+
private ImageMessage(String targetType, String targetId, String targetName,
10+
String fromType, String fromId, String fromName,
11+
String mediaId, long mediaCrc32, String format,
12+
String extras, int width, int height, String imgLink) {
13+
14+
super(targetType, targetId, targetName,
15+
fromType, fromId, fromName,
16+
MsgType.image, extras,
17+
mediaId, mediaCrc32, format);
18+
19+
this.width = width;
20+
this.height = height;
21+
this.imgLink = imgLink;
22+
}
23+
24+
public static Builder newBuilder() {
25+
return new Builder();
26+
}
27+
28+
public static class Builder extends MediaMessage.Builder<ImageMessage, ImageMessage.Builder> {
29+
private int width;
30+
private int height;
31+
private String imgLink;
32+
33+
protected Builder getThis() {
34+
return this;
35+
}
36+
37+
public Builder setWidthHeight(int width, int height) {
38+
this.width = width;
39+
this.height = height;
40+
return this;
41+
}
42+
43+
public Builder setImgLink(String imgLink) {
44+
this.imgLink = imgLink;
45+
return this;
46+
}
47+
48+
49+
@Override
50+
public ImageMessage build() {
51+
52+
return new ImageMessage(targetType, targetId, targetName,
53+
fromType, fromId, fromName,
54+
mediaId, mediaCrc32, format,
55+
extras, width, height, imgLink);
56+
}
57+
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)