View Javadoc

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