在企业号工程上新增netty服务&消息分发
This commit is contained in:
-51
@@ -1,51 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
|
||||
import com.foxinmy.weixin4j.model.BaseMsg;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
import com.foxinmy.weixin4j.xml.XStream;
|
||||
|
||||
/**
|
||||
* 继承的类需实现execute(M inMessage)
|
||||
*
|
||||
* @className AbstractAction
|
||||
* @author jy
|
||||
* @date 2014年10月12日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.mp.action.WeixinAction
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class AbstractAction<M extends BaseMsg> implements
|
||||
WeixinAction {
|
||||
|
||||
public abstract ResponseMessage execute(M inMessage);
|
||||
|
||||
@Override
|
||||
public ResponseMessage execute(String msg) throws DocumentException {
|
||||
BaseMsg message = MessageUtil.xml2msg(msg);
|
||||
if (message == null) {
|
||||
Class<M> messageClass = getGenericType();
|
||||
XStream xstream = XStream.get();
|
||||
xstream.processAnnotations(messageClass);
|
||||
xstream.alias("xml", messageClass);
|
||||
return execute(xstream.fromXML(msg, messageClass));
|
||||
}
|
||||
return execute((M) message);
|
||||
}
|
||||
|
||||
private Class<M> getGenericType() {
|
||||
Class<M> clazz = null;
|
||||
Type type = getClass().getGenericSuperclass();
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType ptype = ((ParameterizedType) type);
|
||||
Type[] args = ptype.getActualTypeArguments();
|
||||
clazz = (Class<M>) args[0];
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.model.BaseMsg;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
|
||||
/**
|
||||
* 回复一个空字符串 而不是一个XML结构体中content字段的内容为空
|
||||
*
|
||||
* @className BlankAction
|
||||
* @author jy.hu
|
||||
* @date 2014年10月2日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.mp.action.AbstractAction
|
||||
*/
|
||||
public class BlankAction<M extends BaseMsg> extends AbstractAction<M> {
|
||||
|
||||
@Override
|
||||
public ResponseMessage execute(M inMessage) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.model.BaseMsg;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.msg.model.Text;
|
||||
|
||||
/**
|
||||
* 调试输出用户消息
|
||||
*
|
||||
* @className DebugAction
|
||||
* @author jy
|
||||
* @date 2014年10月8日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public abstract class DebugAction<M extends BaseMsg> extends AbstractAction<M> {
|
||||
|
||||
@Override
|
||||
public ResponseMessage execute(M message) {
|
||||
return new ResponseMessage(new Text(message.toString()), message);
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.ImageMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
@@ -13,7 +14,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.ImageMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.image)
|
||||
@ActionAnnotation(msgType = MessageType.image)
|
||||
public class ImageAction extends DebugAction<ImageMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.LinkMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
@@ -13,7 +14,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.LinkMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.link)
|
||||
@ActionAnnotation(msgType = MessageType.link)
|
||||
public class LinkAction extends DebugAction<LinkMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.LocationMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
@@ -13,7 +14,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.LocationMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.location)
|
||||
@ActionAnnotation(msgType = MessageType.location)
|
||||
public class LocationAction extends DebugAction<LocationMessage> {
|
||||
|
||||
}
|
||||
|
||||
+4
-3
@@ -1,9 +1,10 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.action.AbstractAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.TextMessage;
|
||||
import com.foxinmy.weixin4j.msg.model.Text;
|
||||
import com.foxinmy.weixin4j.response.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
/**
|
||||
@@ -15,7 +16,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.TextMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.text)
|
||||
@ActionAnnotation(msgType = MessageType.text)
|
||||
public class TextAction extends AbstractAction<TextMessage> {
|
||||
|
||||
@Override
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.VideoMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
@@ -13,7 +14,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.VideoMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.video)
|
||||
@ActionAnnotation(msgType = MessageType.video)
|
||||
public class VideoAction extends DebugAction<VideoMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.VoiceMessage;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
@@ -13,7 +14,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.VoiceMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.voice)
|
||||
@ActionAnnotation(msgType = MessageType.voice)
|
||||
public class VoiceAction extends DebugAction<VoiceMessage> {
|
||||
|
||||
}
|
||||
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.action;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
|
||||
/**
|
||||
* 消息处理接口
|
||||
*
|
||||
* @className Action
|
||||
* @author jy.hu
|
||||
* @date 2014年10月2日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.mp.action.AbstractAction
|
||||
* @see com.foxinmy.weixin4j.mp.action.BlankAction
|
||||
* @see com.foxinmy.weixin4j.mp.action.DebugAction
|
||||
*/
|
||||
public interface WeixinAction {
|
||||
public ResponseMessage execute(String inMsg) throws DocumentException;
|
||||
}
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.LocationEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.LocationEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.location })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.location })
|
||||
public class LocationAction extends DebugAction<LocationEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.MassEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.MassEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.massendjobfinish })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.massendjobfinish })
|
||||
public class MassSendAction extends DebugAction<MassEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.menu.MenuEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.menu.MenuEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.click })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.click })
|
||||
public class MenuClickAction extends DebugAction<MenuEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.menu.MenuLocationEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.menu.MenuLocationEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.location_select })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.location_select })
|
||||
public class MenuLocationAction extends DebugAction<MenuLocationEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.menu.MenuPhotoEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.menu.MenuPhotoEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = {
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = {
|
||||
EventType.pic_photo_or_album, EventType.pic_sysphoto,
|
||||
EventType.pic_weixin })
|
||||
public class MenuPhotoAction extends DebugAction<MenuPhotoEventMessage> {
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.menu.MenuScanEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.menu.MenuScanEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.scancode_push,
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.scancode_push,
|
||||
EventType.scancode_waitmsg })
|
||||
public class MenuScanAction extends DebugAction<MenuScanEventMessage> {
|
||||
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.menu.MenuEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.menu.MenuEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.view })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.view })
|
||||
public class MenuViewAction extends DebugAction<MenuEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.ScanEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.ScanEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.scan })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.scan })
|
||||
public class ScanAction extends DebugAction<ScanEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.ScribeEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.ScribeEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.subscribe })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.subscribe })
|
||||
public class SubscribeAction extends DebugAction<ScribeEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.TemplatesendjobfinishMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.TemplatesendjobfinishMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.templatesendjobfinish })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.templatesendjobfinish })
|
||||
public class TemplateSendAction extends
|
||||
DebugAction<TemplatesendjobfinishMessage> {
|
||||
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.action.event;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.Action;
|
||||
import com.foxinmy.weixin4j.action.DebugAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
|
||||
import com.foxinmy.weixin4j.msg.event.ScribeEventMessage;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.type.MessageType;
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.msg.event.ScribeEventMessage
|
||||
*/
|
||||
@Action(msgType = MessageType.event, eventType = { EventType.unsubscribe })
|
||||
@ActionAnnotation(msgType = MessageType.event, eventType = { EventType.unsubscribe })
|
||||
public class UnsubscribeAction extends DebugAction<ScribeEventMessage> {
|
||||
|
||||
}
|
||||
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.mapping;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
/**
|
||||
* 获取默认的Mapping Key 如text,event_click
|
||||
*
|
||||
* @className AbstractActionMapping
|
||||
* @author jy
|
||||
* @date 2014年10月28日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public abstract class AbstractActionMapping implements ActionMapping {
|
||||
|
||||
protected String getMappingKey(String xmlMsg) throws DocumentException {
|
||||
Document doc = DocumentHelper.parseText(xmlMsg);
|
||||
String msgType = doc.selectSingleNode("/xml/MsgType").getStringValue();
|
||||
if (msgType.equalsIgnoreCase(MessageType.event.name())) {
|
||||
msgType += "_"
|
||||
+ doc.selectSingleNode("/xml/Event").getStringValue();
|
||||
}
|
||||
return msgType.toLowerCase();
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.mapping;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
|
||||
/**
|
||||
* 标注Action类来处理消息请求
|
||||
* @className Action
|
||||
* @author jy
|
||||
* @date 2014年10月12日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Action {
|
||||
|
||||
MessageType msgType();
|
||||
|
||||
EventType[] eventType() default {};
|
||||
}
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.mapping;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.WeixinAction;
|
||||
|
||||
/**
|
||||
* 可扩展的Mapping接口
|
||||
*
|
||||
* @className ActionMapping
|
||||
* @author jy
|
||||
* @date 2014年10月28日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public interface ActionMapping {
|
||||
public WeixinAction getAction(String xmlMsg) throws DocumentException;
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.mapping;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.dom4j.DocumentException;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.WeixinAction;
|
||||
import com.foxinmy.weixin4j.type.EventType;
|
||||
import com.foxinmy.weixin4j.type.MessageType;
|
||||
import com.foxinmy.weixin4j.util.ClassUtil;
|
||||
|
||||
/**
|
||||
* 注解实现的Mapping
|
||||
*
|
||||
* @className AnnotationActionMapping
|
||||
* @author jy
|
||||
* @date 2014年10月28日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.mp.mapping.Action
|
||||
*/
|
||||
public class AnnotationActionMapping extends AbstractActionMapping {
|
||||
private final Map<String, WeixinAction> actionMap;
|
||||
|
||||
public AnnotationActionMapping() {
|
||||
actionMap = new HashMap<String, WeixinAction>();
|
||||
Set<Class<?>> weixinActions = ClassUtil.getClasses(WeixinAction.class
|
||||
.getPackage());
|
||||
for (Class<?> clazz : weixinActions) {
|
||||
Action action = clazz.getAnnotation(Action.class);
|
||||
if (action == null) {
|
||||
continue;
|
||||
}
|
||||
WeixinAction weixinAction = null;
|
||||
try {
|
||||
weixinAction = (WeixinAction) clazz.newInstance();
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
MessageType msgType = action.msgType();
|
||||
EventType[] eventTypes = action.eventType();
|
||||
if (eventTypes != null && eventTypes.length > 0) {
|
||||
for (EventType e : eventTypes) {
|
||||
actionMap.put(
|
||||
(msgType.name() + "_" + e.name()).toLowerCase(),
|
||||
weixinAction);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
actionMap.put(msgType.name().toLowerCase(), weixinAction);
|
||||
}
|
||||
}
|
||||
|
||||
public WeixinAction getAction(String xmlMsg) throws DocumentException {
|
||||
String key = getMappingKey(xmlMsg);
|
||||
return actionMap.get(key);
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
action与消息的mapping实现
|
||||
-133
@@ -1,133 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.type.EncryptType;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("xml")
|
||||
public class HttpWeixinMessage implements Serializable {
|
||||
private static final long serialVersionUID = -9157395300510879866L;
|
||||
|
||||
// 以下字段是加密方式为「安全模式」时的参数
|
||||
@XStreamAlias("ToUserName")
|
||||
private String toUserName;
|
||||
@XStreamAlias("Encrypt")
|
||||
private String encryptContent;
|
||||
private EncryptType encryptType;
|
||||
private String msgSignature;
|
||||
|
||||
// 以下字段每次被动消息时都会带上
|
||||
private String echoStr;
|
||||
private String timeStamp;
|
||||
private String nonce;
|
||||
private String signature;
|
||||
|
||||
private String token;
|
||||
|
||||
// xml消息主体
|
||||
private String xmlContent;
|
||||
|
||||
// request method
|
||||
private HttpMethod method;
|
||||
|
||||
public String getToUserName() {
|
||||
return toUserName;
|
||||
}
|
||||
|
||||
public void setToUserName(String toUserName) {
|
||||
this.toUserName = toUserName;
|
||||
}
|
||||
|
||||
public String getEncryptContent() {
|
||||
return encryptContent;
|
||||
}
|
||||
|
||||
public void setEncryptContent(String encryptContent) {
|
||||
this.encryptContent = encryptContent;
|
||||
}
|
||||
|
||||
public EncryptType getEncryptType() {
|
||||
return encryptType;
|
||||
}
|
||||
|
||||
public void setEncryptType(EncryptType encryptType) {
|
||||
this.encryptType = encryptType;
|
||||
}
|
||||
|
||||
public String getMsgSignature() {
|
||||
return msgSignature;
|
||||
}
|
||||
|
||||
public void setMsgSignature(String msgSignature) {
|
||||
this.msgSignature = msgSignature;
|
||||
}
|
||||
|
||||
public String getEchoStr() {
|
||||
return echoStr;
|
||||
}
|
||||
|
||||
public void setEchoStr(String echoStr) {
|
||||
this.echoStr = echoStr;
|
||||
}
|
||||
|
||||
public String getTimeStamp() {
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
public void setTimeStamp(String timeStamp) {
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
|
||||
public String getNonce() {
|
||||
return nonce;
|
||||
}
|
||||
|
||||
public void setNonce(String nonce) {
|
||||
this.nonce = nonce;
|
||||
}
|
||||
|
||||
public String getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
public void setSignature(String signature) {
|
||||
this.signature = signature;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getXmlContent() {
|
||||
return xmlContent;
|
||||
}
|
||||
|
||||
public void setXmlContent(String xmlContent) {
|
||||
this.xmlContent = xmlContent;
|
||||
}
|
||||
|
||||
public HttpMethod getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(HttpMethod method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HttpMessage [toUserName=" + toUserName + ", encryptContent="
|
||||
+ encryptContent + ", encryptType=" + encryptType
|
||||
+ ", msgSignature=" + msgSignature + ", echoStr=" + echoStr
|
||||
+ ", timeStamp=" + timeStamp + ", nonce=" + nonce
|
||||
+ ", signature=" + signature + ", token=" + token
|
||||
+ ", xmlContent=" + xmlContent + ", method=" + method + "]";
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -14,8 +14,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.HttpWeixinMessage;
|
||||
import com.foxinmy.weixin4j.mp.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.response.HttpWeixinMessage;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
import com.foxinmy.weixin4j.xml.XStream;
|
||||
@@ -43,7 +43,7 @@ public class WeixinMessageDecoder extends
|
||||
if (StringUtils.isNotBlank(xmlContent)) {
|
||||
message = XStream.get(xmlContent, HttpWeixinMessage.class);
|
||||
}
|
||||
message.setMethod(req.getMethod());
|
||||
message.setMethod(req.getMethod().name());
|
||||
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri(),
|
||||
true);
|
||||
log.info("\n=================receive request=================");
|
||||
|
||||
+6
-9
@@ -2,7 +2,6 @@ package com.foxinmy.weixin4j.mp.server;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||
import io.netty.handler.codec.http.HttpResponse;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -12,8 +11,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.mp.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.response.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.DateUtil;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
@@ -33,7 +32,8 @@ import com.thoughtworks.xstream.mapper.DefaultMapper;
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E5%85%A5%E6%8C%87%E5%BC%95">加密接入指引</a>
|
||||
*/
|
||||
public class WeixinMessageEncoder extends MessageToMessageEncoder<ResponseMessage> {
|
||||
public class WeixinMessageEncoder extends
|
||||
MessageToMessageEncoder<ResponseMessage> {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
protected final static XStream mapXstream = XStream.get();
|
||||
static {
|
||||
@@ -51,8 +51,8 @@ public class WeixinMessageEncoder extends MessageToMessageEncoder<ResponseMessag
|
||||
String timestamp = DateUtil.timestamp2string();
|
||||
String encrtypt = MessageUtil.aesEncrypt(mpAccount.getId(),
|
||||
mpAccount.getEncodingAesKey(), xmlContent);
|
||||
String msgSignature = MessageUtil.signature(mpAccount.getToken(), nonce,
|
||||
timestamp, encrtypt);
|
||||
String msgSignature = MessageUtil.signature(mpAccount.getToken(),
|
||||
nonce, timestamp, encrtypt);
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("Encrypt", encrtypt);
|
||||
map.put("MsgSignature", msgSignature);
|
||||
@@ -60,12 +60,9 @@ public class WeixinMessageEncoder extends MessageToMessageEncoder<ResponseMessag
|
||||
map.put("Nonce", nonce);
|
||||
|
||||
String content = mapXstream.toXML(map);
|
||||
HttpResponse httpResponse = HttpUtil
|
||||
.createWeixinMessageResponse(content);
|
||||
out.add(httpResponse);
|
||||
out.add(HttpUtil.createWeixinMessageResponse(content, null));
|
||||
|
||||
log.info("\n=================aes encrtypt out=================");
|
||||
log.info("{}", map);
|
||||
log.info("{}", content);
|
||||
}
|
||||
}
|
||||
+16
-18
@@ -4,23 +4,24 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import io.netty.handler.codec.http.HttpResponse;
|
||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||
import io.netty.handler.codec.http.HttpVersion;
|
||||
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.action.WeixinAction;
|
||||
import com.foxinmy.weixin4j.mp.mapping.ActionMapping;
|
||||
import com.foxinmy.weixin4j.mp.message.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.mp.model.HttpWeixinMessage;
|
||||
import com.foxinmy.weixin4j.mp.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.action.WeixinAction;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionMapping;
|
||||
import com.foxinmy.weixin4j.mp.util.HttpUtil;
|
||||
import com.foxinmy.weixin4j.response.HttpWeixinMessage;
|
||||
import com.foxinmy.weixin4j.response.ResponseMessage;
|
||||
import com.foxinmy.weixin4j.type.EncryptType;
|
||||
import com.foxinmy.weixin4j.util.MessageUtil;
|
||||
|
||||
/**
|
||||
* 微信被动消息处理类
|
||||
*
|
||||
* @className WeixinServerHandler
|
||||
* @author jy
|
||||
* @date 2014年11月16日
|
||||
@@ -53,16 +54,15 @@ public class WeixinServerHandler extends
|
||||
HttpWeixinMessage httpMessage) throws Exception {
|
||||
log.info("\n=================message in=================\n{}",
|
||||
httpMessage);
|
||||
boolean isGet = httpMessage.getMethod().equals(HttpMethod.GET.name());
|
||||
boolean validate = false;
|
||||
if (httpMessage.getMethod() == HttpMethod.GET
|
||||
|| httpMessage.getEncryptType() == EncryptType.RAW) {
|
||||
if (isGet || httpMessage.getEncryptType() == EncryptType.RAW) {
|
||||
validate = MessageUtil.signature(httpMessage.getToken(),
|
||||
httpMessage.getTimeStamp(), httpMessage.getNonce()).equals(
|
||||
httpMessage.getSignature());
|
||||
if (httpMessage.getMethod() == HttpMethod.GET && validate) {
|
||||
HttpResponse httpResponse = HttpUtil
|
||||
.createWeixinMessageResponse(httpMessage.getEchoStr());
|
||||
ctx.write(httpResponse);
|
||||
if (isGet && validate) {
|
||||
ctx.write(HttpUtil.createWeixinMessageResponse(
|
||||
httpMessage.getEchoStr(), ContentType.TEXT_PLAIN));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@@ -88,15 +88,13 @@ public class WeixinServerHandler extends
|
||||
log.info("\n=================message out=================\n{}",
|
||||
response);
|
||||
if (response == null) {
|
||||
HttpResponse httpResponse = HttpUtil
|
||||
.createWeixinMessageResponse("");
|
||||
ctx.write(httpResponse);
|
||||
ctx.write(HttpUtil.createWeixinMessageResponse("",
|
||||
ContentType.TEXT_PLAIN));
|
||||
return;
|
||||
}
|
||||
if (httpMessage.getEncryptType() == EncryptType.RAW) {
|
||||
HttpResponse httpResponse = HttpUtil
|
||||
.createWeixinMessageResponse(response.toXml());
|
||||
ctx.write(httpResponse);
|
||||
ctx.write(HttpUtil.createWeixinMessageResponse(response.toXml(),
|
||||
null));
|
||||
} else {
|
||||
ctx.write(response);
|
||||
}
|
||||
|
||||
+5
-3
@@ -6,15 +6,17 @@ import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||
import io.netty.handler.codec.http.HttpServerCodec;
|
||||
|
||||
import com.foxinmy.weixin4j.mp.mapping.ActionMapping;
|
||||
import com.foxinmy.weixin4j.mp.mapping.AnnotationActionMapping;
|
||||
import com.foxinmy.weixin4j.action.mapping.ActionMapping;
|
||||
import com.foxinmy.weixin4j.action.mapping.AnnotationActionMapping;
|
||||
import com.foxinmy.weixin4j.mp.action.ImageAction;
|
||||
|
||||
public class WeixinServerInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
private final ActionMapping actionMapping;
|
||||
|
||||
public WeixinServerInitializer() {
|
||||
this.actionMapping = new AnnotationActionMapping();
|
||||
this.actionMapping = new AnnotationActionMapping(
|
||||
ImageAction.class.getPackage());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-1
@@ -16,6 +16,7 @@ import io.netty.handler.codec.http.HttpResponse;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
||||
/**
|
||||
* HTTP工具类
|
||||
@@ -28,7 +29,11 @@ import org.apache.http.Consts;
|
||||
*/
|
||||
public class HttpUtil {
|
||||
|
||||
public static HttpResponse createWeixinMessageResponse(String content) {
|
||||
public static HttpResponse createWeixinMessageResponse(String content,
|
||||
ContentType contentType) {
|
||||
if (contentType == null) {
|
||||
contentType = ContentType.APPLICATION_XML;
|
||||
}
|
||||
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1,
|
||||
OK, Unpooled.copiedBuffer(content, Consts.UTF_8));
|
||||
httpResponse.headers().set(CONTENT_TYPE,
|
||||
|
||||
Reference in New Issue
Block a user