big update:去除被动消息实体、commons包、dom4j包

This commit is contained in:
jinyu
2015-05-12 23:27:39 +08:00
parent 165f4b3bb8
commit 3d0c3e2c9c
84 changed files with 836 additions and 3287 deletions
@@ -10,7 +10,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.mime.content.ByteArrayBody;
import com.alibaba.fastjson.JSON;
@@ -30,6 +29,7 @@ import com.foxinmy.weixin4j.type.MediaType;
import com.foxinmy.weixin4j.util.ConfigUtil;
import com.foxinmy.weixin4j.util.FileUtil;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 媒体相关API
@@ -62,7 +62,7 @@ public class MediaApi extends QyApi {
*/
public String uploadMedia(File file) throws WeixinException, IOException {
String mediaTypeKey = IOUtil.getExtension(file.getName());
if (StringUtils.isBlank(mediaTypeKey)) {
if (StringUtil.isBlank(mediaTypeKey)) {
mediaTypeKey = FileUtil.getFileType(file);
}
MediaType mediaType = MediaType.getMediaType(mediaTypeKey);
@@ -237,7 +237,7 @@ public class MediaApi extends QyApi {
return true;
}
});
writer.write(StringUtils.join(column.values(), ","));
writer.write(StringUtil.join(column.values(), ','));
writer.write("\r\n");
}
String mediaId = uploadMedia(batchName,
@@ -3,8 +3,6 @@ package com.foxinmy.weixin4j.qy.api;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -16,6 +14,7 @@ import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.qy.type.InviteType;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 成员API
@@ -282,7 +281,7 @@ public class UserApi extends QyApi {
throws WeixinException {
JSONObject obj = new JSONObject();
obj.put("userid", userId);
if (StringUtils.isBlank(tips)) {
if (StringUtil.isBlank(tips)) {
obj.put("invite_tips", tips);
}
String invite_user_uri = getRequestUri("invite_user_uri");
@@ -1,100 +0,0 @@
package com.foxinmy.weixin4j.qy.event;
import java.io.Serializable;
import com.foxinmy.weixin4j.message.event.EventMessage;
import com.foxinmy.weixin4j.qy.type.EventType;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* 异步任务事件完成通知
*
* @className BatchjobresultMessage
* @author jy
* @date 2015年3月31日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6#.E5.BC.82.E6.AD.A5.E4.BB.BB.E5.8A.A1.E5.AE.8C.E6.88.90.E4.BA.8B.E4.BB.B6.E6.8E.A8.E9.80.81">异步任务事件完成通知</a>
*/
public class BatchjobresultMessage extends EventMessage {
private static final long serialVersionUID = 8014540441322209657L;
public BatchjobresultMessage() {
super(EventType.batch_job_result.name());
}
/**
* 任务信息
*/
@XStreamAlias("BatchJob")
private BatchJob batchJob;
public BatchJob getBatchJob() {
return batchJob;
}
/**
* 任务信息
*
* @className BatchJob
* @author jy
* @date 2015年4月1日
* @since JDK 1.7
* @see
*/
public static class BatchJob implements Serializable {
private static final long serialVersionUID = -7520032656787156391L;
/**
* 异步任务id,最大长度为64字符
*/
@XStreamAlias("JobId")
private String jobId;
/**
* 操作类型,字符串,目前分别有: 1. sync_user(增量更新成员) 2. replace_user(全量覆盖成员) 3.
* invite_user(邀请成员关注) 4. replace_party(全量覆盖部门)
*
* @see com.foxinmy.weixin4j.qy.type.BatchType
*/
@XStreamAlias("JobType")
private String jobType;
/**
* 返回码
*/
@XStreamAlias("ErrCode")
private String ErrCode;
/**
* 对返回码的文本描述内容
*/
@XStreamAlias("ErrMsg")
private String errMsg;
public String getJobId() {
return jobId;
}
public String getJobType() {
return jobType;
}
public String getErrCode() {
return ErrCode;
}
public String getErrMsg() {
return errMsg;
}
@Override
public String toString() {
return "[jobId=" + jobId + ", jobType=" + jobType + ", ErrCode="
+ ErrCode + ", errMsg=" + errMsg + "]";
}
}
@Override
public String toString() {
return "BatchjobresultMessage [batchJob=" + batchJob + ", "
+ super.toString() + "]";
}
}
@@ -1,41 +0,0 @@
package com.foxinmy.weixin4j.qy.event;
import com.foxinmy.weixin4j.message.event.EventMessage;
import com.foxinmy.weixin4j.qy.type.EventType;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* 用户进入应用的事件推送(企业号)本事件只有在应用的回调模式中打开上报开关时上报
*
* @className EnterAgentEventMessage
* @author jy
* @date 2014年12月28日
* @since JDK 1.7
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6#.E7.94.A8.E6.88.B7.E8.BF.9B.E5.85.A5.E5.BA.94.E7.94.A8.E7.9A.84.E4.BA.8B.E4.BB.B6.E6.8E.A8.E9.80.81"
* >用户进入应用的事件推送</a>
*/
public class EnterAgentEventMessage extends EventMessage {
private static final long serialVersionUID = 7675732524832500820L;
public EnterAgentEventMessage() {
super(EventType.enter_agent.name());
}
/**
* 事件KEY值,与自定义菜单接口中KEY值对应
*/
@XStreamAlias("EventKey")
private String eventKey;
public String getEventKey() {
return eventKey;
}
@Override
public String toString() {
return "EnterAgentEventMessage [eventKey=" + eventKey + ", "
+ super.toString() + "]";
}
}
@@ -1,28 +0,0 @@
package com.foxinmy.weixin4j.qy.event;
import com.foxinmy.weixin4j.message.event.EventMessage;
import com.foxinmy.weixin4j.type.EventType;
/**
* 关注/取消关注事件</br> <font color="red">包括直接关注与扫描关注</font>
*
* @className ScribeEventMessage
* @author jy.hu
* @date 2014年4月6日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6#.E6.88.90.E5.91.98.E5.85.B3.E6.B3.A8.2F.E5.8F.96.E6.B6.88.E5.85.B3.E6.B3.A8.E4.BA.8B.E4.BB.B6">成员关注/取消关注事件</a>
*/
public class ScribeEventMessage extends EventMessage {
private static final long serialVersionUID = -6846321620262204915L;
public ScribeEventMessage() {
super(EventType.subscribe.name());
}
@Override
public String toString() {
return "ScribeEventMessage [" + super.toString() + "]";
}
}
@@ -3,10 +3,9 @@ package com.foxinmy.weixin4j.qy.message;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.tuple.NotifyTuple;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 发送消息对象
@@ -29,7 +28,7 @@ public class NotifyMessage implements Serializable {
private static final long serialVersionUID = 1219589414293000383L;
private static final String SEPARATOR = "|";
private static final char SEPARATOR = '|';
/**
* UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
@@ -65,13 +64,13 @@ public class NotifyMessage implements Serializable {
public NotifyMessage(List<String> tousers, List<String> topartys,
List<String> totags, NotifyTuple tuple, int agentid, boolean isSafe) {
if (tousers != null && !tousers.isEmpty()) {
this.touser = StringUtils.join(tousers, SEPARATOR);
this.touser = StringUtil.join(tousers, SEPARATOR);
}
if (topartys != null && !topartys.isEmpty()) {
this.toparty = StringUtils.join(topartys, SEPARATOR);
this.toparty = StringUtil.join(topartys, SEPARATOR);
}
if (totags != null && !totags.isEmpty()) {
this.totag = StringUtils.join(totags, SEPARATOR);
this.totag = StringUtil.join(totags, SEPARATOR);
}
this.agentid = agentid;
this.safe = isSafe ? 1 : 0;
@@ -88,7 +87,7 @@ public class NotifyMessage implements Serializable {
public void setTouser(List<String> tousers) {
if (tousers != null && !tousers.isEmpty()) {
this.touser = StringUtils.join(tousers, SEPARATOR);
this.touser = StringUtil.join(tousers, SEPARATOR);
}
}
@@ -102,7 +101,7 @@ public class NotifyMessage implements Serializable {
public void setToparty(List<String> topartys) {
if (topartys != null && !topartys.isEmpty()) {
this.toparty = StringUtils.join(topartys, SEPARATOR);
this.toparty = StringUtil.join(topartys, SEPARATOR);
}
}
@@ -116,7 +115,7 @@ public class NotifyMessage implements Serializable {
public void setTotag(List<String> totags) {
if (totags != null && !totags.isEmpty()) {
this.totag = StringUtils.join(totags, SEPARATOR);
this.totag = StringUtil.join(totags, SEPARATOR);
}
}
@@ -4,7 +4,7 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* id参数集
@@ -19,6 +19,8 @@ public class IdParameter implements Serializable {
private static final long serialVersionUID = -2689758682205591133L;
private static final char SEPARATOR = '|';
private Map<String, String> parameterMap;
public IdParameter() {
@@ -32,7 +34,7 @@ public class IdParameter implements Serializable {
* @return
*/
public IdParameter putUseIds(String... userIds) {
parameterMap.put("touser", StringUtils.join(userIds, '|'));
parameterMap.put("touser", StringUtil.join(userIds, SEPARATOR));
return this;
}
@@ -43,7 +45,7 @@ public class IdParameter implements Serializable {
* @return
*/
public IdParameter putUseIds(int... partyIds) {
parameterMap.put("toparty", StringUtils.join(partyIds, '|'));
parameterMap.put("toparty", StringUtil.join(partyIds, SEPARATOR));
return this;
}
@@ -54,7 +56,7 @@ public class IdParameter implements Serializable {
* @return
*/
public IdParameter putTagIds(int... tagIds) {
parameterMap.put("totag", StringUtils.join(tagIds, '|'));
parameterMap.put("totag", StringUtil.join(tagIds, SEPARATOR));
return this;
}
@@ -1,25 +0,0 @@
package com.foxinmy.weixin4j.qy.type;
/**
* 企业号事件类型
*
* @className EventType
* @author jy
* @date 2014年9月30日
* @since JDK 1.7
* @see
*/
public enum EventType {
/**
* 进入企业号应用事件
*
* @see com.foxinmy.weixin4j.qy.event.EnterAgentEventMessage
*/
enter_agent,
/**
* 异步任务完成事件
*
* @see com.foxinmy.weixin4j.msg.event.BatchjobresultMessage
*/
batch_job_result;
}
@@ -1,64 +0,0 @@
package com.foxinmy.weixin4j.qy.test;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
public class AesMsgTest extends MessagePush {
StringBuilder xmlSb = new StringBuilder();
@Test
public void testValidate() throws WeixinException, IOException {
String para = "?msg_signature=33f680b72cfd87a6f8f4d751575bcc31266853a9&timestamp=1416488666&nonce=1358224345&echostr=WZu9tT6KDuyhgOZ1aT%2FIoPNaUkA%2FsfVR%2F%2BDua5q84Kvhs6Fc1%2F6f2510%2BUuMByessdQWsoeak0lSIpaYOIlHrw%3D%3D";
xmlSb.delete(0, xmlSb.length());
String response = get(para);
Assert.assertEquals("2143641595566077626", response);
}
@Test
public void testType1() throws WeixinException, IOException {
String para = "?signature=6dd806a20a314723e78bc58742a1b98a7adfd151&timestamp=1415979366&nonce=1865915590";
xmlSb.delete(0, xmlSb.length());
xmlSb.append("<xml>");
xmlSb.append("<ToUserName><![CDATA[gh_248c6f91d64f]]></ToUserName>");
xmlSb.append("<FromUserName><![CDATA[oyFLst1bqtuTcxK-ojF8hOGtLQao]]></FromUserName>");
xmlSb.append("<CreateTime>1415979365</CreateTime>");
xmlSb.append("<MsgType><![CDATA[event]]></MsgType>");
xmlSb.append("<Event><![CDATA[CLICK]]></Event>");
xmlSb.append("<EventKey><![CDATA[CHECKIN]]></EventKey>");
xmlSb.append("</xml>");
String response = push(para, xmlSb.toString());
Assert.assertNotNull(response);
}
@Test
public void testType2() throws WeixinException, IOException {
String para = "?signature=ad05f836772d1cbba1ff2edb7be4b9c9fb3a43d5&timestamp=1415980001&nonce=1803216865&encrypt_type=raw&msg_signature=c0d38e9ca00548f7142627ec2908a4fe8481025e";
xmlSb.delete(0, xmlSb.length());
xmlSb.append("<xml>");
xmlSb.append("<ToUserName><![CDATA[gh_248c6f91d64f]]></ToUserName>");
xmlSb.append("<FromUserName><![CDATA[oyFLst1bqtuTcxK-ojF8hOGtLQao]]></FromUserName>");
xmlSb.append("<CreateTime>1415980001</CreateTime>");
xmlSb.append("<MsgType><![CDATA[event]]></MsgType>");
xmlSb.append("<Event><![CDATA[CLICK]]></Event>");
xmlSb.append("<EventKey><![CDATA[CHECKIN]]></EventKey>");
xmlSb.append("</xml>");
String response = push(para, xmlSb.toString());
Assert.assertNotNull(response);
}
@Test
public void testType3() throws WeixinException, IOException {
String para = "?signature=ad05f836772d1cbba1ff2edb7be4b9c9fb3a43d5&timestamp=1415980001&nonce=1803216865&encrypt_type=aes&msg_signature=c0d38e9ca00548f7142627ec2908a4fe8481025e";
xmlSb.delete(0, xmlSb.length());
xmlSb.append("<xml>");
xmlSb.append("<ToUserName><![CDATA[gh_248c6f91d64f]]></ToUserName>");
xmlSb.append("<Encrypt><![CDATA[R6VQIWDR14XgSRLm25zc7V/WJYqK15gsUiMh0u/5GTMZME6jGtHkyfVN079ZPL065b+ZDq3TnoFKKtjtZlzcodY6Fm8+EujvtbTdVMMFSwdo8AwqVViAn09+DDfqPaNvbHUSiYsL3qlxArs1MH6APRUHFo7MU/piY1x2stJc8+kv28xtF+K8Aou0RuPO7PeQ18Zu/GkLnYNiI1E7UG31UYfOgVKcRjeE0PXa18iF5LBS8G/ce/l+/pH/DJWUBw5uXaqSOlo21tctlgLXu3bYUUkIu8tT49QwhHvRZILtO9icoyCNuTA7iTcHIdlAe1bD1S0ncmopIQCGmoU2/AXC2CCi6trONf3EPNKKKfDeQYHadnVZOg6kTX2cnYmHZLviYeLzjCKFSqSNkimoSKQ/Dcutpsq1D82NCwiExUZW4oo=]]></Encrypt>");
xmlSb.append("</xml>");
String response = push(para, xmlSb.toString());
Assert.assertNotNull(response);
}
}
@@ -1,68 +0,0 @@
package com.foxinmy.weixin4j.qy.test;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.foxinmy.weixin4j.exception.WeixinException;
public class MessagePush {
private final String server = "http://localhost:8090";
private final HttpClient httpClient;
private final HttpPost httpPost;
private final HttpGet httpGet;
public MessagePush() {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost();
httpPost.setURI(URI.create(server));
httpGet = new HttpGet();
httpGet.setURI(URI.create(server));
}
public String get(String para) throws WeixinException, IOException {
httpGet.setURI(URI.create(server + para));
HttpResponse httpResponse = httpClient.execute(httpGet);
return entity(httpResponse);
}
public String push(String xml) throws WeixinException, IOException {
return push("", xml);
}
public String push(String para, String xml) throws WeixinException,
IOException {
httpPost.setURI(URI.create(server + para));
httpPost.setEntity(new StringEntity(xml, StandardCharsets.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
return entity(httpResponse);
}
private String entity(HttpResponse httpResponse) throws WeixinException,
IOException {
StatusLine statusLine = httpResponse.getStatusLine();
int status = statusLine.getStatusCode();
if (status != HttpStatus.SC_OK) {
throw new WeixinException(Integer.toString(status), "request fail");
}
if (status == HttpStatus.SC_MOVED_PERMANENTLY
|| status == HttpStatus.SC_MOVED_TEMPORARILY) {
throw new WeixinException(Integer.toString(status), "uri moved");
}
return EntityUtils.toString(httpResponse.getEntity(),
StandardCharsets.UTF_8);
}
}