整理及重构

This commit is contained in:
Daniel Qian
2014-10-22 10:29:47 +08:00
parent 67795a092d
commit a8f5d07ff3
212 changed files with 1701 additions and 3459 deletions

View File

@@ -0,0 +1,43 @@
/**
* 对公众平台发送给公众账号的消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
// ------------------------------------------------------------------------
/**
* 针对org.apache.commons.codec.binary.Base64
* 需要导入架包commons-codec-1.9或commons-codec-1.8等其他版本)
* 官方下载地址http://commons.apache.org/proper/commons-codec/download_codec.cgi
*/
package me.chanjar.weixin.cp.util.crypto;
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import org.apache.commons.codec.binary.Base64;
public class WxCpCryptUtil extends WxCryptUtil {
/**
* 构造函数
*
* @param wxCpConfigStorage
*/
public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) {
/*
* @param token 公众平台上开发者设置的token
* @param encodingAesKey 公众平台上开发者设置的EncodingAESKey
* @param appidOrCorpid 公众平台appid
*/
String encodingAesKey = wxCpConfigStorage.getAesKey();
String token = wxCpConfigStorage.getToken();
String corpId = wxCpConfigStorage.getCorpId();
this.token = token;
this.appidOrCorpid = corpId;
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
}
}

View File

@@ -0,0 +1,65 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package me.chanjar.weixin.cp.util.json;
import java.lang.reflect.Type;
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;
/**
* @author Daniel Qian
*/
public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDeserializer<WxCpDepart> {
public JsonElement serialize(WxCpDepart group, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
if (group.getId() != null) {
json.addProperty("id", group.getId());
}
if (group.getName() != null) {
json.addProperty("name", group.getName());
}
if (group.getParentId() != null) {
json.addProperty("parentid", group.getParentId());
}
if (group.getOrder() != null) {
json.addProperty("order", String.valueOf(group.getOrder()));
}
return json;
}
public WxCpDepart deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
WxCpDepart depart = new WxCpDepart();
JsonObject departJson = json.getAsJsonObject();
if (departJson.get("id") != null && !departJson.get("id").isJsonNull()) {
depart.setId(GsonHelper.getAsInteger(departJson.get("id")));
}
if (departJson.get("name") != null && !departJson.get("name").isJsonNull()) {
depart.setName(GsonHelper.getAsString(departJson.get("name")));
}
if (departJson.get("order") != null && !departJson.get("order").isJsonNull()) {
depart.setOrder(GsonHelper.getAsInteger(departJson.get("order")));
}
if (departJson.get("parentid") != null && !departJson.get("parentid").isJsonNull()) {
depart.setParentId(GsonHelper.getAsInteger(departJson.get("parentid")));
}
return depart;
}
}

View File

@@ -0,0 +1,28 @@
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.*;
public class WxCpGsonBuilder {
public static final GsonBuilder INSTANCE = new GsonBuilder();
static {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter());
}
public static Gson create() {
return INSTANCE.create();
}
}

View File

@@ -0,0 +1,93 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package me.chanjar.weixin.cp.util.json;
import java.lang.reflect.Type;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.commons.lang3.StringUtils;
/**
*
* @author Daniel Qian
*
*/
public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("agentid", message.getAgentId());
if (StringUtils.isNotBlank(message.getToUser())) {
messageJson.addProperty("touser", message.getToUser());
}
messageJson.addProperty("msgtype", message.getMsgType());
if (StringUtils.isNotBlank(message.getToParty())) {
messageJson.addProperty("toparty", message.getToUser());
}
if (StringUtils.isNotBlank(message.getToTag())) {
messageJson.addProperty("totag", message.getToUser());
}
if (WxCpConsts.CUSTOM_MSG_TEXT.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("text", text);
}
if (WxCpConsts.CUSTOM_MSG_IMAGE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("image", image);
}
if (WxCpConsts.CUSTOM_MSG_FILE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("file", image);
}
if (WxCpConsts.CUSTOM_MSG_VOICE.equals(message.getMsgType())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMediaId());
messageJson.add("voice", voice);
}
if (WxCpConsts.CUSTOM_MSG_VIDEO.equals(message.getMsgType())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMediaId());
video.addProperty("thumb_media_id", message.getThumbMediaId());
video.addProperty("title", message.getTitle());
video.addProperty("description", message.getDescription());
messageJson.add("video", video);
}
if (WxCpConsts.CUSTOM_MSG_NEWS.equals(message.getMsgType())) {
JsonArray articleJsonArray = new JsonArray();
for (WxCpMessage.WxArticle article : message.getArticles()) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("description", article.getDescription());
articleJson.addProperty("url", article.getUrl());
articleJson.addProperty("picurl", article.getPicUrl());
articleJsonArray.add(articleJson);
}
messageJson.add("articles", articleJsonArray);
}
return messageJson;
}
}

