Skip to content

Commit 57c1911

Browse files
author
Javen
committed
Refactored msg json structure - from and to
1 parent f29e667 commit 57c1911

File tree

9 files changed

+211
-144
lines changed

9 files changed

+211
-144
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.GsonBuilder;
8+
9+
public abstract class BaseMessage {
10+
protected static final Logger LOG = LoggerFactory.getLogger(BaseMessage.class);
11+
12+
protected static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
13+
14+
protected MsgType msgType;
15+
16+
protected BaseMessage(MsgType msgType) {
17+
this.msgType = msgType;
18+
}
19+
20+
public String toJson() {
21+
return _gson.toJson(this);
22+
}
23+
24+
25+
public static enum MsgType {
26+
text,
27+
voice,
28+
image
29+
}
30+
31+
}

src/main/java/cn/jpush/api/im/model/ImMessage.java

Lines changed: 117 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,73 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
import cn.jpush.api.im.model.BaseMessage.MsgType;
7+
68
import com.google.gson.Gson;
9+
import com.google.gson.GsonBuilder;
710
import com.google.gson.JsonElement;
8-
import com.google.gson.JsonObject;
9-
import com.google.gson.JsonParser;
1011
import com.google.gson.JsonSyntaxException;
12+
import com.google.gson.annotations.Expose;
13+
import com.google.gson.annotations.SerializedName;
1114

