issue #71 替换掉JAXB,避免因OpenJDK造成无法deserialize xml的错误

This commit is contained in:
Daniel Qian
2015-01-19 15:12:14 +08:00
parent eaa77bc7b3
commit f3ec0b1965
73 changed files with 834 additions and 986 deletions

View File

@@ -1,10 +1,10 @@
package me.chanjar.weixin.cp.api;
import java.util.Map;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import java.util.Map;
/**
* 处理微信推送消息的处理器接口
* @author Daniel Qian

View File

@@ -1,9 +1,9 @@
package me.chanjar.weixin.cp.api;
import java.util.Map;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import java.util.Map;
/**
* 微信消息拦截器,可以用来做验证
* @author Daniel Qian

View File

@@ -1,5 +1,8 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -8,9 +11,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
/**
* <pre>
* 微信消息路由器通过代码化的配置把来自微信的消息交给handler处理

View File

@@ -1,18 +1,19 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
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 java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.cp.bean.*;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.common.exception.WxErrorException;
/**
* 微信API的Service
*/

View File

@@ -1,11 +1,11 @@
package me.chanjar.weixin.cp.bean;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* 消息
* @author Daniel Qian

View File

@@ -1,14 +1,13 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XmlTransformer;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
import org.apache.commons.io.IOUtils;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -25,103 +24,102 @@ import java.util.List;
*
* @author Daniel Qian
*/
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlMessage {
///////////////////////
// 以下都是微信推送过来的消息的xml的element所对应的属性
///////////////////////
@XmlElement(name="AgentID")
@XStreamAlias("AgentID")
private Integer agentId;
@XmlElement(name = "ToUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("ToUserName")
@XStreamConverter(value=XStreamCDataConverter.class)
private String toUserName;
@XmlElement(name = "FromUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("FromUserName")
@XStreamConverter(value=XStreamCDataConverter.class)
private String fromUserName;
@XmlElement(name = "CreateTime")
@XStreamAlias("CreateTime")
private Long createTime;
@XmlElement(name = "MsgType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("MsgType")
@XStreamConverter(value=XStreamCDataConverter.class)
private String msgType;
@XmlElement(name = "Content")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Content")
@XStreamConverter(value=XStreamCDataConverter.class)
private String content;
@XmlElement(name = "MsgId")
@XStreamAlias("MsgId")
private Long msgId;
@XmlElement(name = "PicUrl")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("PicUrl")
@XStreamConverter(value=XStreamCDataConverter.class)
private String picUrl;
@XmlElement(name = "MediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("MediaId")
@XStreamConverter(value=XStreamCDataConverter.class)
private String mediaId;
@XmlElement(name = "Format")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Format")
@XStreamConverter(value=XStreamCDataConverter.class)
private String format;
@XmlElement(name = "ThumbMediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("ThumbMediaId")
@XStreamConverter(value=XStreamCDataConverter.class)
private String thumbMediaId;
@XmlElement(name = "Location_X")
@XStreamAlias("Location_X")
private Double locationX;
@XmlElement(name = "Location_Y")
@XStreamAlias("Location_Y")
private Double locationY;
@XmlElement(name = "Scale")
@XStreamAlias("Scale")
private Double scale;
@XmlElement(name = "Label")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Label")
@XStreamConverter(value=XStreamCDataConverter.class)
private String label;
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Title")
@XStreamConverter(value=XStreamCDataConverter.class)
private String title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Description")
@XStreamConverter(value=XStreamCDataConverter.class)
private String description;
@XmlElement(name = "Url")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Url")
@XStreamConverter(value=XStreamCDataConverter.class)
private String url;
@XmlElement(name = "Event")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Event")
@XStreamConverter(value=XStreamCDataConverter.class)
private String event;
@XmlElement(name = "EventKey")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("EventKey")
@XStreamConverter(value=XStreamCDataConverter.class)
private String eventKey;
@XmlElement(name = "Ticket")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Ticket")
@XStreamConverter(value=XStreamCDataConverter.class)
private String ticket;
@XmlElement(name = "Latitude")
@XStreamAlias("Latitude")
private Double latitude;
@XmlElement(name = "Longitude")
@XStreamAlias("Longitude")
private Double longitude;
@XmlElement(name = "Precision")
@XStreamAlias("Precision")
private Double precision;
@XmlElement(name = "Recognition")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Recognition")
@XStreamConverter(value=XStreamCDataConverter.class)
private String recognition;
///////////////////////////////////////
@@ -130,37 +128,37 @@ public class WxCpXmlMessage {
/**
* 群发的结果
*/
@XmlElement(name = "Status")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Status")
@XStreamConverter(value=XStreamCDataConverter.class)
private String status;
/**
* group_id下粉丝数或者openid_list中的粉丝数
*/
@XmlElement(name = "TotalCount")
@XStreamAlias("TotalCount")
private Integer totalCount;
/**
* 过滤过滤是指特定地区、性别的过滤、用户设置拒收的过滤用户接收已超4条的过滤准备发送的粉丝数原则上filterCount = sentCount + errorCount
*/
@XmlElement(name = "FilterCount")
@XStreamAlias("FilterCount")
private Integer filterCount;
/**
* 发送成功的粉丝数
*/
@XmlElement(name = "SentCount")
@XStreamAlias("SentCount")
private Integer sentCount;
/**
* 发送失败的粉丝数
*/
@XmlElement(name = "ErrorCount")
@XStreamAlias("ErrorCount")
private Integer errorCount;
@XmlElement(name = "ScanCodeInfo")
@XStreamAlias("ScanCodeInfo")
private ScanCodeInfo scanCodeInfo = new ScanCodeInfo();
@XmlElement(name = "SendPicsInfo")
@XStreamAlias("SendPicsInfo")
private SendPicsInfo sendPicsInfo = new SendPicsInfo();
@XmlElement(name = "SendLocationInfo")
@XStreamAlias("SendLocationInfo")
private SendLocationInfo sendLocationInfo = new SendLocationInfo();
public Integer getAgentId() {
@@ -390,19 +388,11 @@ public class WxCpXmlMessage {
}
protected static WxCpXmlMessage fromXml(String xml) {
try {
return XmlTransformer.fromXml(WxCpXmlMessage.class, xml);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
return XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
}
protected static WxCpXmlMessage fromXml(InputStream is) {
try {
return XmlTransformer.fromXml(WxCpXmlMessage.class, is);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
return XStreamTransformer.fromXml(WxCpXmlMessage.class, is);
}
/**
@@ -499,16 +489,15 @@ public class WxCpXmlMessage {
this.sendLocationInfo = sendLocationInfo;
}
@XmlRootElement(name = "ScanCodeInfo")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("ScanCodeInfo")
public static class ScanCodeInfo {
@XmlElement(name = "ScanType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("ScanType")
@XStreamConverter(value=XStreamCDataConverter.class)
private String scanType;
@XmlElement(name = "ScanResult")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("ScanResult")
@XStreamConverter(value=XStreamCDataConverter.class)
private String scanResult;
/**
@@ -538,15 +527,13 @@ public class WxCpXmlMessage {
}
@XmlRootElement(name = "SendPicsInfo")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("SendPicsInfo")
public static class SendPicsInfo {
@XmlElement(name = "Count")
@XStreamAlias("Count")
private Long count;
@XmlElementWrapper(name="PicList")
@XmlElement(name = "item")
@XStreamAlias("PicList")
protected final List<Item> picList = new ArrayList<Item>();
public Long getCount() {
@@ -561,13 +548,11 @@ public class WxCpXmlMessage {
return picList;
}
@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "WxXmlMessage.SendPicsInfo.Item")
@XStreamAlias("item")
public static class Item {
@XmlElement(name = "PicMd5Sum")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("PicMd5Sum")
@XStreamConverter(value=XStreamCDataConverter.class)
private String PicMd5Sum;
public String getPicMd5Sum() {
@@ -580,28 +565,27 @@ public class WxCpXmlMessage {
}
}
@XmlRootElement(name = "SendLocationInfo")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("SendLocationInfo")
public static class SendLocationInfo {
@XmlElement(name = "Location_X")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Location_X")
@XStreamConverter(value=XStreamCDataConverter.class)
private String locationX;
@XmlElement(name = "Location_Y")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Location_Y")
@XStreamConverter(value=XStreamCDataConverter.class)
private String locationY;
@XmlElement(name = "Scale")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Scale")
@XStreamConverter(value=XStreamCDataConverter.class)
private String scale;
@XmlElement(name = "Label")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Label")
@XStreamConverter(value=XStreamCDataConverter.class)
private String label;
@XmlElement(name = "Poiname")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Poiname")
@XStreamConverter(value=XStreamCDataConverter.class)
private String poiname;
public String getLocationX() {

View File

@@ -1,20 +1,15 @@
package me.chanjar.weixin.cp.bean;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller;
import me.chanjar.weixin.common.util.xml.XStreamMediaIdConverter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlOutImageMessage extends WxCpXmlOutMessage {
@XmlElement(name="Image")
@XmlJavaTypeAdapter(MediaIdMarshaller.class)
@XStreamAlias("Image")
@XStreamConverter(value=XStreamMediaIdConverter.class)
private String mediaId;
public WxCpXmlOutImageMessage() {

View File

@@ -1,35 +1,29 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.bean.outxmlbuilder.*;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XmlTransformer;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XStreamAlias("xml")
public abstract class WxCpXmlOutMessage {
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutMessage {
@XmlElement(name="ToUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("ToUserName")
@XStreamConverter(value=XStreamCDataConverter.class)
protected String toUserName;
@XmlElement(name="FromUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("FromUserName")
@XStreamConverter(value=XStreamCDataConverter.class)
protected String fromUserName;
@XmlElement(name="CreateTime")
@XStreamAlias("CreateTime")
protected Long createTime;
@XmlElement(name="MsgType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("MsgType")
@XStreamConverter(value=XStreamCDataConverter.class)
protected String msgType;
public String getToUserName() {
@@ -65,11 +59,7 @@ public class WxCpXmlOutMessage {
}
protected String toXml() {
try {
return XmlTransformer.toXml((Class)this.getClass(), this);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
return XStreamTransformer.toXml((Class)this.getClass(), this);
}
/**

View File

@@ -1,22 +1,20 @@
package me.chanjar.weixin.cp.bean;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlOutNewsMessage extends WxCpXmlOutMessage {
@XmlElement(name = "ArticleCount")
@XStreamAlias("ArticleCount")
protected int articleCount;
@XmlElementWrapper(name="Articles")
@XmlElement(name = "item")
@XStreamAlias("Articles")
protected final List<Item> articles = new ArrayList<Item>();
public WxCpXmlOutNewsMessage() {
@@ -37,24 +35,23 @@ public class WxCpXmlOutNewsMessage extends WxCpXmlOutMessage {
}
@XmlRootElement(name = "Item")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("item")
public static class Item {
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Title")
@XStreamConverter(value=XStreamCDataConverter.class)
private String Title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Description")
@XStreamConverter(value=XStreamCDataConverter.class)
private String Description;
@XmlElement(name="PicUrl")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("PicUrl")
@XStreamConverter(value=XStreamCDataConverter.class)
private String PicUrl;
@XmlElement(name="Url")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Url")
@XStreamConverter(value=XStreamCDataConverter.class)
private String Url;
public String getTitle() {

View File

@@ -1,20 +1,15 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlOutTextMessage extends WxCpXmlOutMessage {
@XmlElement(name="Content")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Content")
@XStreamConverter(value=XStreamCDataConverter.class)
private String content;
public WxCpXmlOutTextMessage() {

View File

@@ -1,19 +1,14 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlOutVideoMessage extends WxCpXmlOutMessage {
@XmlElement(name = "Video")
@XStreamAlias("Video")
protected final Video video = new Video();
public WxCpXmlOutVideoMessage() {
@@ -45,20 +40,19 @@ public class WxCpXmlOutVideoMessage extends WxCpXmlOutMessage {
}
@XmlRootElement(name = "Video")
@XmlAccessorType(XmlAccessType.FIELD)
private static class Video {
@XStreamAlias("Video")
public static class Video {
@XmlElement(name = "MediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("MediaId")
@XStreamConverter(value=XStreamCDataConverter.class)
private String mediaId;
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Title")
@XStreamConverter(value=XStreamCDataConverter.class)
private String title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
@XStreamAlias("Description")
@XStreamConverter(value=XStreamCDataConverter.class)
private String description;
public String getMediaId() {

View File

@@ -1,20 +1,15 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller;
import me.chanjar.weixin.common.util.xml.XStreamMediaIdConverter;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public class WxCpXmlOutVoiceMessage extends WxCpXmlOutMessage {
@XmlElement(name="Voice")
@XmlJavaTypeAdapter(MediaIdMarshaller.class)
@XStreamAlias("Voice")
@XStreamConverter(value=XStreamMediaIdConverter.class)
private String mediaId;
public WxCpXmlOutVoiceMessage() {

View File

@@ -1,11 +1,11 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.cp.bean.WxCpXmlOutNewsMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutNewsMessage.Item;
import java.util.ArrayList;
import java.util.List;
/**
* 图文消息builder
* @author Daniel Qian

View File

@@ -8,18 +8,11 @@
*/
package me.chanjar.weixin.cp.util.json;
import java.lang.reflect.Type;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
/**
* @author Daniel Qian

View File

@@ -2,11 +2,12 @@ package me.chanjar.weixin.cp.util.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.util.json.WxMediaUploadResultAdapter;
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
import me.chanjar.weixin.cp.bean.*;
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;
public class WxCpGsonBuilder {

View File

@@ -8,12 +8,12 @@
*/
package me.chanjar.weixin.cp.util.json;
import java.lang.reflect.Type;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.WxCpUser;
import java.lang.reflect.Type;
/**
* @author Daniel Qian
*/

View File

@@ -0,0 +1,106 @@
package me.chanjar.weixin.cp.util.xml;
import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.cp.bean.*;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* Created by qianjia on 15/1/19.
*/
public class XStreamTransformer {
protected static final Map<Class, XStream> CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();
/**
* xml -> pojo
*
* @param clazz
* @param xml
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, String xml) {
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
return object;
}
@SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, InputStream is) {
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
return object;
}
/**
* pojo -> xml
*
* @param clazz
* @param object
* @return
*/
public static <T> String toXml(Class<T> clazz, T object) {
return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
}
private static Map<Class, XStream> configXStreamInstance() {
Map<Class, XStream> map = new HashMap<Class, XStream>();
map.put(WxCpXmlMessage.class, config_WxCpXmlMessage());
map.put(WxCpXmlOutNewsMessage.class, config_WxCpXmlOutNewsMessage());
map.put(WxCpXmlOutTextMessage.class, config_WxCpXmlOutTextMessage());
map.put(WxCpXmlOutImageMessage.class, config_WxCpXmlOutImageMessage());
map.put(WxCpXmlOutVideoMessage.class, config_WxCpXmlOutVideoMessage());
map.put(WxCpXmlOutVoiceMessage.class, config_WxCpXmlOutVoiceMessage());
return map;
}
private static XStream config_WxCpXmlMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlMessage.class);
xstream.processAnnotations(WxCpXmlMessage.ScanCodeInfo.class);
xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.class);
xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.Item.class);
xstream.processAnnotations(WxCpXmlMessage.SendLocationInfo.class);
return xstream;
}
private static XStream config_WxCpXmlOutImageMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutImageMessage.class);
return xstream;
}
private static XStream config_WxCpXmlOutNewsMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutNewsMessage.class);
xstream.processAnnotations(WxCpXmlOutNewsMessage.Item.class);
return xstream;
}
private static XStream config_WxCpXmlOutTextMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutTextMessage.class);
return xstream;
}
private static XStream config_WxCpXmlOutVideoMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutVideoMessage.class);
xstream.processAnnotations(WxCpXmlOutVideoMessage.Video.class);
return xstream;
}
private static XStream config_WxCpXmlOutVoiceMessage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutVoiceMessage.class);
return xstream;
}
}

