提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import org.jdom.JDOMException; |
|
4 |
|
|
5 |
import java.io.IOException; |
|
6 |
import java.util.*; |
|
7 |
|
|
8 |
/** |
|
9 |
* ��̨Ӧ����<br/> |
|
10 |
* ========================================================================<br/> |
|
11 |
* api˵����<br/> |
|
12 |
* getKey()/setKey(),��ȡ/������Կ<br/> |
|
13 |
* getContent() / setContent(), ��ȡ/����ԭʼ����<br/> |
|
14 |
* getParameter()/setParameter(),��ȡ/���ò���ֵ<br/> |
|
15 |
* getAllParameters(),��ȡ���в���<br/> |
|
16 |
* isTenpaySign(),�Ƿ�Ƹ�ͨǩ��,true:�� false:��<br/> |
|
17 |
* getDebugInfo(),��ȡdebug��Ϣ<br/> |
|
18 |
* |
|
19 |
* ========================================================================<br/> |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class ClientResponseHandler { |
|
23 |
|
|
24 |
/** Ӧ��ԭʼ���� */ |
|
25 |
private String content; |
|
26 |
|
|
27 |
/** Ӧ��IJ��� */ |
|
28 |
@SuppressWarnings("rawtypes") |
|
29 |
private SortedMap parameters; |
|
30 |
|
|
31 |
/** debug��Ϣ */ |
|
32 |
private String debugInfo; |
|
33 |
|
|
34 |
/** ��Կ */ |
|
35 |
private String key; |
|
36 |
|
|
37 |
/** �ַ� */ |
|
38 |
private String charset; |
|
39 |
|
|
40 |
/*@SuppressWarnings("rawtypes") |
|
41 |
public ClientResponseHandler() { |
|
42 |
this.content = ""; |
|
43 |
this.parameters = new TreeMap(); |
|
44 |
this.debugInfo = ""; |
|
45 |
this.key = ""; |
|
46 |
this.charset = "UTF-8"; |
|
47 |
}*/ |
|
48 |
|
|
49 |
public ClientResponseHandler() { |
|
50 |
this.content = ""; |
|
51 |
this.parameters = new TreeMap(); |
|
52 |
this.debugInfo = ""; |
|
53 |
this.key = ""; |
|
54 |
this.charset = "UTF-8"; |
|
55 |
} |
|
56 |
|
|
57 |
public String getContent() { |
|
58 |
return content; |
|
59 |
} |
|
60 |
|
|
61 |
public void setContent(String content) throws Exception { |
|
62 |
this.content = content; |
|
63 |
|
|
64 |
this.doParse(); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* ��ȡ����ֵ |
|
69 |
* |
|
70 |
* @param parameter |
|
71 |
* ������� |
|
72 |
* @return String |
|
73 |
*/ |
|
74 |
public String getParameter(String parameter) { |
|
75 |
String s = (String) this.parameters.get(parameter); |
|
76 |
return (null == s) ? "" : s; |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* ���ò���ֵ |
|
81 |
* |
|
82 |
* @param parameter |
|
83 |
* ������� |
|
84 |
* @param parameterValue |
|
85 |
* ����ֵ |
|
86 |
*/ |
|
87 |
@SuppressWarnings("unchecked") |
|
88 |
public void setParameter(String parameter, String parameterValue) { |
|
89 |
String v = ""; |
|
90 |
if (null != parameterValue) { |
|
91 |
v = parameterValue.trim(); |
|
92 |
} |
|
93 |
this.parameters.put(parameter, v); |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* �������еIJ��� |
|
98 |
* |
|
99 |
* @return SortedMap |
|
100 |
*/ |
|
101 |
@SuppressWarnings("rawtypes") |
|
102 |
public SortedMap getAllParameters() { |
|
103 |
return this.parameters; |
|
104 |
} |
|
105 |
|
|
106 |
public String getDebugInfo() { |
|
107 |
return debugInfo; |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* ��ȡ��Կ |
|
112 |
*/ |
|
113 |
public String getKey() { |
|
114 |
return key; |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* ������Կ |
|
119 |
*/ |
|
120 |
public void setKey(String key) { |
|
121 |
this.key = key; |
|
122 |
} |
|
123 |
|
|
124 |
public String getCharset() { |
|
125 |
return this.charset; |
|
126 |
} |
|
127 |
|
|
128 |
public void setCharset(String charset) { |
|
129 |
this.charset = charset; |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* �Ƿ�Ƹ�ͨǩ��,������:���������a-z����,������ֵ�IJ���μ�ǩ�� |
|
134 |
* |
|
135 |
* @return boolean |
|
136 |
*/ |
|
137 |
@SuppressWarnings("rawtypes") |
|
138 |
public boolean isTenpaySign() { |
|
139 |
StringBuffer sb = new StringBuffer(); |
|
140 |
Set es = this.parameters.entrySet(); |
|
141 |
Iterator it = es.iterator(); |
|
142 |
while (it.hasNext()) { |
|
143 |
Map.Entry entry = (Map.Entry) it.next(); |
|
144 |
String k = (String) entry.getKey(); |
|
145 |
String v = (String) entry.getValue(); |
|
146 |
if (!"sign".equals(k) && null != v && !"".equals(v)) { |
|
147 |
sb.append(k + "=" + v + "&"); |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
sb.append("key=" + this.getKey()); |
|
152 |
|
|
153 |
System.out.println("返回的参数:" + sb); |
|
154 |
// ���ժҪ |
|
155 |
String sign = MD5Util.MD5Encode(sb.toString(), this.charset) |
|
156 |
.toUpperCase(); |
|
157 |
|
|
158 |
String tenpaySign = this.getParameter("sign").toUpperCase(); |
|
159 |
|
|
160 |
// debug��Ϣ |
|
161 |
this.setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:" |
|
162 |
+ tenpaySign); |
|
163 |
|
|
164 |
Boolean b = tenpaySign.equals(sign); |
|
165 |
|
|
166 |
System.out.println("验证返回来的数据是否微信返回的:" + b); |
|
167 |
|
|
168 |
return b; |
|
169 |
} |
|
170 |
|
|
171 |
/** |
|
172 |
* �Ƿ�Ƹ�ͨǩ�� |
|
173 |
* |
|
174 |
* @param signParameterArray |
|
175 |
* ǩ��IJ������� |
|
176 |
* @return boolean |
|
177 |
*/ |
|
178 |
protected boolean isTenpaySign(String signParameterArray[]) { |
|
179 |
|
|
180 |
StringBuffer signPars = new StringBuffer(); |
|
181 |
for (int index = 0; index < signParameterArray.length; index++) { |
|
182 |
String k = signParameterArray[index]; |
|
183 |
String v = this.getParameter(k); |
|
184 |
if (null != v && !"".equals(v)) { |
|
185 |
signPars.append(k + "=" + v + "&"); |
|
186 |
} |
|
187 |
} |
|
188 |
|
|
189 |
signPars.append("key=" + this.getKey()); |
|
190 |
|
|
191 |
// ���ժҪ |
|
192 |
String sign = MD5Util.MD5Encode(signPars.toString(), this.charset) |
|
193 |
.toLowerCase(); |
|
194 |
|
|
195 |
String tenpaySign = this.getParameter("sign").toLowerCase(); |
|
196 |
|
|
197 |
// debug��Ϣ |
|
198 |
this.setDebugInfo(signPars.toString() + " => sign:" + sign |
|
199 |
+ " tenpaySign:" + tenpaySign); |
|
200 |
|
|
201 |
return tenpaySign.equals(sign); |
|
202 |
} |
|
203 |
|
|
204 |
protected void setDebugInfo(String debugInfo) { |
|
205 |
this.debugInfo = debugInfo; |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* ����XML���� |
|
210 |
*/ |
|
211 |
@SuppressWarnings("rawtypes") |
|
212 |
protected void doParse() throws JDOMException, IOException, JDOMException { |
|
213 |
String xmlContent = this.getContent(); |
|
214 |
|
|
215 |
// ����xml,�õ�map |
|
216 |
Map m = XMLUtil.doXMLParse(xmlContent); |
|
217 |
|
|
218 |
// ���� |
|
219 |
Iterator it = m.keySet().iterator(); |
|
220 |
while (it.hasNext()) { |
|
221 |
String k = (String) it.next(); |
|
222 |
String v = (String) m.get(k); |
|
223 |
this.setParameter(k, v); |
|
224 |
} |
|
225 |
|
|
226 |
} |
|
227 |
|
|
228 |
} |