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     * Note: this is not thread-safe
79     */
80    public Response(int code, Header[] headers, HttpResponse resp, InputStream in) {
81      this.code = code;
82      this.headers = headers;
83      this.body = null;
84      this.resp = resp;
85      this.stream = in;
86    }
87
88    /**
89     * @return the HTTP response code
90     */
91    public int getCode() {
92      return code;
93    }
94
95    /**
96     * Gets the input stream instance.
97     *
98     * @return an instance of InputStream class.
99     */
100   public InputStream getStream(){
101     return this.stream;
102   }
103
104   /**
105    * @return the HTTP response headers
106    */
107   public Header[] getHeaders() {
108     return headers;
109   }
110
111   public String getHeader(String key) {
112     for (Header header: headers) {
113       if (header.getName().equalsIgnoreCase(key)) {
114         return header.getValue();
115       }
116     }
117     return null;
118   }
119
120   /**
121    * @return the value of the Location header
122    */
123   public String getLocation() {
124     return getHeader("Location");
125   }
126
127   /**
128    * @return true if a response body was sent
129    */
130   public boolean hasBody() {
131     return body != null;
132   }
133
134   /**
135    * @return the HTTP response body
136    */
137   public byte[] getBody() {
138     if (body == null) {
139       try {
140         body = Client.getResponseBody(resp);
141       } catch (IOException ioe) {
142         Log.debug("encountered ioe when obtaining body", ioe);
143       }
144     }
145     return body;
146   }
147
148   /**
149    * @param code the HTTP response code
150    */
151   public void setCode(int code) {
152     this.code = code;
153   }
154
155   /**
156    * @param headers the HTTP response headers
157    */
158   public void setHeaders(Header[] headers) {
159     this.headers = headers;
160   }
161
162   /**
163    * @param body the response body
164    */
165   public void setBody(byte[] body) {
166     this.body = body;
167   }
168 }