1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest.client;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.apache.http.Header;
26 import org.apache.http.HttpResponse;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.mortbay.log.Log;
30
31
32
33
34 @InterfaceAudience.Public
35 @InterfaceStability.Stable
36 public class Response {
37 private int code;
38 private Header[] headers;
39 private byte[] body;
40 private HttpResponse resp;
41 private InputStream stream;
42
43
44
45
46
47 public Response(int code) {
48 this(code, null, null);
49 }
50
51
52
53
54
55
56 public Response(int code, Header[] headers) {
57 this(code, headers, null);
58 }
59
60
61
62
63
64
65
66 public Response(int code, Header[] headers, byte[] body) {
67 this.code = code;
68 this.headers = headers;
69 this.body = body;
70 }
71
72
73
74
75
76
77
78
79 public Response(int code, Header[] headers, HttpResponse resp, InputStream in) {
80 this.code = code;
81 this.headers = headers;
82 this.body = null;
83 this.resp = resp;
84 this.stream = in;
85 }
86
87
88
89
90 public int getCode() {
91 return code;
92 }
93
94
95
96
97
98
99 public InputStream getStream(){
100 return this.stream;
101 }
102
103
104
105
106 public Header[] getHeaders() {
107 return headers;
108 }
109
110 public String getHeader(String key) {
111 for (Header header: headers) {
112 if (header.getName().equalsIgnoreCase(key)) {
113 return header.getValue();
114 }
115 }
116 return null;
117 }
118
119
120
121
122 public String getLocation() {
123 return getHeader("Location");
124 }
125
126
127
128
129 public boolean hasBody() {
130 return body != null;
131 }
132
133
134
135
136 public byte[] getBody() {
137 if (body == null) {
138 try {
139 body = Client.getResponseBody(resp);
140 } catch (IOException ioe) {
141 Log.debug("encountered ioe when obtaining body", ioe);
142 }
143 }
144 return body;
145 }
146
147
148
149
150 public void setCode(int code) {
151 this.code = code;
152 }
153
154
155
156
157 public void setHeaders(Header[] headers) {
158 this.headers = headers;
159 }
160
161
162
163
164 public void setBody(byte[] body) {
165 this.body = body;
166 }
167 }