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