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  package org.apache.hadoop.hbase.rest.client;
20  
21  import java.io.InputStream;
22  
23  import org.apache.commons.httpclient.Header;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  
27  /**
28   * The HTTP result code, response headers, and body of an HTTP response.
29   */
30  @InterfaceAudience.Public
31  @InterfaceStability.Stable
32  public class Response {
33    private int code;
34    private Header[] headers;
35    private byte[] body;
36    private InputStream stream;
37  
38    /**
39     * Constructor
40     * @param code the HTTP response code
41     */
42    public Response(int code) {
43      this(code, null, null);
44    }
45  
46    /**
47     * Constructor
48     * @param code the HTTP response code
49     * @param headers the HTTP response headers
50     */
51    public Response(int code, Header[] headers) {
52      this(code, headers, null);
53    }
54  
55    /**
56     * Constructor
57     * @param code the HTTP response code
58     * @param headers the HTTP response headers
59     * @param body the response body, can be null
60     */
61    public Response(int code, Header[] headers, byte[] body) {
62      this.code = code;
63      this.headers = headers;
64      this.body = body;
65    }
66    
67    /**
68     * Constructor.
69     *
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 }