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 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   * The HTTP result code, response headers, and body of a HTTP response.
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     * Constructor
45     * @param code the HTTP response code
46     */
47    public Response(int code) {
48      this(code, null, null);
49    }
50  
51    /**
52     * Constructor
53     * @param code the HTTP response code
54     * @param headers the HTTP response headers
55     */
56    public Response(int code, Header[] headers) {
57      this(code, headers, null);
58    }
59  
60    /**
61     * Constructor
62     * @param code the HTTP response code
63     * @param headers the HTTP response headers
64     * @param body the response body, can be null
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     * Constructor
74     * @param code the HTTP response code
75     * @param headers headers the HTTP response headers
76     * @param resp the response
77     * @param in Inputstream if the response had one.
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     * @return the HTTP response code
89     */
90    public int getCode() {
91      return code;
92    }
93    
94    /**
95     * Gets the input stream instance.
96     *
97     * @return an instance of InputStream class.
98     */
99    public InputStream getStream(){
100     return this.stream;
101   }
102 
103   /**
104    * @return the HTTP response headers
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    * @return the value of the Location header
121    */
122   public String getLocation() {
123     return getHeader("Location");
124   }
125 
126   /**
127    * @return true if a response body was sent
128    */
129   public boolean hasBody() {
130     return body != null;
131   }
132 
133   /**
134    * @return the HTTP response body
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    * @param code the HTTP response code
149    */
150   public void setCode(int code) {
151     this.code = code;
152   }
153 
154   /**
155    * @param headers the HTTP response headers
156    */
157   public void setHeaders(Header[] headers) {
158     this.headers = headers;
159   }
160 
161   /**
162    * @param body the response body
163    */
164   public void setBody(byte[] body) {
165     this.body = body;
166   }
167 }