View Javadoc
1   package com.foxinmy.weixin4j.http.support.apache3;
2   
3   import java.io.IOException;
4   
5   import org.apache.commons.httpclient.Header;
6   import org.apache.commons.httpclient.HttpMethod;
7   
8   import com.foxinmy.weixin4j.http.AbstractHttpResponse;
9   import com.foxinmy.weixin4j.http.HttpHeaders;
10  import com.foxinmy.weixin4j.http.HttpStatus;
11  import com.foxinmy.weixin4j.http.HttpVersion;
12  
13  /**
14   * HttpComponent3 Response:Requires commons-httpclient 3.0 or higher
15   *
16   * @className HttpComponent3Response
17   * @author jinyu(foxinmy@gmail.com)
18   * @date 2015年8月17日
19   * @since JDK 1.6
20   * @see
21   */
22  public class HttpComponent3Response extends AbstractHttpResponse {
23  
24  	private final HttpMethod httpMethod;
25  
26  	private HttpHeaders headers;
27  	private HttpVersion protocol;
28  	private HttpStatus status;
29  
30  	public HttpComponent3Response(HttpMethod httpMethod) throws IOException {
31  		super(httpMethod.getResponseBody());
32  		this.httpMethod = httpMethod;
33  	}
34  
35  	@Override
36  	public HttpHeaders getHeaders() {
37  		if (headers == null) {
38  			headers = new HttpHeaders();
39  			Header[] headers = httpMethod.getResponseHeaders();
40  			for (Header header : headers) {
41  				this.headers.add(header.getName(), header.getValue());
42  			}
43  		}
44  		return headers;
45  	}
46  
47  	@Override
48  	public HttpVersion getProtocol() {
49  		org.apache.commons.httpclient.HttpVersion version = httpMethod
50  				.getParams().getVersion();
51  		if (version == null) {
52  			return null;
53  		}
54  		Header connection = httpMethod.getResponseHeader("Connection");
55  		if (protocol == null) {
56  			protocol = new HttpVersion("HTTP", version.getMajor(),
57  					version.getMinor(), connection != null
58  							&& KEEP_ALIVE.equalsIgnoreCase(connection
59  									.getValue()));
60  		}
61  		return protocol;
62  	}
63  
64  	@Override
65  	public HttpStatus getStatus() {
66  		if (status == null) {
67  			status = new HttpStatus(httpMethod.getStatusCode(),
68  					httpMethod.getStatusText());
69  		}
70  		return status;
71  	}
72  
73  	@Override
74  	public void close() {
75  		httpMethod.releaseConnection();
76  		//Protocol.unregisterProtocol("https");
77  	}
78  }