View File

@@ -1,93 +0,0 @@
package me.chanjar.weixin.cp.util.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.*;
import me.chanjar.weixin.cp.bean.*;
import org.xml.sax.InputSource;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class XmlTransformer {
protected static final JAXBContext JAXB_CONTEXT = initJAXBContext();
/**
* xml -> pojo
*
* @param clazz
* @param xml
* @return
* @throws JAXBException
*/
@SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
T object = (T) um.unmarshal(new StringReader(xml));
return object;
}
@SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8");
T object = (T) um.unmarshal(inputSource);
return object;
}
/**
* pojo -> xml
*
* @param clazz
* @param object
* @return
* @throws JAXBException
*/
public static <T> String toXml(Class<T> clazz, T object) throws JAXBException {
StringWriter stringWriter = new StringWriter();
toXml(clazz, object, stringWriter);
return stringWriter.getBuffer().toString();
}
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
Marshaller m = JAXB_CONTEXT.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(CharacterEscapeHandler.class.getName(), CHAR_ESCAPE_HANDLER);
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
m.marshal(object, writer);
}
protected static final CharacterEscapeHandler CHAR_ESCAPE_HANDLER = new CharacterUnescapeHandler();
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
writer.write(ac, i, j);
}
}
private static JAXBContext initJAXBContext() {
/*
* JAXBContext对象是线程安全的根据官方文档的建议将对象作为全局实例
* https://jaxb.java.net/guide/Performance_and_thread_safety.html
*/
try {
return JAXBContext.newInstance(
WxCpXmlOutMessage.class,
WxCpXmlOutImageMessage.class,
WxCpXmlOutNewsMessage.class,
WxCpXmlOutTextMessage.class,
WxCpXmlOutVideoMessage.class,
WxCpXmlOutVoiceMessage.class,
WxCpXmlMessage.class);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,23 +1,17 @@
package me.chanjar.weixin.cp.api;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.google.inject.Binder;
import com.google.inject.Module;
import org.xml.sax.InputSource;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import java.io.InputStream;
public class ApiTestModule implements Module {
@Override
public void configure(Binder binder) {
try {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1);
WxCpServiceImpl wxService = new WxCpServiceImpl();
@@ -25,21 +19,16 @@ public class ApiTestModule implements Module {
binder.bind(WxCpServiceImpl.class).toInstance(wxService);
binder.bind(WxCpConfigStorage.class).toInstance(config);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8");
T object = (T) um.unmarshal(inputSource);
return object;
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);
}
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
public static class WxXmlCpInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
protected String userId;

View File

@@ -1,14 +1,12 @@
package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/**
* 基础API测试
* @author Daniel Qian

View File

@@ -1,15 +1,13 @@
package me.chanjar.weixin.cp.api;
import java.util.List;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.util.List;
/**
* 测试部门接口

View File

@@ -1,20 +1,18 @@
package me.chanjar.weixin.cp.api;
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;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 测试多媒体文件上传下载

View File

@@ -1,14 +1,12 @@
package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/***
* 测试发送消息
* @author Daniel Qian

View File

@@ -1,7 +1,5 @@
package me.chanjar.weixin.cp.api;
import java.util.Map;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
@@ -9,6 +7,8 @@ import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Map;
/**
* 测试消息路由器
* @author Daniel Qian

View File

@@ -1,9 +1,9 @@
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 me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

View File

@@ -1,19 +1,15 @@
package me.chanjar.weixin.cp.api;
import javax.xml.bind.JAXBException;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.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.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
/**
* 测试菜单
* @author Daniel Qian
@@ -42,7 +38,7 @@ public class WxMenuAPITest {
}
@DataProvider(name="menu")
public Object[][] getMenu() throws JAXBException {
public Object[][] getMenu() {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.BUTTON_CLICK);

View File

@@ -1,11 +1,10 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage.WxArticle;
import org.testng.Assert;
import org.testng.annotations.Test;
import me.chanjar.weixin.cp.bean.WxCpMessage.WxArticle;
@Test
public class WxCpMessageTest {

View File

@@ -1,21 +1,16 @@
package me.chanjar.weixin.cp.demo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage;
import org.xml.sax.InputSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.InputStream;
/**
* @author Daniel Qian
*/
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
@Override
@@ -25,11 +20,10 @@ class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
}
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) throws JAXBException {
Unmarshaller um = JAXBContext.newInstance(WxCpDemoInMemoryConfigStorage.class).createUnmarshaller();
InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8");
return (WxCpDemoInMemoryConfigStorage) um.unmarshal(inputSource);
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpDemoInMemoryConfigStorage.class);
return (WxCpDemoInMemoryConfigStorage) xstream.fromXML(is);
}
}

View File

@@ -8,7 +8,6 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import javax.xml.bind.JAXBException;
import java.io.InputStream;
import java.util.Map;
@@ -37,7 +36,6 @@ public class WxCpDemoServer {
}
private static void initWeixin() {
try {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1);
@@ -87,8 +85,5 @@ public class WxCpDemoServer {
.end()
;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,20 +1,18 @@
package me.chanjar.weixin.cp.demo;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.cp.api.*;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
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 javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
/**
* @author Daniel Qian