View Javadoc

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 org.apache.commons.httpclient.Header;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  
26  /**
27   * The HTTP result code, response headers, and body of a HTTP response.
28   */
29  @InterfaceAudience.Public
30  @InterfaceStability.Stable
31  public class Response {
32    private int code;
33    private Header[] headers;
34    private byte[] body;
35  
36    /**
37     * Constructor
38     * @param code the HTTP response code
39     */
40    public Response(int code) {
41      this(code, null, null);
42    }
43  
44    /**
45     * Constructor
46     * @param code the HTTP response code
47     * @param headers the HTTP response headers
48     */
49    public Response(int code, Header[] headers) {
50      this(code, headers, null);
51    }
52  
53    /**
54     * Constructor
55     * @param code the HTTP response code
56     * @param headers the HTTP response headers
57     * @param body the response body, can be null
58     */
59    public Response(int code, Header[] headers, byte[] body) {
60      this.code = code;
61      this.headers = headers;
62      this.body = body;
63    }
64  
65    /**
66     * @return the HTTP response code
67     */
68    public int getCode() {
69      return code;
70    }
71  
72    /**
73     * @return the HTTP response headers
74     */
75    public Header[] getHeaders() {
76      return headers;
77    }
78  
79    public String getHeader(String key) {
80      for (Header header: headers) {
81        if (header.getName().equalsIgnoreCase(key)) {
82          return header.getValue();
83        }
84      }
85      return null;
86    }
87  
88    /**
89     * @return the value of the Location header
90     */
91    public String getLocation() {
92      return getHeader("Location");
93    }
94  
95    /**
96     * @return true if a response body was sent
97     */
98    public boolean hasBody() {
99      return body != null;
100   }
101 
102   /**
103    * @return the HTTP response body
104    */
105   public byte[] getBody() {
106     return body;
107   }
108 
109   /**
110    * @param code the HTTP response code
111    */
112   public void setCode(int code) {
113     this.code = code;
114   }
115 
116   /**
117    * @param headers the HTTP response headers
118    */
119   public void setHeaders(Header[] headers) {
120     this.headers = headers;
121   }
122 
123   /**
124    * @param body the response body
125    */
126   public void setBody(byte[] body) {
127     this.body = body;
128   }
129 }