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    /**
77     * @return the value of the Location header
78     */
79    public String getLocation() {
80      for (Header header: headers) {
81        if (header.getName().equals("Location")) {
82          return header.getValue();
83        }
84      }
85      return null;
86    }
87  
88    /**
89     * @return true if a response body was sent
90     */
91    public boolean hasBody() {
92      return body != null;
93    }
94  
95    /**
96     * @return the HTTP response body
97     */
98    public byte[] getBody() {
99      return body;
100   }
101 
102   /**
103    * @param code the HTTP response code
104    */
105   public void setCode(int code) {
106     this.code = code;
107   }
108 
109   /**
110    * @param headers the HTTP response headers
111    */
112   public void setHeaders(Header[] headers) {
113     this.headers = headers;
114   }
115 
116   /**
117    * @param body the response body
118    */
119   public void setBody(byte[] body) {
120     this.body = body;
121   }
122 }