code clean up for cp module

This commit is contained in:
BinaryWang
2016-08-02 19:49:27 +08:00
parent f7a64a7cf6
commit d05da579a8
58 changed files with 1069 additions and 1027 deletions

View File

@@ -7,8 +7,8 @@ import java.io.File;
/**
* 微信客户端配置存储
* @author Daniel Qian
*
* @author Daniel Qian
*/
public interface WxCpConfigStorage {
@@ -36,6 +36,7 @@ public interface WxCpConfigStorage {
/**
* 应该是线程安全的
*
* @param jsapiTicket
*/
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
@@ -66,6 +67,7 @@ public interface WxCpConfigStorage {
/**
* http client builder
*
* @return ApacheHttpClientBuilder
*/
ApacheHttpClientBuilder getApacheHttpClientBuilder();

View File

@@ -7,8 +7,8 @@ import java.io.File;
/**
* 基于内存的微信配置provider在实际生产环境中应该将这些配置持久化
* @author Daniel Qian
*
* @author Daniel Qian
*/
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
@@ -39,6 +39,10 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
return this.accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public boolean isAccessTokenExpired() {
return System.currentTimeMillis() > this.expiresTime;
}
@@ -91,30 +95,34 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
return this.corpId;
}
public String getCorpSecret() {
return this.corpSecret;
}
public String getToken() {
return this.token;
}
public long getExpiresTime() {
return this.expiresTime;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
public String getCorpSecret() {
return this.corpSecret;
}
public void setCorpSecret(String corpSecret) {
this.corpSecret = corpSecret;
}
public String getToken() {
return this.token;
}
public void setToken(String token) {
this.token = token;
}
public long getExpiresTime() {
return this.expiresTime;
}
public void setExpiresTime(long expiresTime) {
this.expiresTime = expiresTime;
}
public String getAesKey() {
return aesKey;
}
@@ -123,14 +131,6 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
this.aesKey = aesKey;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public void setExpiresTime(long expiresTime) {
this.expiresTime = expiresTime;
}
public String getAgentId() {
return agentId;
}

View File

@@ -1,7 +1,6 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;

View File

@@ -1,13 +1,13 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
import me.chanjar.weixin.common.session.InternalSession;
import me.chanjar.weixin.common.session.InternalSessionManager;
import me.chanjar.weixin.common.session.StandardSessionManager;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.LogExceptionHandler;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.slf4j.Logger;
@@ -45,15 +45,13 @@ import java.util.concurrent.Future;
* router.route(message);
*
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public class WxCpMessageRouter {
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
private final List<WxCpMessageRouterRule> rules = new ArrayList<WxCpMessageRouterRule>();
private final WxCpService wxCpService;
@@ -79,6 +77,7 @@ public class WxCpMessageRouter {
* 设置自定义的 {@link ExecutorService}
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
* </pre>
*
* @param executorService
*/
public void setExecutorService(ExecutorService executorService) {
@@ -90,6 +89,7 @@ public class WxCpMessageRouter {
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker}
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker}
* </pre>
*
* @param messageDuplicateChecker
*/
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
@@ -101,6 +101,7 @@ public class WxCpMessageRouter {
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager}
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
* </pre>
*
* @param sessionManager
*/
public void setSessionManager(WxSessionManager sessionManager) {
@@ -112,6 +113,7 @@ public class WxCpMessageRouter {
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler}
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler}
* </pre>
*
* @param exceptionHandler
*/
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
@@ -131,6 +133,7 @@ public class WxCpMessageRouter {
/**
* 处理微信消息
*
* @param wxMessage
*/
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
@@ -216,6 +219,7 @@ public class WxCpMessageRouter {
/**
* 对session的访问结束
*
* @param wxMessage
*/
protected void sessionEndAccess(WxCpXmlMessage wxMessage) {

View File

@@ -1,8 +1,8 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
@@ -186,7 +186,6 @@ public class WxCpMessageRouterRule {
/**
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
*
*/
public WxCpMessageRouter end() {
this.routerBuilder.getRules().add(this);
@@ -195,7 +194,6 @@ public class WxCpMessageRouterRule {
/**
* 规则结束,但是消息还会进入其他规则
*
*/
public WxCpMessageRouter next() {
this.reEnter = true;

View File

@@ -1,10 +1,5 @@
package me.chanjar.weixin.cp.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
@@ -17,6 +12,11 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 微信API的Service
*/
@@ -47,8 +47,9 @@ public interface WxCpService {
/**
* 获取access_token, 不强制刷新access_token
* @see #getAccessToken(boolean)
*
* @throws WxErrorException
* @see #getAccessToken(boolean)
*/
String getAccessToken() throws WxErrorException;
@@ -60,6 +61,7 @@ public interface WxCpService {
* 程序员在非必要情况下尽量不要主动调用此方法
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token
* </pre>
*
* @param forceRefresh 强制刷新
* @throws me.chanjar.weixin.common.exception.WxErrorException
*/
@@ -67,8 +69,9 @@ public interface WxCpService {
/**
* 获得jsapi_ticket,不强制刷新jsapi_ticket
* @see #getJsapiTicket(boolean)
*
* @throws WxErrorException
* @see #getJsapiTicket(boolean)
*/
String getJsapiTicket() throws WxErrorException;
@@ -79,6 +82,7 @@ public interface WxCpService {
*
* 详情请见http://qydev.weixin.qq.com/wiki/index.php?title=微信JS接口#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
* </pre>
*
* @param forceRefresh 强制刷新
* @throws WxErrorException
*/
@@ -90,6 +94,7 @@ public interface WxCpService {
*
* 详情请见http://qydev.weixin.qq.com/wiki/index.php?title=微信JS接口#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
* </pre>
*
* @param url url
*/
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
@@ -128,9 +133,9 @@ public interface WxCpService {
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
*
* @param media_id
* @return 保存到本地的临时文件
* @throws WxErrorException
* @param media_id
*/
File mediaDownload(String media_id) throws WxErrorException;
@@ -152,10 +157,10 @@ public interface WxCpService {
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
* @see #menuCreate(String, me.chanjar.weixin.common.bean.WxMenu)
*
* @param menu
* @throws WxErrorException
* @see #menuCreate(String, me.chanjar.weixin.common.bean.WxMenu)
*/
void menuCreate(WxMenu menu) throws WxErrorException;
@@ -166,11 +171,11 @@ public interface WxCpService {
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuCreate(me.chanjar.weixin.common.bean.WxMenu)
*
* @param agentId 企业号应用的id
* @param menu
* @throws WxErrorException
* @see #menuCreate(me.chanjar.weixin.common.bean.WxMenu)
*/
void menuCreate(String agentId, WxMenu menu) throws WxErrorException;
@@ -181,9 +186,9 @@ public interface WxCpService {
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
* @see #menuDelete(String)
*
* @throws WxErrorException
* @see #menuDelete(String)
*/
void menuDelete() throws WxErrorException;
@@ -194,10 +199,10 @@ public interface WxCpService {
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuDelete()
*
* @param agentId 企业号应用的id
* @throws WxErrorException
* @see #menuDelete()
*/
void menuDelete(String agentId) throws WxErrorException;
@@ -208,9 +213,9 @@ public interface WxCpService {
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
* @see #menuGet(String)
*
* @throws WxErrorException
* @see #menuGet(String)
*/
WxMenu menuGet() throws WxErrorException;
@@ -221,10 +226,10 @@ public interface WxCpService {
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuGet()
*
* @param agentId 企业号应用的id
* @throws WxErrorException
* @see #menuGet()
*/
WxMenu menuGet(String agentId) throws WxErrorException;
@@ -279,6 +284,7 @@ public interface WxCpService {
*
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98.28.E8.AF.A6.E6.83.85.29
* </pre>
*
* @param departId 必填。部门id
* @param fetchChild 非必填。1/0是否递归获取子部门下面的成员
* @param status 非必填。0获取全部员工1获取已关注成员列表2获取禁用成员列表4获取未关注成员列表。status可叠加
@@ -330,6 +336,7 @@ public interface WxCpService {
*
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E6.89.B9.E9.87.8F.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98
* </pre>
*
* @param userids 员工UserID列表。对应管理端的帐号
* @throws WxErrorException
*/
@@ -367,7 +374,6 @@ public interface WxCpService {
/**
* 获得标签列表
*
*/
List<WxCpTag> tagGet() throws WxErrorException;
@@ -391,6 +397,7 @@ public interface WxCpService {
* 构造oauth2授权的url连接
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=企业获取code
* </pre>
*
* @param redirectUri
* @param state
* @return url
@@ -405,10 +412,10 @@ public interface WxCpService {
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
* @see #oauth2getUserInfo(String, String)
*
* @param code
* @return [userid, deviceid]
* @see #oauth2getUserInfo(String, String)
*/
String[] oauth2getUserInfo(String code) throws WxErrorException;
@@ -420,11 +427,11 @@ public interface WxCpService {
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #oauth2getUserInfo(String)
*
* @param agentId 企业号应用的id
* @param code
* @return [userid, deviceid]
* @see #oauth2getUserInfo(String)
*/
String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException;
@@ -442,6 +449,7 @@ public interface WxCpService {
* 邀请成员关注
* http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E9.82.80.E8.AF.B7.E6.88.90.E5.91.98.E5.85.B3.E6.B3.A8
* </pre>
*
* @param userId 用户的userid
* @param inviteTips 推送到微信上的提示语只有认证号可以使用。当使用微信推送时该字段默认为“请关注XXX企业号”邮件邀请时该字段无效。
* @return 1:微信邀请 2.邮件邀请
@@ -454,6 +462,7 @@ public interface WxCpService {
* 获取微信服务器的ip段
* http://qydev.weixin.qq.com/wiki/index.php?title=回调模式#.E8.8E.B7.E5.8F.96.E5.BE.AE.E4.BF.A1.E6.9C.8D.E5.8A.A1.E5.99.A8.E7.9A.84ip.E6.AE.B5
* </pre>
*
* @return { "ip_list": ["101.226.103.*", "101.226.62.*"] }
* @throws WxErrorException
*/
@@ -461,6 +470,7 @@ public interface WxCpService {
/**
* 当本Service没有实现某个API的时候可以用这个针对所有微信API中的GET请求
*
* @param url
* @param queryParam
* @throws WxErrorException
@@ -469,6 +479,7 @@ public interface WxCpService {
/**
* 当本Service没有实现某个API的时候可以用这个针对所有微信API中的POST请求
*
* @param url
* @param postData
* @throws WxErrorException
@@ -481,6 +492,7 @@ public interface WxCpService {
* 比{@link #get}和{@link #post}方法更灵活可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
* 可以参考,{@link me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor}的实现方法
* </pre>
*
* @param executor
* @param uri
* @param data
@@ -502,6 +514,7 @@ public interface WxCpService {
* 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试
* 默认1000ms
* </pre>
*
* @param retrySleepMillis
*/
void setRetrySleepMillis(int retrySleepMillis);
@@ -511,18 +524,21 @@ public interface WxCpService {
* 设置当微信系统响应系统繁忙时,最大重试次数
* 默认5次
* </pre>
*
* @param maxRetryTimes
*/
void setMaxRetryTimes(int maxRetryTimes);
/**
* 获取某个sessionId对应的session,如果sessionId没有对应的session则新建一个并返回。
*
* @param id id可以为任意字符串建议使用FromUserName作为id
*/
WxSession getSession(String id);
/**
* 获取某个sessionId对应的session,如果sessionId没有对应的session若create为true则新建一个否则返回null。
*
* @param id id可以为任意字符串建议使用FromUserName作为id
* @param create
*/
@@ -533,12 +549,14 @@ public interface WxCpService {
* 设置WxSessionManager只有当需要使用个性化的WxSessionManager的时候才需要调用此方法
* WxCpService默认使用的是{@link me.chanjar.weixin.common.session.StandardSessionManager}
* </pre>
*
* @param sessionManager
*/
void setSessionManager(WxSessionManager sessionManager);
/**
* 上传部门列表覆盖企业号上的部门信息
*
* @param mediaId
* @throws WxErrorException
*/
@@ -546,6 +564,7 @@ public interface WxCpService {
/**
* 上传用户列表覆盖企业号上的用户信息
*
* @param mediaId
* @throws WxErrorException
*/
@@ -553,6 +572,7 @@ public interface WxCpService {
/**
* 获取异步任务结果
*
* @param joinId
* @throws WxErrorException
*/

View File

@@ -1,23 +1,5 @@
package me.chanjar.weixin.cp.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@@ -25,7 +7,6 @@ import com.google.gson.JsonPrimitive;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.menu.WxMenu;
@@ -39,20 +20,30 @@ import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.DefaultApacheHttpHttpClientBuilder;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
public class WxCpServiceImpl implements WxCpService {
@@ -73,17 +64,13 @@ public class WxCpServiceImpl implements WxCpService {
protected CloseableHttpClient httpClient;
protected HttpHost httpProxy;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
protected WxSessionManager sessionManager = new StandardSessionManager();
/**
* 临时文件目录
*/
protected File tmpDirFile;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
try {
@@ -342,7 +329,8 @@ public class WxCpServiceImpl implements WxCpService {
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() { }.getType()
new TypeToken<List<WxCpUser>>() {
}.getType()
);
}
@@ -364,7 +352,8 @@ public class WxCpServiceImpl implements WxCpService {
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() { }.getType()
new TypeToken<List<WxCpUser>>() {
}.getType()
);
}
@@ -414,7 +403,8 @@ public class WxCpServiceImpl implements WxCpService {
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() { }.getType()
new TypeToken<List<WxCpUser>>() {
}.getType()
);
}
@@ -580,6 +570,7 @@ public class WxCpServiceImpl implements WxCpService {
throw new RuntimeException(e);
}
}
protected CloseableHttpClient getHttpclient() {
return httpClient;
}

View File

@@ -16,6 +16,10 @@ public class WxCpDepart implements Serializable {
private Integer parentId;
private Integer order;
public static WxCpDepart fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpDepart.class);
}
public Integer getId() {
return id;
}
@@ -48,10 +52,6 @@ public class WxCpDepart implements Serializable {
this.order = order;
}
public static WxCpDepart fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpDepart.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

View File

@@ -9,8 +9,8 @@ import java.util.List;
/**
* 消息
* @author Daniel Qian
*
* @author Daniel Qian
*/
public class WxCpMessage implements Serializable {
@@ -29,151 +29,6 @@ public class WxCpMessage implements Serializable {
private String safe;
private List<WxArticle> articles = new ArrayList<WxArticle>();
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getToParty() {
return toParty;
}
public void setToParty(String toParty) {
this.toParty = toParty;
}
public String getToTag() {
return toTag;
}
public void setToTag(String toTag) {
this.toTag = toTag;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getMsgType() {
return msgType;
}
public String getSafe() {
return safe;
}
public void setSafe(String safe) {
this.safe = safe;
}
/**
* <pre>
* 请使用
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_TEXT}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_IMAGE}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VOICE}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MUSIC}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VIDEO}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_NEWS}
* </pre>
* @param msgType
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getThumbMediaId() {
return thumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMusicUrl() {
return musicUrl;
}
public void setMusicUrl(String musicUrl) {
this.musicUrl = musicUrl;
}
public String getHqMusicUrl() {
return hqMusicUrl;
}
public void setHqMusicUrl(String hqMusicUrl) {
this.hqMusicUrl = hqMusicUrl;
}
public List<WxArticle> getArticles() {
return articles;
}
public void setArticles(List<WxArticle> articles) {
this.articles = articles;
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public static class WxArticle {
private String title;
private String description;
private String url;
private String picUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
}
/**
* 获得文本消息builder
*/
@@ -216,4 +71,174 @@ public class WxCpMessage implements Serializable {
return new FileBuilder();
}
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getToParty() {
return toParty;
}
public void setToParty(String toParty) {
this.toParty = toParty;
}
public String getToTag() {
return toTag;
}
public void setToTag(String toTag) {
this.toTag = toTag;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getMsgType() {
return msgType;
}
/**
* <pre>
* 请使用
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_TEXT}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_IMAGE}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VOICE}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MUSIC}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VIDEO}
* {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_NEWS}
* </pre>
*
* @param msgType
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getSafe() {
return safe;
}
public void setSafe(String safe) {
this.safe = safe;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getThumbMediaId() {
return thumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMusicUrl() {
return musicUrl;
}
public void setMusicUrl(String musicUrl) {
this.musicUrl = musicUrl;
}
public String getHqMusicUrl() {
return hqMusicUrl;
}
public void setHqMusicUrl(String hqMusicUrl) {
this.hqMusicUrl = hqMusicUrl;
}
public List<WxArticle> getArticles() {
return articles;
}
public void setArticles(List<WxArticle> articles) {
this.articles = articles;
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public static class WxArticle {
private String title;
private String description;
private String url;
private String picUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
}
}

View File

@@ -23,6 +23,10 @@ public class WxCpTag implements Serializable {
this.name = name;
}
public static WxCpTag fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTag.class);
}
public String getName() {
return name;
}
@@ -39,10 +43,6 @@ public class WxCpTag implements Serializable {
this.id = id;
}
public static WxCpTag fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTag.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

View File

@@ -13,6 +13,7 @@ import java.util.List;
*/
public class WxCpUser implements Serializable {
private final List<Attr> extAttrs = new ArrayList<Attr>();
private String userId;
private String name;
private Integer[] departIds;
@@ -25,7 +26,10 @@ public class WxCpUser implements Serializable {
private String avatar;
private Integer status;
private Integer enable;
private final List<Attr> extAttrs = new ArrayList<Attr>();
public static WxCpUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
}
public String getUserId() {
return userId;
@@ -135,10 +139,6 @@ public class WxCpUser implements Serializable {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public static WxCpUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
}
public static class Attr {
private String name;

View File

@@ -162,6 +162,43 @@ public class WxCpXmlMessage implements Serializable {
@XStreamAlias("SendLocationInfo")
private SendLocationInfo sendLocationInfo = new SendLocationInfo();
protected static WxCpXmlMessage fromXml(String xml) {
return XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
}
protected static WxCpXmlMessage fromXml(InputStream is) {
return XStreamTransformer.fromXml(WxCpXmlMessage.class, is);
}
/**
* 从加密字符串转换
*
* @param encryptedXml
* @param wxCpConfigStorage
* @param timestamp
* @param nonce
* @param msgSignature
*/
public static WxCpXmlMessage fromEncryptedXml(
String encryptedXml,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
return fromXml(plainText);
}
public static WxCpXmlMessage fromEncryptedXml(
InputStream is,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public Integer getAgentId() {
return agentId;
}
@@ -197,7 +234,6 @@ public class WxCpXmlMessage implements Serializable {
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_LINK}
* {@link me.chanjar.weixin.common.api.WxConsts#XML_MSG_EVENT}
* </pre>
*
*/
public String getMsgType() {
return msgType;
@@ -387,43 +423,6 @@ public class WxCpXmlMessage implements Serializable {
this.fromUserName = fromUserName;
}
protected static WxCpXmlMessage fromXml(String xml) {
return XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
}
protected static WxCpXmlMessage fromXml(InputStream is) {
return XStreamTransformer.fromXml(WxCpXmlMessage.class, is);
}
/**
* 从加密字符串转换
*
* @param encryptedXml
* @param wxCpConfigStorage
* @param timestamp
* @param nonce
* @param msgSignature
*/
public static WxCpXmlMessage fromEncryptedXml(
String encryptedXml,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
return fromXml(plainText);
}
public static WxCpXmlMessage fromEncryptedXml(
InputStream is,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String getStatus() {
return status;
}
@@ -566,11 +565,10 @@ public class WxCpXmlMessage implements Serializable {
@XStreamAlias("SendPicsInfo")
public static class SendPicsInfo {
@XStreamAlias("Count")
private Long count;
@XStreamAlias("PicList")
protected final List<Item> picList = new ArrayList<Item>();
@XStreamAlias("Count")
private Long count;
public Long getCount() {
return count;

View File

@@ -26,6 +26,41 @@ public abstract class WxCpXmlOutMessage {
@XStreamConverter(value = XStreamCDataConverter.class)
protected String msgType;
/**
* 获得文本消息builder
*/
public static TextBuilder TEXT() {
return new TextBuilder();
}
/**
* 获得图片消息builder
*/
public static ImageBuilder IMAGE() {
return new ImageBuilder();
}
/**
* 获得语音消息builder
*/
public static VoiceBuilder VOICE() {
return new VoiceBuilder();
}
/**
* 获得视频消息builder
*/
public static VideoBuilder VIDEO() {
return new VideoBuilder();
}
/**
* 获得图文消息builder
*/
public static NewsBuilder NEWS() {
return new NewsBuilder();
}
public String getToUserName() {
return toUserName;
}
@@ -70,39 +105,4 @@ public abstract class WxCpXmlOutMessage {
WxCpCryptUtil pc = new WxCpCryptUtil(wxCpConfigStorage);
return pc.encrypt(plainXml);
}
/**
* 获得文本消息builder
*/
public static TextBuilder TEXT() {
return new TextBuilder();
}
/**
* 获得图片消息builder
*/
public static ImageBuilder IMAGE() {
return new ImageBuilder();
}
/**
* 获得语音消息builder
*/
public static VoiceBuilder VOICE() {
return new VoiceBuilder();
}
/**
* 获得视频消息builder
*/
public static VideoBuilder VIDEO() {
return new VideoBuilder();
}
/**
* 获得图文消息builder
*/
public static NewsBuilder NEWS() {
return new NewsBuilder();
}
}

View File

@@ -11,11 +11,10 @@ import java.util.List;
@XStreamAlias("xml")
public class WxCpXmlOutNewsMessage extends WxCpXmlOutMessage {
@XStreamAlias("ArticleCount")
protected int articleCount;
@XStreamAlias("Articles")
protected final List<Item> articles = new ArrayList<Item>();
@XStreamAlias("ArticleCount")
protected int articleCount;
public WxCpXmlOutNewsMessage() {
this.msgType = WxConsts.XML_MSG_NEWS;

View File

@@ -8,8 +8,8 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.FILE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class FileBuilder extends BaseBuilder<FileBuilder> {
private String mediaId;

View File

@@ -8,8 +8,8 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.IMAGE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
private String mediaId;

View File

@@ -12,8 +12,8 @@ import java.util.List;
* 用法:
* WxCustomMessage m = WxCustomMessage.NEWS().addArticle(article).toUser(...).build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class NewsBuilder extends BaseBuilder<NewsBuilder> {

View File

@@ -8,8 +8,8 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.TEXT().content(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class TextBuilder extends BaseBuilder<TextBuilder> {
private String content;

View File

@@ -14,8 +14,8 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
* .toUser(...)
* .build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class VideoBuilder extends BaseBuilder<VideoBuilder> {
private String mediaId;

View File

@@ -8,8 +8,8 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.VOICE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> {
private String mediaId;

View File

@@ -4,6 +4,7 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutImageMessage;
/**
* 图片消息builder
*
* @author Daniel Qian
*/
public final class ImageBuilder extends BaseBuilder<ImageBuilder, WxCpXmlOutImageMessage> {

View File

@@ -8,6 +8,7 @@ import java.util.List;
/**
* 图文消息builder
*
* @author Daniel Qian
*/
public final class NewsBuilder extends BaseBuilder<NewsBuilder, WxCpXmlOutNewsMessage> {

View File

@@ -4,8 +4,8 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
/**
* 文本消息builder
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class TextBuilder extends BaseBuilder<TextBuilder, WxCpXmlOutTextMessage> {
private String content;

View File

@@ -4,8 +4,8 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutVideoMessage;
/**
* 视频消息builder
* @author Daniel Qian
*
* @author Daniel Qian
*/
public final class VideoBuilder extends BaseBuilder<VideoBuilder, WxCpXmlOutVideoMessage> {
@@ -17,10 +17,12 @@ public final class VideoBuilder extends BaseBuilder<VideoBuilder, WxCpXmlOutVide
this.title = title;
return this;
}
public VideoBuilder description(String description) {
this.description = description;
return this;
}
public VideoBuilder mediaId(String mediaId) {
this.mediaId = mediaId;
return this;

View File

@@ -4,6 +4,7 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutVoiceMessage;
/**
* 语音消息builder
*
* @author Daniel Qian
*/
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder, WxCpXmlOutVoiceMessage> {

View File

@@ -2,6 +2,10 @@
* 对公众平台发送给公众账号的消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
* <p>
* 针对org.apache.commons.codec.binary.Base64
* 需要导入架包commons-codec-1.9或commons-codec-1.8等其他版本)
* 官方下载地址http://commons.apache.org/proper/commons-codec/download_codec.cgi
*/
// ------------------------------------------------------------------------

View File

@@ -16,9 +16,7 @@ import me.chanjar.weixin.cp.bean.WxCpMessage;
import java.lang.reflect.Type;
/**
*
* @author Daniel Qian
*
*/
public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {

View File

@@ -29,6 +29,7 @@ public class XStreamTransformer {
/**
* 注册扩展消息的解析器
*
* @param clz 类型
* @param xStream xml解析器
*/
@@ -38,7 +39,6 @@ public class XStreamTransformer {
/**
* pojo -> xml
*
*/
public static <T> String toXml(Class<T> clazz, T object) {
return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);

View File

@@ -10,6 +10,13 @@ import java.io.InputStream;
public class ApiTestModule implements Module {
public static <T> T fromXml(Class<T> clazz, InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", clazz);
xstream.processAnnotations(clazz);
return (T) xstream.fromXML(is);
}
@Override
public void configure(Binder binder) {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
@@ -21,13 +28,6 @@ public class ApiTestModule implements Module {
binder.bind(WxCpConfigStorage.class).toInstance(config);
}
public static <T> T fromXml(Class<T> clazz, InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", clazz);
xstream.processAnnotations(clazz);
return (T) xstream.fromXML(is);
}
@XStreamAlias("xml")
public static class WxXmlCpInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
@@ -40,6 +40,7 @@ public class ApiTestModule implements Module {
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}

View File

@@ -9,8 +9,8 @@ import org.testng.annotations.Test;
/**
* 基础API测试
* @author Daniel Qian
*
* @author Daniel Qian
*/
@Test(groups = "baseAPI")
@Guice(modules = ApiTestModule.class)

View File

@@ -16,8 +16,8 @@ import java.util.List;
/**
* 测试多媒体文件上传下载
* @author Daniel Qian
*
* @author Daniel Qian
*/
//@Test(groups="mediaAPI", dependsOnGroups="baseAPI")
@Test

View File

@@ -13,8 +13,8 @@ import java.util.Map;
/**
* 测试消息路由器
* @author Daniel Qian
*
* @author Daniel Qian
*/
@Test
public class WxCpMessageRouterTest {
@@ -152,25 +152,6 @@ public class WxCpMessageRouterTest {
}
public static class WxEchoCpMessageHandler implements WxCpMessageHandler {
private StringBuffer sb;
private String echoStr;
public WxEchoCpMessageHandler(StringBuffer sb, String echoStr) {
this.sb = sb;
this.echoStr = echoStr;
}
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService,
WxSessionManager sessionManager) {
sb.append(this.echoStr).append(',');
return null;
}
}
@DataProvider
public Object[][] standardSessionManager() {
@@ -292,6 +273,25 @@ public class WxCpMessageRouterTest {
}
}
public static class WxEchoCpMessageHandler implements WxCpMessageHandler {
private StringBuffer sb;
private String echoStr;
public WxEchoCpMessageHandler(StringBuffer sb, String echoStr) {
this.sb = sb;
this.echoStr = echoStr;
}
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService,
WxSessionManager sessionManager) {
sb.append(this.echoStr).append(',');
return null;
}
}
public static class WxSessionMessageHandler implements WxCpMessageHandler {
@Override

View File

@@ -1,21 +1,19 @@
package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
/**
* 测试菜单
* @author Daniel Qian
*
* @author Daniel Qian
*/
@Test(groups = "menuAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)

View File

@@ -13,17 +13,16 @@ import java.io.InputStream;
@XStreamAlias("xml")
class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
@Override
public String toString() {
return "SimpleWxConfigProvider [appidOrCorpid=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken
+ ", expiresTime=" + expiresTime + ", token=" + token + ", aesKey=" + aesKey + "]";
}
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpDemoInMemoryConfigStorage.class);
return (WxCpDemoInMemoryConfigStorage) xstream.fromXML(is);
}
@Override
public String toString() {
return "SimpleWxConfigProvider [appidOrCorpid=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken
+ ", expiresTime=" + expiresTime + ", token=" + token + ", aesKey=" + aesKey + "]";
}
}

View File

@@ -18,7 +18,8 @@ public class WxCpOAuth2Servlet extends HttpServlet {
this.wxCpService = wxCpService;
}
@Override protected void service(HttpServletRequest request, HttpServletResponse response)
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");