import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.zttx.bert.wx.parser.WxMsgKit;
import org.zttx.bert.wx.vo.recv.WxRecvMsg;
import org.zttx.bert.wx.vo.send.WxSendMsg;
/**
* @ClassName: WeiXin.java
* @Description: 微信帮助类
* @author Bert
* 2014-3-31 下午10:10:13
*/
public final class WeiXin {
/**
* 认证接口
* @param token
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean access(String token, String signature, String timestamp, String nonce) {
List<String> ss = new ArrayList<String>();
ss.add(timestamp);
ss.add(nonce);
ss.add(token);
Collections.sort(ss);
StringBuilder builder = new StringBuilder();
for (String s : ss) {
builder.append(s);
}
return signature.equalsIgnoreCase(HashKit.sha1(builder.toString()));
}
/**
* 解析微信请求
* @param in
* @return
* @throws JDOMException
* @throws IOException
*/
public static WxRecvMsg recv(InputStream in) throws JDOMException, IOException {
return WxMsgKit.parse(in);
}
public static void send(WxSendMsg msg, OutputStream out) throws JDOMException, IOException {
Document doc = WxMsgKit.parse(msg);
if (null != doc) {
new XMLOutputter().output(doc, out);
} else {
Logger.getAnonymousLogger().warning("发送消息时,解析出dom为空 msg :" + msg);
}
}
/**
* 绑定xml数据
* @param msg
* @return
*/
public static WxSendMsg builderSendByRecv(WxRecvMsg msg) {
WxRecvMsg m = new WxRecvMsg(msg);
String from = m.getFromUser();
m.setFromUser(m.getToUser());
m.setToUser(from);
m.setCreateDt((System.currentTimeMillis() / 1000) + "");
return new WxSendMsg(m);
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.zttx.bert.wx.parser.WxMsgKit;
import org.zttx.bert.wx.vo.recv.WxRecvMsg;
import org.zttx.bert.wx.vo.send.WxSendMsg;
/**
* @ClassName: WeiXin.java
* @Description: 微信帮助类
* @author Bert
* 2014-3-31 下午10:10:13
*/
public final class WeiXin {
/**
* 认证接口
* @param token
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean access(String token, String signature, String timestamp, String nonce) {
List<String> ss = new ArrayList<String>();
ss.add(timestamp);
ss.add(nonce);
ss.add(token);
Collections.sort(ss);
StringBuilder builder = new StringBuilder();
for (String s : ss) {
builder.append(s);
}
return signature.equalsIgnoreCase(HashKit.sha1(builder.toString()));
}
/**
* 解析微信请求
* @param in
* @return
* @throws JDOMException
* @throws IOException
*/
public static WxRecvMsg recv(InputStream in) throws JDOMException, IOException {
return WxMsgKit.parse(in);
}
public static void send(WxSendMsg msg, OutputStream out) throws JDOMException, IOException {
Document doc = WxMsgKit.parse(msg);
if (null != doc) {
new XMLOutputter().output(doc, out);
} else {
Logger.getAnonymousLogger().warning("发送消息时,解析出dom为空 msg :" + msg);
}
}
/**
* 绑定xml数据
* @param msg
* @return
*/
public static WxSendMsg builderSendByRecv(WxRecvMsg msg) {
WxRecvMsg m = new WxRecvMsg(msg);
String from = m.getFromUser();
m.setFromUser(m.getToUser());
m.setToUser(from);
m.setCreateDt((System.currentTimeMillis() / 1000) + "");
return new WxSendMsg(m);
}
}