View Javadoc
1   package com.foxinmy.weixin4j.request;
2   
3   import io.netty.handler.codec.DecoderResult;
4   import io.netty.handler.codec.http.HttpHeaders;
5   import io.netty.handler.codec.http.HttpMessage;
6   import io.netty.handler.codec.http.HttpMethod;
7   import io.netty.handler.codec.http.HttpVersion;
8   import io.netty.handler.codec.http.QueryStringDecoder;
9   
10  import java.util.List;
11  import java.util.Map;
12  
13  import com.foxinmy.weixin4j.type.EncryptType;
14  import com.foxinmy.weixin4j.util.AesToken;
15  
16  /**
17   * 微信请求
18   * 
19   * @className WeixinRequest
20   * @author jinyu(foxinmy@gmail.com)
21   * @date 2015年3月29日
22   * @since JDK 1.6
23   * @see
24   */
25  public class WeixinRequest implements HttpMessage {
26  
27  	/**
28  	 * 请求的表头
29  	 */
30  	private HttpHeaders headers;
31  	/**
32  	 * 请求的方式
33  	 */
34  	private HttpMethod method;
35  	/**
36  	 * 请求的URI
37  	 */
38  	private String uri;
39  
40  	// 以下字段每次被动消息时都会带上
41  	/**
42  	 * 随机字符串
43  	 */
44  	private String echoStr;
45  	/**
46  	 * 时间戳
47  	 */
48  	private String timeStamp;
49  	/**
50  	 * 随机数
51  	 */
52  	private String nonce;
53  	/**
54  	 * 参数签名
55  	 */
56  	private String signature;
57  	/**
58  	 * AES模式下消息签名
59  	 */
60  	private String msgSignature;
61  
62  	/**
63  	 * 加密类型(POST时存在)
64  	 * 
65  	 * @see com.foxinmy.weixin4j.type.EncryptType
66  	 */
67  	private EncryptType encryptType;
68  
69  	/**
70  	 * xml消息明文主体
71  	 */
72  	private String originalContent;
73  
74  	/**
75  	 * xml消息密文主体(AES时存在)
76  	 */
77  	private String encryptContent;
78  	/**
79  	 * aes & token
80  	 */
81  	private AesToken aesToken;
82  	/**
83  	 * url parameter
84  	 */
85  	private Map<String, List<String>> parameters;
86  	private DecoderResult decoderResult;
87  	private HttpVersion protocolVersion;
88  
89  	public WeixinRequest(HttpHeaders headers, HttpMethod method, String uri,
90  			EncryptType encryptType, String echoStr, String timeStamp,
91  			String nonce, String signature, String msgSignature,
92  			String originalContent, String encryptContent, AesToken aesToken) {
93  		this.headers = headers;
94  		this.method = method;
95  		this.uri = uri;
96  		this.encryptType = encryptType;
97  		this.echoStr = echoStr;
98  		this.timeStamp = timeStamp;
99  		this.nonce = nonce;
100 		this.signature = signature;
101 		this.msgSignature = msgSignature;
102 		this.originalContent = originalContent;
103 		this.encryptContent = encryptContent;
104 		this.aesToken = aesToken;
105 	}
106 
107 	public HttpMethod getMethod() {
108 		return method;
109 	}
110 
111 	public String getUri() {
112 		return uri;
113 	}
114 
115 	public String getEchoStr() {
116 		return echoStr;
117 	}
118 
119 	public String getTimeStamp() {
120 		return timeStamp;
121 	}
122 
123 	public String getNonce() {
124 		return nonce;
125 	}
126 
127 	public String getSignature() {
128 		return signature;
129 	}
130 
131 	public String getMsgSignature() {
132 		return msgSignature;
133 	}
134 
135 	public EncryptType getEncryptType() {
136 		return encryptType;
137 	}
138 
139 	public String getOriginalContent() {
140 		return originalContent;
141 	}
142 
143 	public String getEncryptContent() {
144 		return encryptContent;
145 	}
146 
147 	public AesToken getAesToken() {
148 		return aesToken;
149 	}
150 
151 	public Map<String, List<String>> getParameters() {
152 		if (parameters == null) {
153 			this.parameters = new QueryStringDecoder(uri, true).parameters();
154 		}
155 		return parameters;
156 	}
157 
158 	@Override
159 	public DecoderResult getDecoderResult() {
160 		return decoderResult;
161 	}
162 
163 	@Override
164 	public void setDecoderResult(DecoderResult decoderResult) {
165 		this.decoderResult = decoderResult;
166 	}
167 
168 	@Override
169 	public HttpVersion getProtocolVersion() {
170 		return protocolVersion;
171 	}
172 
173 	@Override
174 	public HttpMessage setProtocolVersion(HttpVersion protocolVersion) {
175 		this.protocolVersion = protocolVersion;
176 		return this;
177 	}
178 
179 	@Override
180 	public HttpHeaders headers() {
181 		return headers;
182 	}
183 
184 	@Override
185 	public DecoderResult decoderResult() {
186 		return decoderResult;
187 	}
188 
189 	@Override
190 	public HttpVersion protocolVersion() {
191 		return protocolVersion;
192 	}
193 
194 	@Override
195 	public String toString() {
196 		return "WeixinRequest [headers=" + headers.entries() + ", method="
197 				+ method + ", uri=" + uri + ", echoStr=" + echoStr
198 				+ ", timeStamp=" + timeStamp + ", nonce=" + nonce
199 				+ ", signature=" + signature + ", msgSignature=" + msgSignature
200 				+ ", encryptType=" + encryptType + ", originalContent="
201 				+ originalContent + ", encryptContent=" + encryptContent
202 				+ ", aesToken=" + aesToken + ", decoderResult=" + decoderResult
203 				+ ", protocolVersion=" + protocolVersion + "]";
204 	}
205 }