├── robot-demo
├── src
│ └── main
│ │ ├── webapp
│ │ ├── index.action
│ │ └── WEB-INF
│ │ │ ├── pages
│ │ │ ├── welcome.jsp
│ │ │ └── weixin.jspx
│ │ │ ├── weixin-router.xml
│ │ │ └── web.xml
│ │ ├── resources
│ │ ├── appContext-robot.xml
│ │ ├── struts.properties
│ │ ├── struts.xml
│ │ └── struts2packages
│ │ │ └── portal-package.xml
│ │ └── java
│ │ └── org
│ │ └── mvnsearch
│ │ └── wx
│ │ └── web
│ │ └── controllers
│ │ └── PortalController.java
└── pom.xml
├── robot-sdk
├── src
│ ├── test
│ │ ├── resources
│ │ │ ├── weixin-router.xml
│ │ │ └── weixin-robot-flow.puml
│ │ └── java
│ │ │ └── org
│ │ │ └── mvnsearch
│ │ │ └── wx
│ │ │ ├── ConfTest.java
│ │ │ └── WeixinMessageTest.java
│ └── main
│ │ └── java
│ │ └── org
│ │ └── mvnsearch
│ │ └── wx
│ │ ├── rewrite
│ │ ├── RewrittenUrl.java
│ │ ├── Rule.java
│ │ ├── NormalRewrittenUrl.java
│ │ ├── UrlRewriter.java
│ │ ├── Conf.java
│ │ ├── NormalRule.java
│ │ └── RuleChain.java
│ │ ├── WeixinUtils.java
│ │ ├── servlet
│ │ ├── WeixinMessageContext.java
│ │ └── WexinRobotServlet.java
│ │ └── WeixinMessage.java
└── pom.xml
├── pom.xml
└── README.md
/robot-demo/src/main/webapp/index.action:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/robot-demo/src/main/webapp/WEB-INF/pages/welcome.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Weixin Robot
6 |
7 |
8 | Weixin Robot Demo
9 |
10 |
11 |
--------------------------------------------------------------------------------
/robot-sdk/src/test/resources/weixin-router.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | text
7 | ^\d{13}$
8 | /weixin.wx
9 |
10 |
11 |
--------------------------------------------------------------------------------
/robot-demo/src/main/resources/appContext-robot.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
--------------------------------------------------------------------------------
/robot-demo/src/main/webapp/WEB-INF/pages/weixin.jspx:
--------------------------------------------------------------------------------
1 |
2 |
3 | ${wxMsg.sender}
4 | ${wxMsg.receiver}
5 | ${wxMsg.createdTime}
6 | text
7 | ${content}
8 | 0
9 |
10 |
--------------------------------------------------------------------------------
/robot-demo/src/main/webapp/WEB-INF/weixin-router.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | text
7 | [\d]{11}
8 | /weixin2.wx
9 |
10 |
11 |
12 | text
13 | /weixin.wx
14 |
15 |
16 |
--------------------------------------------------------------------------------
/robot-sdk/src/test/resources/weixin-robot-flow.puml:
--------------------------------------------------------------------------------
1 | @startdot
2 | digraph WeixRobotFlow {
3 | WeixinServer [style=rounded]
4 | WeixinRobotServlet [shape=box]
5 | WebFramework [shape=box]
6 | MessageRouter [shape=diamond]
7 |
8 | WeixinServer -> WeixinRobotServlet [style=bold,label="Message Post"];
9 | WeixinRobotServlet -> MessageRouter [label="Regex Router"]
10 | MessageRouter -> WebFramework [label="Forward"]
11 | WebFramework -> WeixinServer [label="Response"]
12 | }
13 | @enddot
--------------------------------------------------------------------------------
/robot-demo/src/main/resources/struts.properties:
--------------------------------------------------------------------------------
1 | struts.i18n.reload = true
2 | struts.devMode = true
3 | struts.configuration.xml.reload = true
4 | struts.url.http.port = 80
5 | struts.serve.static = true
6 | struts.serve.static.browserCache = false
7 | struts.objectFactory = spring
8 | struts.objectFactory.spring.autoWire = name
9 | struts.objectFactory.spring.useClassCache = true
10 | struts.locale = zh_CN
11 | struts.i18n.encoding = UTF-8
12 | struts.ui.theme = xhtml
13 | struts.enable.DynamicMethodInvocation = true
14 | struts.custom.i18n.resources = global
15 | struts.action.extension = action,do,jsn,wx
16 | struts.multipart.maxSize = 5242880
--------------------------------------------------------------------------------
/robot-sdk/src/test/java/org/mvnsearch/wx/ConfTest.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx;
2 |
3 | import junit.framework.TestCase;
4 | import org.mvnsearch.wx.rewrite.Conf;
5 | import org.mvnsearch.wx.rewrite.Rule;
6 |
7 | /**
8 | * conf test
9 | *
10 | * @author linux_china
11 | */
12 | public class ConfTest extends TestCase {
13 |
14 | /**
15 | * test to construct conf
16 | *
17 | * @throws Exception exception
18 | */
19 | public void testConstructConf() throws Exception {
20 | Conf conf = new Conf(null, "classpath:weixin-router.xml");
21 | for (Rule rule : conf.getRules()) {
22 | System.out.println(rule);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/robot-demo/src/main/resources/struts.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/robot-demo/src/main/resources/struts2packages/portal-package.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | /WEB-INF/pages/welcome.jsp
11 | /WEB-INF/pages/weixin.jspx
12 |
13 |
14 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/RewrittenUrl.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import javax.servlet.ServletException;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 | import java.io.IOException;
7 |
8 | /**
9 | * rewritten url
10 | *
11 | * @author linux_china
12 | */
13 | public interface RewrittenUrl {
14 | /**
15 | * get target url
16 | *
17 | * @return target url
18 | */
19 | public String getTarget();
20 |
21 | /**
22 | * execute rewrite
23 | *
24 | * @param request http servlet request
25 | * @param response http servlet response
26 | * @return rewrite mark
27 | * @throws ServletException servlet exception
28 | * @throws IOException IO exception
29 | */
30 | public boolean doRewrite(HttpServletRequest request,
31 | HttpServletResponse response)
32 | throws ServletException, IOException;
33 | }
34 |
--------------------------------------------------------------------------------
/robot-sdk/src/test/java/org/mvnsearch/wx/WeixinMessageTest.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx;
2 |
3 | import junit.framework.TestCase;
4 |
5 | /**
6 | * weixin message test case
7 | *
8 | * @author linux_china
9 | */
10 | public class WeixinMessageTest extends TestCase {
11 |
12 | /**
13 | * test to prse xml
14 | *
15 | * @throws Exception exception
16 | */
17 | public void testParseXML() throws Exception {
18 | String msgXml = "\n" +
19 | "\n" +
20 | "\n" +
21 | "1365073034\n" +
22 | "\n" +
23 | "\n" +
24 | "5862944037781496076\n" +
25 | "";
26 | WeixinMessage msg = WeixinUtils.parseXML(msgXml);
27 | System.out.println(msg.getResponseXml("text", "我能帮助你什么?"));
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/Rule.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import org.jetbrains.annotations.Nullable;
4 | import org.mvnsearch.wx.WeixinMessage;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | import java.lang.reflect.InvocationTargetException;
11 |
12 | /**
13 | * rewrite rule
14 | *
15 | * @author linux_china
16 | */
17 | public interface Rule {
18 | /**
19 | * match weixin message?
20 | *
21 | * @param wxMsg weixin message
22 | * @param request http servlet request
23 | * @param response http servlet response
24 | * @return rewritten url
25 | * @throws IOException io exception
26 | * @throws ServletException servlet exception
27 | */
28 | @Nullable
29 | public RewrittenUrl matches(WeixinMessage wxMsg,
30 | final HttpServletRequest request,
31 | final HttpServletResponse response)
32 | throws IOException, ServletException;
33 | }
34 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/NormalRewrittenUrl.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import javax.servlet.ServletException;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 | import java.io.IOException;
7 |
8 | /**
9 | * normal rewrite url
10 | *
11 | * @author linux_china
12 | */
13 | public class NormalRewrittenUrl implements RewrittenUrl {
14 | /**
15 | * target forward url
16 | */
17 | private String target;
18 |
19 | /**
20 | * construct method
21 | *
22 | * @param target target url
23 | */
24 | public NormalRewrittenUrl(String target) {
25 | this.target = target;
26 | }
27 |
28 | /**
29 | * get target url
30 | *
31 | * @return target url
32 | */
33 | public String getTarget() {
34 | return this.target;
35 | }
36 |
37 | /**
38 | * execute rewrite
39 | *
40 | * @param request http servlet request
41 | * @param response http servlet response
42 | * @return rewrite mark
43 | * @throws ServletException servlet exception
44 | * @throws IOException IO exception
45 | */
46 | public boolean doRewrite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47 | request.getRequestDispatcher(getTarget()).forward(request, response);
48 | return true;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/UrlRewriter.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import org.mvnsearch.wx.WeixinMessage;
4 |
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 | import java.lang.reflect.InvocationTargetException;
10 |
11 | /**
12 | * url rewriter
13 | *
14 | * @author linux_china
15 | */
16 | public class UrlRewriter {
17 | /**
18 | * rewriter configuration
19 | */
20 | private Conf conf;
21 |
22 | /**
23 | * construct method
24 | *
25 | * @param conf rewriter configuration
26 | */
27 | public UrlRewriter(Conf conf) {
28 | this.conf = conf;
29 | }
30 |
31 |
32 | /**
33 | * Helpful for testing but otherwise, don't use.
34 | */
35 | public RewrittenUrl processRequest(WeixinMessage wxMsg,
36 | final HttpServletRequest request,
37 | final HttpServletResponse response)
38 | throws IOException, ServletException, InvocationTargetException {
39 | RuleChain chain = getNewChain(request);
40 | chain.process(wxMsg, request, response);
41 | return chain.getFinalRewrittenRequest();
42 | }
43 |
44 | /**
45 | * get new rule chain
46 | *
47 | * @param request http servlet request
48 | * @return rule chain
49 | */
50 | private RuleChain getNewChain(final HttpServletRequest request) {
51 | return new RuleChain(conf.getRules());
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/Conf.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import org.jdom.Document;
4 | import org.jdom.Element;
5 | import org.jdom.input.SAXBuilder;
6 |
7 | import javax.servlet.ServletContext;
8 | import java.io.InputStream;
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | /**
13 | * weixin robot configuration
14 | *
15 | * @author linux_china
16 | */
17 | public class Conf {
18 | /**
19 | * rules
20 | */
21 | private List rules = new ArrayList();
22 |
23 | /**
24 | * construct method
25 | *
26 | * @param path path
27 | * @throws Exception exception
28 | */
29 | @SuppressWarnings("unchecked")
30 | public Conf(ServletContext servletContext, String path) throws Exception {
31 | InputStream inputStream;
32 | if (path.startsWith("classpath:")) {
33 | inputStream = this.getClass().getResourceAsStream(path.replace("classpath:", "/"));
34 | } else {
35 | inputStream = servletContext.getResourceAsStream(path);
36 | }
37 | SAXBuilder saxBuilder = new SAXBuilder();
38 | Document document = saxBuilder.build(inputStream);
39 | List ruleElements = document.getRootElement().getChildren("rule");
40 | for (Element ruleElement : ruleElements) {
41 | rules.add(new NormalRule(ruleElement.getChildTextTrim("type"), ruleElement.getChildTextTrim("content"), (ruleElement.getChildTextTrim("forward"))));
42 | }
43 | }
44 |
45 | /**
46 | * get forward rules
47 | *
48 | * @return rules
49 | */
50 | public List getRules() {
51 | return rules;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/robot-demo/src/main/java/org/mvnsearch/wx/web/controllers/PortalController.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.web.controllers;
2 |
3 | import com.opensymphony.xwork2.ActionSupport;
4 | import org.apache.struts2.interceptor.ServletRequestAware;
5 | import org.mvnsearch.wx.WeixinMessage;
6 |
7 | import javax.servlet.http.HttpServletRequest;
8 | import java.util.Date;
9 |
10 | /**
11 | * portal controller
12 | *
13 | * @author linux_china
14 | */
15 | public class PortalController extends ActionSupport implements ServletRequestAware {
16 | /**
17 | * http servlet request
18 | */
19 | HttpServletRequest request;
20 |
21 | /**
22 | * inject http servlet request
23 | *
24 | * @param httpServletRequest http servlet request
25 | */
26 | public void setServletRequest(HttpServletRequest httpServletRequest) {
27 | this.request = httpServletRequest;
28 | }
29 |
30 | /**
31 | * project front page
32 | *
33 | * @return welcome page
34 | */
35 | public String index() {
36 | return "index";
37 | }
38 |
39 | /**
40 | * 显示微信消息
41 | *
42 | * @return weixing response
43 | */
44 | public String weixin() {
45 | WeixinMessage wxMsg = (WeixinMessage) request.getAttribute("wxMsg");
46 | if (wxMsg == null) {
47 | wxMsg = new WeixinMessage();
48 | wxMsg.setSender("jacky");
49 | wxMsg.setReceiver("leijuan");
50 | wxMsg.setContent("txt");
51 | wxMsg.setCreatedTime(new Date());
52 | request.setAttribute("wxMsg", wxMsg);
53 | }
54 | request.setAttribute("content", "echo:" + wxMsg.getContent());
55 | return "weixin";
56 | }
57 |
58 | /**
59 | * 显示微信消息2
60 | *
61 | * @return weixing response
62 | */
63 | public String weixin2() {
64 | request.setAttribute("content", "weixin2");
65 | return "weixin";
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | org.mvnsearch.wx
6 | weixin-robot
7 | pom
8 | 1.0.0-SNAPSHOT
9 |
10 | robot-sdk
11 | robot-demo
12 |
13 | Weixin Robot Java Project
14 |
15 | 微信公共平台自动回复机器人Java SDK
16 |
17 | 2013
18 |
19 | MvnSearch
20 | http://www.mvnsearch.org
21 |
22 |
23 | UTF-8
24 |
25 |
26 |
27 | linux_china
28 | Jacky Chan
29 | libing.chen@gmail.com
30 | http://weibo.com/linux2china
31 |
32 | 开发人员
33 |
34 |
35 |
36 |
37 | scm:git@github.com:linux-china/weixin-robot-java.git
38 | scm:git@github.com:linux-china/weixin-robot-java.git
39 | https://github.com/linux-china/weixin-robot-java
40 |
41 |
42 |
43 |
44 | junit
45 | junit
46 | 3.8.2
47 | test
48 |
49 |
50 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/NormalRule.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import org.jetbrains.annotations.Nullable;
4 | import org.mvnsearch.wx.WeixinMessage;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 | import java.io.IOException;
10 | import java.util.regex.Pattern;
11 |
12 | /**
13 | * normal rule
14 | *
15 | * @author linux_china
16 | */
17 | public class NormalRule implements Rule {
18 | /**
19 | * type
20 | */
21 | private String type;
22 | /**
23 | * content
24 | */
25 | private String content;
26 | /**
27 | * regex pattern
28 | */
29 | private Pattern regexPattern;
30 | /**
31 | * forward
32 | */
33 | private String forward;
34 |
35 | /**
36 | * construct method
37 | *
38 | * @param type type
39 | * @param content content
40 | * @param forward forward
41 | */
42 | public NormalRule(String type, String content, String forward) {
43 | this.type = type;
44 | this.content = content;
45 | if (content != null && !content.isEmpty()) {
46 | this.regexPattern = Pattern.compile(content);
47 | }
48 | this.forward = forward;
49 | }
50 |
51 | /**
52 | * match weixin message?
53 | *
54 | * @param wxMsg weixin message
55 | * @param request http servlet request
56 | * @param response http servlet response
57 | * @return rewritten url
58 | * @throws IOException io exception
59 | * @throws ServletException servlet exception
60 | */
61 | @Nullable
62 | public RewrittenUrl matches(WeixinMessage wxMsg, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
63 | boolean matched = false;
64 | if (type.equals(wxMsg.getType())) {
65 | matched = regexPattern == null || regexPattern.matcher(wxMsg.getContent()).find();
66 | }
67 | return matched ? new NormalRewrittenUrl(forward) : null;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/robot-sdk/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.mvnsearch.wx
7 | wx-robot-sdk
8 | 1.0.0-SNAPSHOT
9 |
10 | Weixin Robot Java SDK
11 |
12 |
13 | UTF-8
14 |
15 |
16 |
17 | linux_china
18 | Jacky Chan
19 | libing.chen@gmail.com
20 | http://weibo.com/linux2china
21 |
22 | 开发人员
23 |
24 |
25 |
26 |
27 | scm:git@github.com:linux-china/weixin-robot-java.git
28 | scm:git@github.com:linux-china/weixin-robot-java.git
29 | https://github.com/linux-china/weixin-robot-java
30 |
31 |
32 |
33 |
34 | javax.servlet
35 | servlet-api
36 | 2.5
37 | provided
38 |
39 |
40 | org.jdom
41 | jdom
42 | 1.1.3
43 |
44 |
45 | commons-codec
46 | commons-codec
47 | 1.7
48 |
49 |
50 | com.intellij
51 | annotations
52 | 12.0
53 |
54 |
55 | junit
56 | junit
57 | 3.8.1
58 | test
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/rewrite/RuleChain.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.rewrite;
2 |
3 | import org.mvnsearch.wx.WeixinMessage;
4 |
5 | import javax.servlet.ServletException;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 | import java.lang.reflect.InvocationTargetException;
10 | import java.util.List;
11 |
12 | /**
13 | * rule chain
14 | *
15 | * @author linux_china
16 | */
17 | public class RuleChain {
18 | /**
19 | * rules
20 | */
21 | private List rules;
22 | /**
23 | * target
24 | */
25 | private RewrittenUrl rewrittenUrl;
26 |
27 | /**
28 | * rule chain construct
29 | *
30 | * @param rules rules
31 | */
32 | public RuleChain(List rules) {
33 | this.rules = rules;
34 | }
35 |
36 | /**
37 | * process weixin message
38 | *
39 | * @param wxMsg weixin message
40 | * @param request http servlet request
41 | * @param response response
42 | * @throws IOException IO Exception
43 | * @throws ServletException servlet exception
44 | * @throws InvocationTargetException invocation target exception
45 | */
46 | public void process(WeixinMessage wxMsg, HttpServletRequest request, HttpServletResponse response)
47 | throws IOException, ServletException, InvocationTargetException {
48 | doRules(wxMsg, request, response);
49 | }
50 |
51 | /**
52 | * iterated match in rules
53 | *
54 | * @param wxMsg weixin message
55 | * @param request http servlet request
56 | * @param response http servlet response
57 | * @throws IOException IO Exception
58 | * @throws ServletException servlet exception
59 | */
60 | public void doRules(WeixinMessage wxMsg, HttpServletRequest request, HttpServletResponse response)
61 | throws IOException, ServletException {
62 | for (Rule rule : rules) {
63 | RewrittenUrl rewrittenUrl = rule.matches(wxMsg, request, response);
64 | if (rewrittenUrl != null) {
65 | this.rewrittenUrl = rewrittenUrl;
66 | return;
67 | }
68 | }
69 | }
70 |
71 | /**
72 | * get final rewritten request
73 | *
74 | * @return rewritten url
75 | */
76 | public RewrittenUrl getFinalRewrittenRequest() {
77 | return this.rewrittenUrl;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/WeixinUtils.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx;
2 |
3 | import org.jdom.Document;
4 | import org.jdom.Element;
5 | import org.jdom.input.SAXBuilder;
6 |
7 | import java.io.InputStream;
8 | import java.io.StringReader;
9 | import java.util.Date;
10 |
11 | /**
12 | * weixin utils
13 | *
14 | * @author linux_china
15 | */
16 | public class WeixinUtils {
17 |
18 | /**
19 | * parse xml to get weixin message
20 | *
21 | * @param xmlText xml text
22 | * @return weixin message
23 | * @throws Exception exception
24 | */
25 | public static WeixinMessage parseXML(String xmlText) throws Exception {
26 | SAXBuilder saxBuilder = new SAXBuilder();
27 | Document document = saxBuilder.build(new StringReader(xmlText));
28 | return constructMsg(document);
29 | }
30 |
31 | /**
32 | * parse xml to get weixin message
33 | *
34 | * @param inputStream xml text
35 | * @return weixin message
36 | * @throws Exception exception
37 | */
38 | public static WeixinMessage parseXML(InputStream inputStream) throws Exception {
39 | SAXBuilder saxBuilder = new SAXBuilder();
40 | Document document = saxBuilder.build(inputStream);
41 | return constructMsg(document);
42 | }
43 |
44 | /**
45 | * construct weixin message from xml document
46 | *
47 | * @param document xml document
48 | * @return weixin message
49 | */
50 | private static WeixinMessage constructMsg(Document document) {
51 | Element rootElement = document.getRootElement();
52 | WeixinMessage msg = new WeixinMessage();
53 | msg.setId(rootElement.getChildTextTrim("MsgId"));
54 | msg.setReceiver(rootElement.getChildTextTrim("ToUserName"));
55 | msg.setSender(rootElement.getChildTextTrim("FromUserName"));
56 | msg.setType(rootElement.getChildTextTrim("MsgType"));
57 | msg.setContent(rootElement.getChildTextTrim("Content"));
58 | String createTime = rootElement.getChildTextTrim("CreateTime");
59 | if (createTime != null) {
60 | msg.setCreatedTime(new Date(Long.valueOf(createTime) * 1000));
61 | }
62 | //event message
63 | if ("event".equals(msg.getType())) {
64 | String event = rootElement.getChildText("EventKey");
65 | String eventKey = rootElement.getChildText("EventKey");
66 | msg.setType(event);
67 | msg.setContent(eventKey);
68 | }
69 | return msg;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/robot-demo/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 | Weixin Robot Demo
9 |
10 |
11 | contextConfigLocation
12 |
13 | classpath:/appContext-robot.xml
14 |
15 |
16 |
17 |
18 |
19 | org.springframework.web.context.ContextLoaderListener
20 |
21 |
22 |
23 | WexinRobotServlet
24 | org.mvnsearch.wx.servlet.WexinRobotServlet
25 |
26 | token
27 | nananjingdu
28 |
29 |
30 | confFile
31 | /WEB-INF/weixin-router.xml
32 |
33 |
34 |
35 |
36 |
37 | WexinRobotServlet
38 | /servlet/weixinrobot
39 |
40 |
41 |
42 | struts
43 | org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
44 |
45 |
46 |
47 | struts
48 | *.action
49 | REQUEST
50 | FORWARD
51 |
52 |
53 | struts
54 | *.wx
55 | REQUEST
56 | FORWARD
57 |
58 |
59 | index.action
60 |
61 |
62 |
63 |
64 | *.jsp
65 | UTF-8
66 | true
67 |
68 |
69 | *.jspx
70 | UTF-8
71 | true
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/robot-sdk/src/main/java/org/mvnsearch/wx/servlet/WeixinMessageContext.java:
--------------------------------------------------------------------------------
1 | package org.mvnsearch.wx.servlet;
2 |
3 | import org.mvnsearch.wx.WeixinMessage;
4 |
5 | import java.util.Collections;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * weixin message context
11 | *
12 | * @author linux_china
13 | */
14 | @SuppressWarnings("unchecked")
15 | public class WeixinMessageContext {
16 | /**
17 | * empty map
18 | */
19 | private static Map emptyMap = Collections.emptyMap();
20 | /**
21 | * thread local variable
22 | */
23 | private final static ThreadLocal