提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.UnsupportedEncodingException; |
|
5 |
import java.net.URLEncoder; |
|
6 |
import java.util.Iterator; |
|
7 |
import java.util.Map; |
|
8 |
import java.util.Set; |
|
9 |
import java.util.SortedMap; |
|
10 |
import java.util.TreeMap; |
|
11 |
|
|
12 |
import javax.servlet.http.HttpServletRequest; |
|
13 |
import javax.servlet.http.HttpServletResponse; |
|
14 |
|
|
15 |
/** |
|
16 |
* �������� ��������̳д��࣬��дcreateSign�������ɡ� |
|
17 |
*JX |
|
18 |
*linhan |
|
19 |
*/ |
|
20 |
public class RequestHandler { |
|
21 |
|
|
22 |
/** ���url��ַ */ |
|
23 |
private String gateUrl; |
|
24 |
|
|
25 |
/** ��Կ */ |
|
26 |
private String key; |
|
27 |
|
|
28 |
/** ����IJ��� */ |
|
29 |
@SuppressWarnings("rawtypes") |
|
30 |
private SortedMap parameters; |
|
31 |
|
|
32 |
/** debug��Ϣ */ |
|
33 |
private String debugInfo; |
|
34 |
|
|
35 |
private HttpServletRequest request; |
|
36 |
|
|
37 |
private HttpServletResponse response; |
|
38 |
|
|
39 |
/** |
|
40 |
* ���캯�� |
|
41 |
* |
|
42 |
* @param request |
|
43 |
* @param response |
|
44 |
*/ |
|
45 |
/*@SuppressWarnings("rawtypes") |
|
46 |
public RequestHandler(HttpServletRequest request, |
|
47 |
HttpServletResponse response) { |
|
48 |
this.request = request; |
|
49 |
this.response = response; |
|
50 |
|
|
51 |
this.gateUrl = "https://gw.tenpay.com/gateway/pay.htm"; |
|
52 |
this.key = ""; |
|
53 |
this.parameters = new TreeMap(); |
|
54 |
this.debugInfo = ""; |
|
55 |
}*/ |
|
56 |
|
|
57 |
/** |
|
58 |
* ���캯�� |
|
59 |
* @param request |
|
60 |
* @param response |
|
61 |
*/ |
|
62 |
public RequestHandler(HttpServletRequest request, HttpServletResponse response) { |
|
63 |
this.request = request; |
|
64 |
this.response = response; |
|
65 |
|
|
66 |
this.gateUrl = "https://gw.tenpay.com/gateway/pay.htm"; |
|
67 |
this.key = ""; |
|
68 |
this.parameters = new TreeMap(); |
|
69 |
this.debugInfo = ""; |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* ��ʼ������ |
|
74 |
*/ |
|
75 |
public void init() { |
|
76 |
// nothing to do |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* ��ȡ��ڵ�ַ,�������ֵ |
|
81 |
*/ |
|
82 |
public String getGateUrl() { |
|
83 |
return gateUrl; |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* ������ڵ�ַ,�������ֵ |
|
88 |
*/ |
|
89 |
public void setGateUrl(String gateUrl) { |
|
90 |
this.gateUrl = gateUrl; |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* ��ȡ��Կ |
|
95 |
*/ |
|
96 |
public String getKey() { |
|
97 |
return key; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 秘钥 |
|
102 |
*/ |
|
103 |
public void setKey(String key) { |
|
104 |
this.key = key; |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* ��ȡ����ֵ |
|
109 |
* |
|
110 |
* @param parameter |
|
111 |
* ������� |
|
112 |
* @return String |
|
113 |
*/ |
|
114 |
public String getParameter(String parameter) { |
|
115 |
String s = (String) this.parameters.get(parameter); |
|
116 |
return (null == s) ? "" : s; |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* ���ò���ֵ |
|
121 |
* |
|
122 |
* @param parameter |
|
123 |
* ������� |
|
124 |
* @param parameterValue |
|
125 |
* ����ֵ |
|
126 |
*/ |
|
127 |
@SuppressWarnings("unchecked") |
|
128 |
public void setParameter(String parameter, String parameterValue) { |
|
129 |
String v = ""; |
|
130 |
if (null != parameterValue) { |
|
131 |
v = parameterValue.trim(); |
|
132 |
} |
|
133 |
this.parameters.put(parameter, v); |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* �������еIJ��� |
|
138 |
* |
|
139 |
* @return SortedMap |
|
140 |
*/ |
|
141 |
@SuppressWarnings("rawtypes") |
|
142 |
public SortedMap getAllParameters() { |
|
143 |
return this.parameters; |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 |
* ��ȡdebug��Ϣ |
|
148 |
*/ |
|
149 |
public String getDebugInfo() { |
|
150 |
return debugInfo; |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* ��ȡ����������URL |
|
155 |
* |
|
156 |
* @return String |
|
157 |
* @throws UnsupportedEncodingException |
|
158 |
*/ |
|
159 |
/*@SuppressWarnings("rawtypes") |
|
160 |
public String getRequestURL() throws UnsupportedEncodingException { |
|
161 |
|
|
162 |
this.createSign(); |
|
163 |
|
|
164 |
StringBuffer sb = new StringBuffer(); |
|
165 |
String enc = TenpayUtil.getCharacterEncoding(this.request, |
|
166 |
this.response); |
|
167 |
Set es = this.parameters.entrySet(); |
|
168 |
Iterator it = es.iterator(); |
|
169 |
while (it.hasNext()) { |
|
170 |
Map.Entry entry = (Map.Entry) it.next(); |
|
171 |
String k = (String) entry.getKey(); |
|
172 |
String v = (String) entry.getValue(); |
|
173 |
|
|
174 |
if (!"spbill_create_ip".equals(k)) { |
|
175 |
sb.append(k + "=" + URLEncoder.encode(v, enc) + "&"); |
|
176 |
} else { |
|
177 |
sb.append(k + "=" + v.replace(".", "%2E") + "&"); |
|
178 |
|
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
// ȥ�����һ��& |
|
183 |
String reqPars = sb.substring(0, sb.lastIndexOf("&")); |
|
184 |
|
|
185 |
return this.getGateUrl() + "?" + reqPars; |
|
186 |
|
|
187 |
}*/ |
|
188 |
|
|
189 |
/** |
|
190 |
* ��ȡ����������URL |
|
191 |
* @return String |
|
192 |
* @throws UnsupportedEncodingException |
|
193 |
*/ |
|
194 |
public String getRequestURL() throws UnsupportedEncodingException { |
|
195 |
|
|
196 |
this.createSign(); |
|
197 |
|
|
198 |
StringBuffer sb = new StringBuffer(); |
|
199 |
String enc = TenpayUtil.getCharacterEncoding(this.request, this.response); |
|
200 |
Set es = this.parameters.entrySet(); |
|
201 |
Iterator it = es.iterator(); |
|
202 |
while(it.hasNext()) { |
|
203 |
Map.Entry entry = (Map.Entry)it.next(); |
|
204 |
String k = (String)entry.getKey(); |
|
205 |
String v = (String)entry.getValue(); |
|
206 |
|
|
207 |
if(!"spbill_create_ip".equals(k)) { |
|
208 |
sb.append(k + "=" + URLEncoder.encode(v, enc) + "&"); |
|
209 |
} else { |
|
210 |
sb.append(k + "=" + v.replace(".", "%2E") + "&"); |
|
211 |
|
|
212 |
|
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
//ȥ�����һ��& |
|
217 |
String reqPars = sb.substring(0, sb.lastIndexOf("&")); |
|
218 |
|
|
219 |
return this.getGateUrl() + "?" + reqPars; |
|
220 |
|
|
221 |
} |
|
222 |
|
|
223 |
public void doSend() throws UnsupportedEncodingException, IOException { |
|
224 |
this.response.sendRedirect(this.getRequestURL()); |
|
225 |
} |
|
226 |
|
|
227 |
/** |
|
228 |
* TODO 生成签名 |
|
229 |
*/ |
|
230 |
@SuppressWarnings("rawtypes") |
|
231 |
public String createSign() { |
|
232 |
StringBuffer sb = new StringBuffer(); |
|
233 |
Set es = this.parameters.entrySet(); |
|
234 |
Iterator it = es.iterator(); |
|
235 |
while (it.hasNext()) { |
|
236 |
Map.Entry entry = (Map.Entry) it.next(); |
|
237 |
String k = (String) entry.getKey(); |
|
238 |
String v = (String) entry.getValue(); |
|
239 |
if (null != v && !"".equals(v) && !"sign".equals(k) |
|
240 |
&& !"key".equals(k)) { |
|
241 |
sb.append(k + "=" + v + "&"); |
|
242 |
} |
|
243 |
} |
|
244 |
sb.append("key=" + this.getKey()); |
|
245 |
System.out.println("sb="+sb); |
|
246 |
String enc = TenpayUtil.getCharacterEncoding(this.request, |
|
247 |
this.response); |
|
248 |
String sign = MD5Util.MD5Encode(sb.toString(), enc).toUpperCase(); |
|
249 |
|
|
250 |
this.setParameter("sign", sign); |
|
251 |
|
|
252 |
// debug��Ϣ |
|
253 |
this.setDebugInfo(sb.toString() + " => sign:" + sign); |
|
254 |
|
|
255 |
return sign; |
|
256 |
} |
|
257 |
|
|
258 |
/** |
|
259 |
* ����debug��Ϣ |
|
260 |
*/ |
|
261 |
protected void setDebugInfo(String debugInfo) { |
|
262 |
this.debugInfo = debugInfo; |
|
263 |
} |
|
264 |
|
|
265 |
protected HttpServletRequest getHttpServletRequest() { |
|
266 |
return this.request; |
|
267 |
} |
|
268 |
|
|
269 |
protected HttpServletResponse getHttpServletResponse() { |
|
270 |
return this.response; |
|
271 |
} |
|
272 |
|
|
273 |
} |