1 package com.foxinmy.weixin4j.http.support.okhttp;
2
3
4 import com.foxinmy.weixin4j.http.AbstractHttpResponse;
5 import com.foxinmy.weixin4j.http.HttpHeaders;
6 import com.foxinmy.weixin4j.http.HttpStatus;
7 import com.foxinmy.weixin4j.http.HttpVersion;
8 import com.squareup.okhttp.Headers;
9 import com.squareup.okhttp.Protocol;
10 import com.squareup.okhttp.Response;
11
12
13
14
15
16
17
18
19
20 public class OkHttpResponse2 extends AbstractHttpResponse {
21 private final Response response;
22 private HttpHeaders headers;
23 private HttpVersion protocol;
24 private HttpStatus status;
25
26 public OkHttpResponse2(Response response, byte[] content) {
27 super(content);
28 this.response = response;
29 }
30
31 @Override
32 public HttpHeaders getHeaders() {
33 if (this.headers == null) {
34 this.headers = new HttpHeaders();
35 Headers headers = this.response.headers();
36 for (int i = 0; i < headers.size(); i++) {
37 this.headers.add(headers.name(i), headers.value(i));
38 }
39 }
40 return this.headers;
41 }
42
43 @Override
44 public HttpVersion getProtocol() {
45 if (protocol == null) {
46 String protocol = this.response.protocol().toString().split("/")[0];
47 boolean keepAlive = KEEP_ALIVE.equalsIgnoreCase(this.response
48 .header("Connection"));
49 if (this.response.protocol() == Protocol.HTTP_1_0) {
50 return new HttpVersion(protocol, 1, 0, keepAlive);
51 } else if (this.response.protocol() == Protocol.HTTP_1_1) {
52 return new HttpVersion(protocol, 1, 1, keepAlive);
53 } else if (this.response.protocol() == Protocol.HTTP_2) {
54 return new HttpVersion(protocol, 2, 0, keepAlive);
55 } else if (this.response.protocol() == Protocol.SPDY_3) {
56 return new HttpVersion(protocol, 3, 0, keepAlive);
57 } else {
58 this.protocol = new HttpVersion(protocol, keepAlive);
59 }
60 }
61 return protocol;
62 }
63
64 @Override
65 public HttpStatus getStatus() {
66 if (status == null) {
67 status = new HttpStatus(this.response.code(),
68 this.response.message());
69 }
70 return status;
71 }
72
73 @Override
74 public void close() {
75
76 }
77 }