View Javadoc
1   package com.foxinmy.weixin4j.http.support.apache4;
2   
3   import java.io.IOException;
4   
5   import org.apache.http.Header;
6   import org.apache.http.ProtocolVersion;
7   import org.apache.http.StatusLine;
8   
9   import com.foxinmy.weixin4j.http.AbstractHttpResponse;
10  import com.foxinmy.weixin4j.http.HttpHeaders;
11  import com.foxinmy.weixin4j.http.HttpStatus;
12  import com.foxinmy.weixin4j.http.HttpVersion;
13  
14  /**
15   * HttpComponent4 Response:Requires Apache HttpComponents 4.2 or lower
16   * 
17   * @className HttpComponent4_1Response
18   * @author jinyu(foxinmy@gmail.com)
19   * @date 2015年8月18日
20   * @since JDK 1.6
21   * @see
22   */
23  public class HttpComponent4_1Response extends AbstractHttpResponse {
24  	private final org.apache.http.HttpResponse httpResponse;
25  
26  	private HttpHeaders headers;
27  	private HttpVersion protocol;
28  	private HttpStatus status;
29  
30  	public HttpComponent4_1Response(org.apache.http.HttpResponse httpResponse,
31  			byte[] content) throws IOException {
32  		super(content);
33  		this.httpResponse = httpResponse;
34  	}
35  
36  	@Override
37  	public HttpHeaders getHeaders() {
38  		if (headers == null) {
39  			headers = new HttpHeaders();
40  			Header[] headers = httpResponse.getAllHeaders();
41  			for (Header header : headers) {
42  				this.headers.add(header.getName(), header.getValue());
43  			}
44  		}
45  		return headers;
46  	}
47  
48  	@Override
49  	public HttpVersion getProtocol() {
50  		if (protocol == null) {
51  			ProtocolVersion version = httpResponse.getProtocolVersion();
52  			Header connection = httpResponse.getFirstHeader("Connection");
53  			protocol = new HttpVersion(version.getProtocol(),
54  					version.getMajor(), version.getMinor(), connection != null
55  							&& KEEP_ALIVE.equalsIgnoreCase(connection
56  									.getValue()));
57  		}
58  		return protocol;
59  	}
60  
61  	@Override
62  	public HttpStatus getStatus() {
63  		if (status == null) {
64  			StatusLine statusLine = httpResponse.getStatusLine();
65  			status = new HttpStatus(statusLine.getStatusCode(),
66  					statusLine.getReasonPhrase());
67  		}
68  		return status;
69  	}
70  
71  	@Override
72  	public void close() {
73  		try {
74  			httpResponse.getEntity().consumeContent();
75  		} catch (IOException e) {
76  			;
77  		}
78  	}
79  }