View File

@@ -0,0 +1,35 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
package me.chanjar.weixin.cp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.bean.WxCpTag;
import java.lang.reflect.Type;
/**
* @author Daniel Qian
*/
public class WxCpTagGsonAdapter implements JsonSerializer<WxCpTag>, JsonDeserializer<WxCpTag> {
public JsonElement serialize(WxCpTag group, Type typeOfSrc, JsonSerializationContext context) {
JsonObject o = new JsonObject();
o.addProperty("tagid", group.getId());
o.addProperty("tagname", group.getName());
return o;
}
public WxCpTag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
return new WxCpTag(GsonHelper.getString(jsonObject, "tagid"), GsonHelper.getString(jsonObject, "name"));
}
}

View File

@@ -0,0 +1,115 @@
/*
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
*
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
* arose from modification of the original source, or other redistribution of this source
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
*/
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;
/**
* @author Daniel Qian
*/
public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSerializer<WxCpUser> {
public WxCpUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject o = json.getAsJsonObject();
WxCpUser user = new WxCpUser();
user.setUserId(GsonHelper.getString(o, "userid"));
user.setName(GsonHelper.getString(o, "name"));
if(o.get("department") != null) {
JsonArray departJsonArray = o.get("department").getAsJsonArray();
Integer[] departIds = new Integer[departJsonArray.size()];
int i = 0;
for (JsonElement jsonElement : departJsonArray) {
departIds[i++] = jsonElement.getAsInt();
}
user.setDepartIds(departIds);
}
user.setPosition(GsonHelper.getString(o, "position"));
user.setMobile(GsonHelper.getString(o, "mobile"));
Integer gender = GsonHelper.getInteger(o, "gender");
if (new Integer(1).equals(gender)) {
user.setGender("");
} else if (new Integer(2).equals(gender)) {
user.setGender("");
} else {
user.setGender("未知");
}
user.setTel(GsonHelper.getString(o, "tel"));
user.setEmail(GsonHelper.getString(o, "email"));
user.setWeiXinId(GsonHelper.getString(o, "weixinid"));
if (GsonHelper.isNotNull(o.get("extattr"))) {
JsonArray attrJsonElements = o.get("extattr").getAsJsonObject().get("attrs").getAsJsonArray();
for (JsonElement attrJsonElement : attrJsonElements) {
WxCpUser.Attr attr = new WxCpUser.Attr(
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"),
GsonHelper.getString(attrJsonElement.getAsJsonObject(), "value")
);
user.getExtAttrs().add(attr);
}
}
return user;
}
@Override
public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) {
JsonObject o = new JsonObject();
if (user.getUserId() != null) {
o.addProperty("userid", user.getUserId());
}
if (user.getName() != null) {
o.addProperty("name", user.getName());
}
if (user.getDepartIds() != null) {
JsonArray jsonArray = new JsonArray();
for (Integer departId : user.getDepartIds()) {
jsonArray.add(new JsonPrimitive(departId));
}
o.add("department", jsonArray);
}
if (user.getPosition() != null) {
o.addProperty("position", user.getPosition());
}
if (user.getMobile() != null) {
o.addProperty("mobile", user.getMobile());
}
if (user.getGender() != null) {
o.addProperty("gender", user.getGender().equals("") ? 0 : 1);
}
if (user.getTel() != null) {
o.addProperty("tel", user.getTel());
}
if (user.getEmail() != null) {
o.addProperty("email", user.getEmail());
}
if (user.getWeiXinId() != null) {
o.addProperty("weixinid", user.getWeiXinId());
}
if (user.getExtAttrs().size() > 0) {
JsonArray attrsJsonArray = new JsonArray();
for (WxCpUser.Attr attr : user.getExtAttrs()) {
JsonObject attrJson = new JsonObject();
attrJson.addProperty("name", attr.getName());
attrJson.addProperty("value", attr.getValue());
attrsJsonArray.add(attrJson);
}
JsonObject attrsJson = new JsonObject();
attrsJson.add("attrs", attrsJsonArray);
o.add("extattr", attrsJson);
}
return o;
}
}

View File

@@ -0,0 +1,92 @@
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.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,
WxCpXmlOutMewsMessage.class,
WxCpXmlOutTextMessage.class,
WxCpXmlOutVideoMessage.class,
WxCpXmlOutVoiceMessage.class,
WxCpXmlMessage.class);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}