1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 package org.apache.hadoop.hbase.rest.client;
21
22 import java.io.InputStream;
23
24 import org.apache.commons.httpclient.Header;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.hbase.classification.InterfaceStability;
27
28 /**
29 * The HTTP result code, response headers, and body of a HTTP response.
30 */
31 @InterfaceAudience.Public
32 @InterfaceStability.Stable
33 public class Response {
34 private int code;
35 private Header[] headers;
36 private byte[] body;
37 private InputStream stream;
38
39 /**
40 * Constructor
41 * @param code the HTTP response code
42 */
43 public Response(int code) {
44 this(code, null, null);
45 }
46
47 /**
48 * Constructor
49 * @param code the HTTP response code
50 * @param headers the HTTP response headers
51 */
52 public Response(int code, Header[] headers) {
53 this(code, headers, null);
54 }
55
56 /**
57 * Constructor
58 * @param code the HTTP response code
59 * @param headers the HTTP response headers
60 * @param body the response body, can be null
61 */
62 public Response(int code, Header[] headers, byte[] body) {
63 this.code = code;
64 this.headers = headers;
65 this.body = body;
66 }
67
68 /**
69 * Constructor
70 * @param code the HTTP response code
71 * @param headers headers the HTTP response headers
72 * @param body the response body, can be null
73 * @param in Inputstream if the response had one.
74 */
75 public Response(int code, Header[] headers, byte[] body, InputStream in) {
76 this.code = code;
77 this.headers = headers;
78 this.body = body;
79 this.stream = in;
80 }
81
82 /**
83 * @return the HTTP response code
84 */
85 public int getCode() {
86 return code;
87 }
88
89 /**
90 * Gets the input stream instance.
91 *
92 * @return an instance of InputStream class.
93 */
94 public InputStream getStream(){
95 return this.stream;
96 }
97
98 /**
99 * @return the HTTP response headers
100 */
101 public Header[] getHeaders() {
102 return headers;
103 }
104
105 public String getHeader(String key) {
106 for (Header header: headers) {
107 if (header.getName().equalsIgnoreCase(key)) {
108 return header.getValue();
109 }
110 }
111 return null;
112 }
113
114 /**
115 * @return the value of the Location header
116 */
117 public String getLocation() {
118 return getHeader("Location");
119 }
120
121 /**
122 * @return true if a response body was sent
123 */
124 public boolean hasBody() {
125 return body != null;
126 }
127
128 /**
129 * @return the HTTP response body
130 */
131 public byte[] getBody() {
132 return body;
133 }
134
135 /**
136 * @param code the HTTP response code
137 */
138 public void setCode(int code) {
139 this.code = code;
140 }
141
142 /**
143 * @param headers the HTTP response headers
144 */
145 public void setHeaders(Header[] headers) {
146 this.headers = headers;
147 }
148
149 /**
150 * @param body the response body
151 */
152 public void setBody(byte[] body) {
153 this.body = body;
154 }
155 }