12-
public abstract class ImMessage {
15+
/**
16+
* Message container
17+
*
18+
*/
19+
public class ImMessage {
1320
protected static final Logger LOG = LoggerFactory.getLogger(ImMessage.class);
1421

15-
private static final String KEY_MSG_TYPE = "msg_type";
1622
private static final int CURRENT_VERSION = 1;
1723

18-
protected static Gson _gson = new Gson();
19-
protected static JsonParser _jsonParser = new JsonParser();
24+
private static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
2025

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;
26+
@Expose
27+
private int version;
28+
29+
@Expose
30+
@SerializedName("target_type")
31+
private String targetType;
32+
33+
@Expose
34+
@SerializedName("target_id")
35+
private String targetId;
36+
37+
@Expose
38+
@SerializedName("target_name")
39+
private String targetName;
40+
41+
@Expose
42+
@SerializedName("from_type")
43+
private String fromType;
44+
45+
@Expose
46+
@SerializedName("from_id")
47+
private String fromId;
48+
49+
@Expose
50+
@SerializedName("from_name")
51+
private String fromName;
52+
53+
@Expose
54+
@SerializedName("create_time")
55+
private int createTime;
56+
57+
@Expose
58+
private String extras;
3159

60+
@Expose
61+
@SerializedName("msg_type")
62+
private MsgType msgType;
63+
64+
@Expose
65+
@SerializedName("msg_body")
66+
private JsonElement msgBody;
67+
68+
private BaseMessage baseMessage;
3269

3370
protected ImMessage(String targetType, String targetId, String targetName,
3471
String fromType, String fromId, String fromName,
35-
MsgType msgType, String extras) {
72+
String extras, BaseMessage message) {
3673

3774
this.version = CURRENT_VERSION;
3875
this.createTime = (int) (System.currentTimeMillis() / 1000);
@@ -45,100 +82,105 @@ protected ImMessage(String targetType, String targetId, String targetName,
4582
this.fromId = fromId;
4683
this.fromName = fromName;
4784

48-
this.msgType = msgType;
4985
this.extras = extras;
86+
87+
this.msgType = message.msgType;
88+
this.baseMessage = message;
89+
}
90+
91+
public static Builder newBuilder() {
92+
return new Builder();
5093
}
5194

5295
public String toJson() {
96+
this.msgBody = _gson.toJsonTree(baseMessage);
5397
return _gson.toJson(this);
5498
}
5599

56100

57101
public static ImMessage fromJson(String json) throws Exception {
58-
MsgType type = null;
59-
JsonElement root = null;
102+
ImMessage imMessage = null;
60103
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-
104+
imMessage = _gson.fromJson(json, ImMessage.class);
77105
} catch (JsonSyntaxException e) {
78-
throw new Exception("Invalid json");
106+
throw new Exception("Not a valid json.");
79107
}
80108

109+
MsgType type = imMessage.msgType;
110+
JsonElement msgBody = imMessage.msgBody;
111+
112+
if (null == type || null == msgBody) {
113+
throw new Exception("msgType and msgBody should not be null.");
114+
}
115+
116+
// maybe add more param check
117+
118+
BaseMessage baseMessage = null;
119+
81120
switch (type) {
82121
case text:
83-
return _gson.fromJson(root, TextMessage.class);
122+
baseMessage = _gson.fromJson(msgBody, TextMessage.class);
123+
break;
124+
84125
case voice:
85-
return _gson.fromJson(root, VoiceMessage.class);
126+
baseMessage = _gson.fromJson(msgBody, VoiceMessage.class);
127+
break;
128+
86129
case image:
87-
return _gson.fromJson(root, ImageMessage.class);
130+
baseMessage = _gson.fromJson(msgBody, ImageMessage.class);
131+
break;
132+
88133
default:
89134
new Exception("Unknown IM message type.");
90-
return null;
91135
}
136+
137+
imMessage.baseMessage = baseMessage;
138+
return imMessage;
92139
}
93140

94141

95-
protected abstract static class Builder<T extends ImMessage, B extends Builder<T, B>> {
96-
private B theBuilder;
142+
public static class Builder {
97143

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;
144+
private String targetType;
145+
private String targetId;
146+
private String targetName;
147+
private String fromType;
148+
private String fromId;
149+
private String fromName;
150+
private String extras;
106151

107-
public Builder() {
108-
this.theBuilder = getThis();
109-
}
110-
111-
public B setTarget(String type, String id, String name) {
152+
private MsgType msgType;
153+
private BaseMessage baseMessage;
154+
155+
public Builder setTarget(String type, String id, String name) {
112156
this.targetType = type;
113157
this.targetId = id;
114158
this.targetName = name;
115-
return theBuilder;
159+
return this;
116160
}
117161

118-
public B setFrom(String type, String id, String name) {
162+
public Builder setFrom(String type, String id, String name) {
119163
this.fromType = type;
120-
this.fromName = name;
121164
this.fromId = id;
122-
return theBuilder;
165+
this.fromName = name;
166+
return this;
123167
}
124168

125-
public B setExtras(String extras) {
169+
public Builder setExtras(String extras) {
126170
this.extras = extras;
127-
return theBuilder;
128-
}
171+
return this;
172+
}
129173

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
174+
public Builder setMessage(BaseMessage message) {
175+
this.baseMessage = message;
176+
return this;
177+
}
178+
179+
public ImMessage build() {
180+
return new ImMessage(targetType, targetId, targetName,
181+
fromType, fromId, fromName,
182+
extras, baseMessage);
183+
}
142184
}
143185

144186
}

src/main/java/cn/jpush/api/im/model/ImageMessage.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package cn.jpush.api.im.model;
22

3+
import com.google.gson.annotations.Expose;
4+
35
public class ImageMessage extends MediaMessage {
4-
protected int width;
5-
protected int height;
6-
protected String imgLink;
7-
6+
@Expose private int width;
7+
@Expose private int height;
8+
@Expose private String imgLink;
89

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) {
10+
private ImageMessage(String mediaId, long mediaCrc32, String format,
11+
int width, int height, String imgLink) {
1312

14-
super(targetType, targetId, targetName,
15-
fromType, fromId, fromName,
16-
MsgType.image, extras,
17-
mediaId, mediaCrc32, format);
13+
super(MsgType.image, mediaId, mediaCrc32, format);
1814

1915
this.width = width;
2016
this.height = height;
@@ -30,6 +26,7 @@ public static class Builder extends MediaMessage.Builder<ImageMessage, ImageMess
3026
private int height;
3127
private String imgLink;
3228

29+
@Override
3330
protected Builder getThis() {
3431
return this;
3532
}
@@ -48,11 +45,8 @@ public Builder setImgLink(String imgLink) {
4845

4946
@Override
5047
public ImageMessage build() {
51-
52-
return new ImageMessage(targetType, targetId, targetName,
53-
fromType, fromId, fromName,
54-
mediaId, mediaCrc32, format,
55-
extras, width, height, imgLink);
48+
return new ImageMessage(mediaId, mediaCrc32, format,
49+
width, height, imgLink);
5650
}
5751

5852
}

src/main/java/cn/jpush/api/im/model/MediaMessage.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
package cn.jpush.api.im.model;
22

3+
import com.google.gson.annotations.Expose;
34

4-
public abstract class MediaMessage extends ImMessage {
5-
protected String mediaId;
6-
protected long mediaCrc32;
7-
protected String format;
5+
6+
public abstract class MediaMessage extends BaseMessage {
7+
@Expose protected String mediaId;
8+
@Expose protected long mediaCrc32;
9+
@Expose protected String format;
810

9-
protected MediaMessage(String targetType, String targetId, String targetName,
10-
String fromType, String fromId, String fromName,
11-
MsgType msgType, String extras,
11+
protected MediaMessage(MsgType msgType,
1212
String mediaId, long mediaCrc32, String format) {
13-
14-
super(targetType, targetId, targetName,
15-
fromType, fromId, fromName,
16-
msgType, extras);
13+
super(msgType);
1714

1815
this.mediaId = mediaId;
1916
this.mediaCrc32 = mediaCrc32;
2017
this.format = format;
2118
}
2219

2320

24-
protected static abstract class Builder<T extends MediaMessage, B extends MediaMessage.Builder<T, B>>
25-
extends ImMessage.Builder<T, B> {
21+
protected static abstract class Builder<T extends MediaMessage, B extends MediaMessage.Builder<T, B>> {
2622
private B theBuilder;
2723

2824
protected String mediaId;
2925
protected long mediaCrc32;
3026
protected String format;
3127

28+
protected abstract B getThis();
29+
30+
protected abstract T build();
31+
3232
public Builder() {
3333
this.theBuilder = getThis();
3434
}

0 commit comments

Comments
 (0)