33import org .slf4j .Logger ;
44import org .slf4j .LoggerFactory ;
55
6+ import cn .jpush .api .im .model .BaseMessage .MsgType ;
7+
68import com .google .gson .Gson ;
9+ import com .google .gson .GsonBuilder ;
710import com .google .gson .JsonElement ;
8- import com .google .gson .JsonObject ;
9- import com .google .gson .JsonParser ;
1011import 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}
0 commit comments