fwq
2022-05-11 4a044670a4b8a9bcc0a38b413ef803b9842a5052
提交 | 用户 | age
5c5945 1 package com.hx.mp.util;
E 2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.UnsupportedEncodingException;
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  * Ӧ������ Ӧ������̳д��࣬��дisTenpaySign�������ɡ�
17  *JX
18  *linhan
19  */
20 public class ResponseHandler {
21
22     /** ��Կ */
23     private String key;
24
25     /** Ӧ��IJ��� */
26     @SuppressWarnings("rawtypes")
27     private SortedMap parameters;
28
29     /** debug��Ϣ */
30     private String debugInfo;
31
32     private HttpServletRequest request;
33
34     private HttpServletResponse response;
35
36     private String uriEncoding;
37
38     /**
39      * ���캯��
40      * 
41      * @param request
42      * @param response
43      */
44     @SuppressWarnings("rawtypes")
45     public ResponseHandler(HttpServletRequest request,
46             HttpServletResponse response) {
47         this.request = request;
48         this.response = response;
49
50         this.key = "";
51         this.parameters = new TreeMap();
52         this.debugInfo = "";
53
54         this.uriEncoding = "";
55
56         Map m = this.request.getParameterMap();
57         Iterator it = m.keySet().iterator();
58         while (it.hasNext()) {
59             String k = (String) it.next();
60             String v = ((String[]) m.get(k))[0];
61             this.setParameter(k, v);
62         }
63
64     }
65
66     /**
67      * ��ȡ��Կ
68      */
69     public String getKey() {
70         return key;
71     }
72
73     /**
74      * ������Կ
75      */
76     public void setKey(String key) {
77         this.key = key;
78     }
79
80     /**
81      * ��ȡ����ֵ
82      * 
83      * @param parameter
84      *            �������
85      * @return String
86      */
87     public String getParameter(String parameter) {
88         String s = (String) this.parameters.get(parameter);
89         return (null == s) ? "" : s;
90     }
91
92     /**
93      * ���ò���ֵ
94      * 
95      * @param parameter
96      *            �������
97      * @param parameterValue
98      *            ����ֵ
99      */
100     @SuppressWarnings("unchecked")
101     public void setParameter(String parameter, String parameterValue) {
102         String v = "";
103         if (null != parameterValue) {
104             v = parameterValue.trim();
105         }
106         this.parameters.put(parameter, v);
107     }
108
109     /**
110      * �������еIJ���
111      * 
112      * @return SortedMap
113      */
114     @SuppressWarnings("rawtypes")
115     public SortedMap getAllParameters() {
116         return this.parameters;
117     }
118
119     /**
120      * �Ƿ�Ƹ�ͨǩ��,������:���������a-z����,������ֵ�IJ���μ�ǩ��
121      * 
122      * @return boolean
123      */
124     @SuppressWarnings("rawtypes")
125     public boolean isTenpaySign() {
126         StringBuffer sb = new StringBuffer();
127         Set es = this.parameters.entrySet();
128         Iterator it = es.iterator();
129         while (it.hasNext()) {
130             Map.Entry entry = (Map.Entry) it.next();
131             String k = (String) entry.getKey();
132             String v = (String) entry.getValue();
133             if (!"sign".equals(k) && null != v && !"".equals(v)) {
134                 sb.append(k + "=" + v + "&");
135             }
136         }
137
138         sb.append("key=" + this.getKey());
139
140         // ���ժҪ
141         String enc = TenpayUtil.getCharacterEncoding(this.request,
142                 this.response);
143         String sign = MD5Util.MD5Encode(sb.toString(), enc).toLowerCase();
144
145         String tenpaySign = this.getParameter("sign").toLowerCase();
146
147         // debug��Ϣ
148         this.setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:"
149                 + tenpaySign);
150
151         return tenpaySign.equals(sign);
152     }
153
154     /**
155      * ���ش������Ƹ�ͨ��������
156      * 
157      * @param msg
158      *            : Success or fail��
159      * @throws IOException
160      */
161     public void sendToCFT(String msg) throws IOException {
162         String strHtml = msg;
163         PrintWriter out = this.getHttpServletResponse().getWriter();
164         out.println(strHtml);
165         out.flush();
166         out.close();
167
168     }
169
170     /**
171      * ��ȡuri����
172      * 
173      * @return String
174      */
175     public String getUriEncoding() {
176         return uriEncoding;
177     }
178
179     /**
180      * ����uri����
181      * 
182      * @param uriEncoding
183      * @throws UnsupportedEncodingException
184      */
185     @SuppressWarnings("rawtypes")
186     public void setUriEncoding(String uriEncoding)
187             throws UnsupportedEncodingException {
188         if (!"".equals(uriEncoding.trim())) {
189             this.uriEncoding = uriEncoding;
190
191             // ����ת��
192             String enc = TenpayUtil.getCharacterEncoding(request, response);
193             Iterator it = this.parameters.keySet().iterator();
194             while (it.hasNext()) {
195                 String k = (String) it.next();
196                 String v = this.getParameter(k);
197                 v = new String(v.getBytes(uriEncoding.trim()), enc);
198                 this.setParameter(k, v);
199             }
200         }
201     }
202
203     /**
204      * ��ȡdebug��Ϣ
205      */
206     public String getDebugInfo() {
207         return debugInfo;
208     }
209
210     /**
211      * ����debug��Ϣ
212      */
213     protected void setDebugInfo(String debugInfo) {
214         this.debugInfo = debugInfo;
215     }
216
217     protected HttpServletRequest getHttpServletRequest() {
218         return this.request;
219     }
220
221     protected HttpServletResponse getHttpServletResponse() {
222         return this.response;
223     }
224
225 }