1 package com.foxinmy.weixin4j.dispatcher;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import com.foxinmy.weixin4j.message.ImageMessage;
7 import com.foxinmy.weixin4j.message.LinkMessage;
8 import com.foxinmy.weixin4j.message.LocationMessage;
9 import com.foxinmy.weixin4j.message.TextMessage;
10 import com.foxinmy.weixin4j.message.VideoMessage;
11 import com.foxinmy.weixin4j.message.VoiceMessage;
12 import com.foxinmy.weixin4j.message.event.LocationEventMessage;
13 import com.foxinmy.weixin4j.message.event.MenuEventMessage;
14 import com.foxinmy.weixin4j.message.event.MenuLocationEventMessage;
15 import com.foxinmy.weixin4j.message.event.MenuPhotoEventMessage;
16 import com.foxinmy.weixin4j.message.event.MenuScanEventMessage;
17 import com.foxinmy.weixin4j.mp.event.KfCloseEventMessage;
18 import com.foxinmy.weixin4j.mp.event.KfCreateEventMessage;
19 import com.foxinmy.weixin4j.mp.event.KfSwitchEventMessage;
20 import com.foxinmy.weixin4j.mp.event.MassEventMessage;
21 import com.foxinmy.weixin4j.mp.event.TemplatesendjobfinishMessage;
22 import com.foxinmy.weixin4j.mp.event.VerifyExpireEventMessage;
23 import com.foxinmy.weixin4j.mp.event.VerifyFailEventMessage;
24 import com.foxinmy.weixin4j.qy.event.BatchjobresultMessage;
25 import com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage;
26 import com.foxinmy.weixin4j.request.WeixinMessage;
27 import com.foxinmy.weixin4j.type.AccountType;
28 import com.foxinmy.weixin4j.type.EventType;
29 import com.foxinmy.weixin4j.type.MessageType;
30
31
32
33
34
35
36
37
38
39
40 public class DefaultMessageMatcher implements WeixinMessageMatcher {
41
42 private final Map<WeixinMessageKey, Class<? extends WeixinMessage>> messageClassMap;
43
44 public DefaultMessageMatcher() {
45 messageClassMap = new HashMap<WeixinMessageKey, Class<? extends WeixinMessage>>();
46 initMessageClass();
47 }
48
49 private void initMessageClass() {
50
51
52
53 initGeneralMessageClass();
54
55
56
57 initEventMessageClass();
58
59
60
61 initMpEventMessageClass();
62
63
64
65 initQyEventMessageClass();
66 }
67
68 private void initGeneralMessageClass() {
69 for (AccountType accountType : AccountType.values()) {
70 messageClassMap.put(new WeixinMessageKey(MessageType.text.name(),
71 null, accountType), TextMessage.class);
72 messageClassMap.put(new WeixinMessageKey(MessageType.image.name(),
73 null, accountType), ImageMessage.class);
74 messageClassMap.put(new WeixinMessageKey(MessageType.voice.name(),
75 null, accountType), VoiceMessage.class);
76 messageClassMap.put(new WeixinMessageKey(MessageType.video.name(),
77 null, accountType), VideoMessage.class);
78 messageClassMap.put(
79 new WeixinMessageKey(MessageType.shortvideo.name(), null,
80 accountType), VideoMessage.class);
81 messageClassMap.put(
82 new WeixinMessageKey(MessageType.location.name(), null,
83 accountType), LocationMessage.class);
84 messageClassMap.put(new WeixinMessageKey(MessageType.link.name(),
85 null, accountType), LinkMessage.class);
86 }
87 }
88
89 private void initEventMessageClass() {
90 String messageType = MessageType.event.name();
91 EventType[] eventTypes = new EventType[] { EventType.subscribe,
92 EventType.unsubscribe };
93 for (EventType eventType : eventTypes) {
94 messageClassMap.put(
95 new WeixinMessageKey(messageType, eventType.name(),
96 AccountType.MP),
97 com.foxinmy.weixin4j.mp.event.ScribeEventMessage.class);
98 }
99 for (EventType eventType : eventTypes) {
100 messageClassMap.put(
101 new WeixinMessageKey(messageType, eventType.name(),
102 AccountType.QY),
103 com.foxinmy.weixin4j.qy.event.ScribeEventMessage.class);
104 }
105 for (AccountType accountType : AccountType.values()) {
106 messageClassMap.put(new WeixinMessageKey(messageType,
107 EventType.location.name(), accountType),
108 LocationEventMessage.class);
109 messageClassMap.put(new WeixinMessageKey(messageType,
110 EventType.location_select.name(), accountType),
111 MenuLocationEventMessage.class);
112 for (EventType eventType : new EventType[] { EventType.click,
113 EventType.view }) {
114 messageClassMap.put(
115 new WeixinMessageKey(messageType, eventType.name(),
116 accountType), MenuEventMessage.class);
117 }
118 for (EventType eventType : new EventType[] {
119 EventType.scancode_push, EventType.scancode_waitmsg }) {
120 messageClassMap.put(
121 new WeixinMessageKey(messageType, eventType.name(),
122 accountType), MenuScanEventMessage.class);
123 }
124 for (EventType eventType : new EventType[] {
125 EventType.pic_sysphoto, EventType.pic_photo_or_album,
126 EventType.pic_weixin }) {
127 messageClassMap.put(
128 new WeixinMessageKey(messageType, eventType.name(),
129 accountType), MenuPhotoEventMessage.class);
130 }
131 }
132 }
133
134 private void initMpEventMessageClass() {
135 String messageType = MessageType.event.name();
136 AccountType accountType = AccountType.MP;
137 messageClassMap.put(
138 new WeixinMessageKey(messageType, EventType.scan.name(),
139 accountType),
140 com.foxinmy.weixin4j.mp.event.ScanEventMessage.class);
141 messageClassMap.put(new WeixinMessageKey(messageType,
142 EventType.masssendjobfinish.name(), accountType),
143 MassEventMessage.class);
144 messageClassMap.put(new WeixinMessageKey(messageType,
145 EventType.templatesendjobfinish.name(), accountType),
146 TemplatesendjobfinishMessage.class);
147 messageClassMap.put(new WeixinMessageKey(messageType,
148 EventType.kf_create_session.name(), accountType),
149 KfCreateEventMessage.class);
150 messageClassMap.put(new WeixinMessageKey(messageType,
151 EventType.kf_close_session.name(), accountType),
152 KfCloseEventMessage.class);
153 messageClassMap.put(new WeixinMessageKey(messageType,
154 EventType.kf_switch_session.name(), accountType),
155 KfSwitchEventMessage.class);
156 EventType[] eventTypes = new EventType[] {
157 EventType.qualification_verify_success,
158 EventType.naming_verify_success, EventType.annual_renew,
159 EventType.verify_expired };
160 for (EventType eventType : eventTypes) {
161 messageClassMap.put(
162 new WeixinMessageKey(messageType, eventType.name(),
163 accountType), VerifyExpireEventMessage.class);
164 }
165 eventTypes = new EventType[] { EventType.qualification_verify_success,
166 EventType.naming_verify_fail };
167 for (EventType eventType : eventTypes) {
168 messageClassMap.put(
169 new WeixinMessageKey(messageType, eventType.name(),
170 accountType), VerifyFailEventMessage.class);
171 }
172 }
173
174 private void initQyEventMessageClass() {
175 String messageType = MessageType.event.name();
176 messageClassMap.put(new WeixinMessageKey(messageType,
177 EventType.batch_job_result.name(), AccountType.QY),
178 BatchjobresultMessage.class);
179 messageClassMap.put(new WeixinMessageKey(messageType,
180 EventType.enter_agent.name(), AccountType.QY),
181 EnterAgentEventMessage.class);
182
183
184
185 }
186
187 @Override
188 public Class<? extends WeixinMessage> match(WeixinMessageKey messageKey) {
189 return messageClassMap.get(messageKey);
190 }
191
192 @Override
193 public void regist(WeixinMessageKey messageKey,
194 Class<? extends WeixinMessage> messageClass) {
195 Class<?> clazz = match(messageKey);
196 if (clazz != null) {
197 throw new IllegalArgumentException("duplicate messagekey '"
198 + messageKey + "' define for " + clazz);
199 }
200 messageClassMap.put(messageKey, messageClass);
201 }
202 }