| @@ -1,12 +1,14 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import com.google.inject.Binder; | |||
| import com.google.inject.Module; | |||
| import com.thoughtworks.xstream.XStream; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
| import java.io.InputStream; | |||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
| public class ApiTestModule implements Module { | |||
| @@ -19,13 +21,18 @@ public class ApiTestModule implements Module { | |||
| @Override | |||
| public void configure(Binder binder) { | |||
| InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | |||
| WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1); | |||
| WxCpServiceImpl wxService = new WxCpServiceImpl(); | |||
| wxService.setWxCpConfigStorage(config); | |||
| binder.bind(WxCpServiceImpl.class).toInstance(wxService); | |||
| binder.bind(WxCpConfigStorage.class).toInstance(config); | |||
| try (InputStream is1 = ClassLoader | |||
| .getSystemResourceAsStream("test-config.xml")) { | |||
| WxXmlCpInMemoryConfigStorage config = fromXml( | |||
| WxXmlCpInMemoryConfigStorage.class, is1); | |||
| WxCpServiceImpl wxService = new WxCpServiceImpl(); | |||
| wxService.setWxCpConfigStorage(config); | |||
| binder.bind(WxCpServiceImpl.class).toInstance(wxService); | |||
| binder.bind(WxCpConfigStorage.class).toInstance(config); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| @XStreamAlias("xml") | |||
| @@ -1,16 +1,17 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import org.testng.annotations.DataProvider; | |||
| import org.testng.annotations.Test; | |||
| import java.util.concurrent.ExecutionException; | |||
| import java.util.concurrent.ExecutorService; | |||
| import java.util.concurrent.Executors; | |||
| import java.util.concurrent.Future; | |||
| import org.testng.annotations.DataProvider; | |||
| import org.testng.annotations.Test; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| @Test | |||
| public class WxCpBusyRetryTest { | |||
| @@ -19,7 +20,9 @@ public class WxCpBusyRetryTest { | |||
| WxCpService service = new WxCpServiceImpl() { | |||
| @Override | |||
| protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||
| protected synchronized <T, E> T executeInternal( | |||
| RequestExecutor<T, E> executor, String uri, E data) | |||
| throws WxErrorException { | |||
| WxError error = new WxError(); | |||
| error.setErrorCode(-1); | |||
| throw new WxErrorException(error); | |||
| @@ -1,13 +1,15 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import java.util.List; | |||
| import org.testng.Assert; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import java.util.List; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| /** | |||
| * 测试部门接口 | |||
| @@ -24,11 +26,12 @@ public class WxCpDepartAPITest { | |||
| protected WxCpDepart depart; | |||
| public void testDepartCreate() throws WxErrorException { | |||
| WxCpDepart depart = new WxCpDepart(); | |||
| depart.setName("子部门" + System.currentTimeMillis()); | |||
| depart.setParentId(1); | |||
| depart.setOrder(1); | |||
| Integer departId = this.wxCpService.departCreate(depart); | |||
| WxCpDepart cpDepart = new WxCpDepart(); | |||
| cpDepart.setName("子部门" + System.currentTimeMillis()); | |||
| cpDepart.setParentId(1); | |||
| cpDepart.setOrder(1); | |||
| Integer departId = this.wxCpService.departCreate(cpDepart); | |||
| System.out.println(departId); | |||
| } | |||
| @Test(dependsOnMethods = "testDepartCreate") | |||
| @@ -1,18 +1,20 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.testng.Assert; | |||
| import org.testng.annotations.DataProvider; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| /** | |||
| * 测试多媒体文件上传下载 | |||
| @@ -31,17 +33,21 @@ public class WxCpMediaAPITest { | |||
| @Test(dataProvider = "uploadMedia") | |||
| public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { | |||
| InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); | |||
| WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream); | |||
| Assert.assertNotNull(res.getType()); | |||
| Assert.assertNotNull(res.getCreatedAt()); | |||
| Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); | |||
| try (InputStream inputStream = ClassLoader | |||
| .getSystemResourceAsStream(fileName);) { | |||
| WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, | |||
| inputStream); | |||
| Assert.assertNotNull(res.getType()); | |||
| Assert.assertNotNull(res.getCreatedAt()); | |||
| Assert.assertTrue( | |||
| res.getMediaId() != null || res.getThumbMediaId() != null); | |||
| if (res.getMediaId() != null) { | |||
| this.media_ids.add(res.getMediaId()); | |||
| } | |||
| if (res.getThumbMediaId() != null) { | |||
| this.media_ids.add(res.getThumbMediaId()); | |||
| if (res.getMediaId() != null) { | |||
| this.media_ids.add(res.getMediaId()); | |||
| } | |||
| if (res.getThumbMediaId() != null) { | |||
| this.media_ids.add(res.getThumbMediaId()); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,16 +1,22 @@ | |||
| package me.chanjar.weixin.cp.demo; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.cp.api.*; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.Map; | |||
| import org.eclipse.jetty.server.Server; | |||
| import org.eclipse.jetty.servlet.ServletHandler; | |||
| import org.eclipse.jetty.servlet.ServletHolder; | |||
| import java.io.InputStream; | |||
| import java.util.Map; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||
| import me.chanjar.weixin.cp.api.WxCpMessageHandler; | |||
| import me.chanjar.weixin.cp.api.WxCpMessageRouter; | |||
| import me.chanjar.weixin.cp.api.WxCpService; | |||
| import me.chanjar.weixin.cp.api.WxCpServiceImpl; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage; | |||
| public class WxCpDemoServer { | |||
| @@ -36,55 +42,48 @@ public class WxCpDemoServer { | |||
| server.join(); | |||
| } | |||
| private static void initWeixin() { | |||
| InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | |||
| WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1); | |||
| wxCpConfigStorage = config; | |||
| wxCpService = new WxCpServiceImpl(); | |||
| wxCpService.setWxCpConfigStorage(config); | |||
| WxCpMessageHandler handler = new WxCpMessageHandler() { | |||
| @Override | |||
| public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, | |||
| WxCpService wxCpService, WxSessionManager sessionManager) { | |||
| WxCpXmlOutTextMessage m = WxCpXmlOutMessage | |||
| .TEXT() | |||
| .content("测试加密消息") | |||
| .fromUser(wxMessage.getToUserName()) | |||
| .toUser(wxMessage.getFromUserName()) | |||
| .build(); | |||
| return m; | |||
| } | |||
| }; | |||
| WxCpMessageHandler oauth2handler = new WxCpMessageHandler() { | |||
| @Override | |||
| public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, | |||
| WxCpService wxCpService, WxSessionManager sessionManager) { | |||
| String href = "<a href=\"" + wxCpService.oauth2buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null) | |||
| + "\">测试oauth2</a>"; | |||
| return WxCpXmlOutMessage | |||
| .TEXT() | |||
| .content(href) | |||
| .fromUser(wxMessage.getToUserName()) | |||
| .toUser(wxMessage.getFromUserName()).build(); | |||
| } | |||
| }; | |||
| wxCpMessageRouter = new WxCpMessageRouter(wxCpService); | |||
| wxCpMessageRouter | |||
| .rule() | |||
| .async(false) | |||
| .content("哈哈") // 拦截内容为“哈哈”的消息 | |||
| .handler(handler) | |||
| .end() | |||
| .rule() | |||
| .async(false) | |||
| .content("oauth") | |||
| .handler(oauth2handler) | |||
| .end() | |||
| ; | |||
| private static void initWeixin() throws IOException { | |||
| try (InputStream is1 = ClassLoader | |||
| .getSystemResourceAsStream("test-config.xml")) { | |||
| WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage | |||
| .fromXml(is1); | |||
| wxCpConfigStorage = config; | |||
| wxCpService = new WxCpServiceImpl(); | |||
| wxCpService.setWxCpConfigStorage(config); | |||
| WxCpMessageHandler handler = new WxCpMessageHandler() { | |||
| @Override | |||
| public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
| Map<String, Object> context, WxCpService wxService, | |||
| WxSessionManager sessionManager) { | |||
| WxCpXmlOutTextMessage m = WxCpXmlOutMessage.TEXT().content("测试加密消息") | |||
| .fromUser(wxMessage.getToUserName()) | |||
| .toUser(wxMessage.getFromUserName()).build(); | |||
| return m; | |||
| } | |||
| }; | |||
| WxCpMessageHandler oauth2handler = new WxCpMessageHandler() { | |||
| @Override | |||
| public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, | |||
| Map<String, Object> context, WxCpService wxService, | |||
| WxSessionManager sessionManager) { | |||
| String href = "<a href=\"" | |||
| + wxService.oauth2buildAuthorizationUrl( | |||
| wxCpConfigStorage.getOauth2redirectUri(), null) | |||
| + "\">测试oauth2</a>"; | |||
| return WxCpXmlOutMessage.TEXT().content(href) | |||
| .fromUser(wxMessage.getToUserName()) | |||
| .toUser(wxMessage.getFromUserName()).build(); | |||
| } | |||
| }; | |||
| wxCpMessageRouter = new WxCpMessageRouter(wxCpService); | |||
| wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为“哈哈”的消息 | |||
| .handler(handler).end().rule().async(false).content("oauth") | |||
| .handler(oauth2handler).end(); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,5 +1,11 @@ | |||
| package me.chanjar.weixin.cp.demo; | |||
| import java.io.IOException; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||
| import me.chanjar.weixin.cp.api.WxCpMessageRouter; | |||
| @@ -8,17 +14,11 @@ import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
| import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||
| import javax.servlet.ServletException; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.io.IOException; | |||
| /** | |||
| * @author Daniel Qian | |||
| */ | |||
| public class WxCpEndpointServlet extends HttpServlet { | |||
| private static final long serialVersionUID = 1L; | |||
| protected WxCpConfigStorage wxCpConfigStorage; | |||
| protected WxCpService wxCpService; | |||
| protected WxCpMessageRouter wxCpMessageRouter; | |||
| @@ -32,7 +32,7 @@ public class WxCpEndpointServlet extends HttpServlet { | |||
| @Override | |||
| protected void service(HttpServletRequest request, HttpServletResponse response) | |||
| throws ServletException, IOException { | |||
| throws IOException { | |||
| response.setContentType("text/html;charset=utf-8"); | |||
| response.setStatus(HttpServletResponse.SC_OK); | |||
| @@ -1,16 +1,17 @@ | |||
| package me.chanjar.weixin.cp.demo; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.cp.api.WxCpService; | |||
| import java.io.IOException; | |||
| import java.util.Arrays; | |||
| import javax.servlet.ServletException; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.io.IOException; | |||
| import java.util.Arrays; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.cp.api.WxCpService; | |||
| public class WxCpOAuth2Servlet extends HttpServlet { | |||
| private static final long serialVersionUID = 1L; | |||
| protected WxCpService wxCpService; | |||
| @@ -20,7 +21,7 @@ public class WxCpOAuth2Servlet extends HttpServlet { | |||
| @Override | |||
| protected void service(HttpServletRequest request, HttpServletResponse response) | |||
| throws ServletException, IOException { | |||
| throws IOException { | |||
| response.setContentType("text/html;charset=utf-8"); | |||
| response.setStatus(HttpServletResponse.SC_OK); | |||