package com.hx.mp.util; import org.jdom.JDOMException; import java.io.IOException; import java.util.*; /** * ��̨Ӧ����
* ========================================================================
* api˵����
* getKey()/setKey(),��ȡ/������Կ
* getContent() / setContent(), ��ȡ/����ԭʼ����
* getParameter()/setParameter(),��ȡ/���ò���ֵ
* getAllParameters(),��ȡ���в���
* isTenpaySign(),�Ƿ�Ƹ�ͨǩ��,true:�� false:��
* getDebugInfo(),��ȡdebug��Ϣ
* * ========================================================================
* */ public class ClientResponseHandler { /** Ӧ��ԭʼ���� */ private String content; /** Ӧ��IJ��� */ @SuppressWarnings("rawtypes") private SortedMap parameters; /** debug��Ϣ */ private String debugInfo; /** ��Կ */ private String key; /** �ַ� */ private String charset; /*@SuppressWarnings("rawtypes") public ClientResponseHandler() { this.content = ""; this.parameters = new TreeMap(); this.debugInfo = ""; this.key = ""; this.charset = "UTF-8"; }*/ public ClientResponseHandler() { this.content = ""; this.parameters = new TreeMap(); this.debugInfo = ""; this.key = ""; this.charset = "UTF-8"; } public String getContent() { return content; } public void setContent(String content) throws Exception { this.content = content; this.doParse(); } /** * ��ȡ����ֵ * * @param parameter * ������� * @return String */ public String getParameter(String parameter) { String s = (String) this.parameters.get(parameter); return (null == s) ? "" : s; } /** * ���ò���ֵ * * @param parameter * ������� * @param parameterValue * ����ֵ */ @SuppressWarnings("unchecked") public void setParameter(String parameter, String parameterValue) { String v = ""; if (null != parameterValue) { v = parameterValue.trim(); } this.parameters.put(parameter, v); } /** * �������еIJ��� * * @return SortedMap */ @SuppressWarnings("rawtypes") public SortedMap getAllParameters() { return this.parameters; } public String getDebugInfo() { return debugInfo; } /** * ��ȡ��Կ */ public String getKey() { return key; } /** * ������Կ */ public void setKey(String key) { this.key = key; } public String getCharset() { return this.charset; } public void setCharset(String charset) { this.charset = charset; } /** * �Ƿ�Ƹ�ͨǩ��,������:���������a-z����,������ֵ�IJ���μ�ǩ�� * * @return boolean */ @SuppressWarnings("rawtypes") public boolean isTenpaySign() { StringBuffer sb = new StringBuffer(); Set es = this.parameters.entrySet(); Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); String v = (String) entry.getValue(); if (!"sign".equals(k) && null != v && !"".equals(v)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + this.getKey()); System.out.println("返回的参数:" + sb); // ���ժҪ String sign = MD5Util.MD5Encode(sb.toString(), this.charset) .toUpperCase(); String tenpaySign = this.getParameter("sign").toUpperCase(); // debug��Ϣ this.setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:" + tenpaySign); Boolean b = tenpaySign.equals(sign); System.out.println("验证返回来的数据是否微信返回的:" + b); return b; } /** * �Ƿ�Ƹ�ͨǩ�� * * @param signParameterArray * ǩ��IJ������� * @return boolean */ protected boolean isTenpaySign(String signParameterArray[]) { StringBuffer signPars = new StringBuffer(); for (int index = 0; index < signParameterArray.length; index++) { String k = signParameterArray[index]; String v = this.getParameter(k); if (null != v && !"".equals(v)) { signPars.append(k + "=" + v + "&"); } } signPars.append("key=" + this.getKey()); // ���ժҪ String sign = MD5Util.MD5Encode(signPars.toString(), this.charset) .toLowerCase(); String tenpaySign = this.getParameter("sign").toLowerCase(); // debug��Ϣ this.setDebugInfo(signPars.toString() + " => sign:" + sign + " tenpaySign:" + tenpaySign); return tenpaySign.equals(sign); } protected void setDebugInfo(String debugInfo) { this.debugInfo = debugInfo; } /** * ����XML���� */ @SuppressWarnings("rawtypes") protected void doParse() throws JDOMException, IOException, JDOMException { String xmlContent = this.getContent(); // ����xml,�õ�map Map m = XMLUtil.doXMLParse(xmlContent); // ���ò��� Iterator it = m.keySet().iterator(); while (it.hasNext()) { String k = (String) it.next(); String v = (String) m.get(k); this.setParameter(k, v); } } }