001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.rest.client;
021
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.apache.http.Header;
029import org.apache.http.HttpResponse;
030
031/**
032 * The HTTP result code, response headers, and body of a HTTP response.
033 */
034@InterfaceAudience.Public
035public class Response {
036  private static final Logger LOG = LoggerFactory.getLogger(Response.class);
037
038  private int code;
039  private Header[] headers;
040  private byte[] body;
041  private HttpResponse resp;
042  private InputStream stream;
043
044  /**
045   * Constructor
046   * @param code the HTTP response code
047   */
048  public Response(int code) {
049    this(code, null, null);
050  }
051
052  /**
053   * Constructor
054   * @param code the HTTP response code
055   * @param headers the HTTP response headers
056   */
057  public Response(int code, Header[] headers) {
058    this(code, headers, null);
059  }
060
061  /**
062   * Constructor
063   * @param code the HTTP response code
064   * @param headers the HTTP response headers
065   * @param body the response body, can be null
066   */
067  public Response(int code, Header[] headers, byte[] body) {
068    this.code = code;
069    this.headers = headers;
070    this.body = body;
071  }
072  
073  /**
074   * Constructor
075   * @param code the HTTP response code
076   * @param headers headers the HTTP response headers
077   * @param resp the response
078   * @param in Inputstream if the response had one.
079   * Note: this is not thread-safe
080   */
081  public Response(int code, Header[] headers, HttpResponse resp, InputStream in) {
082    this.code = code;
083    this.headers = headers;
084    this.body = null;
085    this.resp = resp;
086    this.stream = in;
087  }
088
089  /**
090   * @return the HTTP response code
091   */
092  public int getCode() {
093    return code;
094  }
095  
096  /**
097   * Gets the input stream instance.
098   *
099   * @return an instance of InputStream class.
100   */
101  public InputStream getStream(){
102    return this.stream;
103  }
104
105  /**
106   * @return the HTTP response headers
107   */
108  public Header[] getHeaders() {
109    return headers;
110  }
111
112  public String getHeader(String key) {
113    for (Header header: headers) {
114      if (header.getName().equalsIgnoreCase(key)) {
115        return header.getValue();
116      }
117    }
118    return null;
119  }
120
121  /**
122   * @return the value of the Location header
123   */
124  public String getLocation() {
125    return getHeader("Location");
126  }
127
128  /**
129   * @return true if a response body was sent
130   */
131  public boolean hasBody() {
132    return body != null;
133  }
134
135  /**
136   * @return the HTTP response body
137   */
138  public byte[] getBody() {
139    if (body == null) {
140      try {
141        body = Client.getResponseBody(resp);
142      } catch (IOException ioe) {
143        LOG.debug("encountered ioe when obtaining body", ioe);
144      }
145    }
146    return body;
147  }
148
149  /**
150   * @param code the HTTP response code
151   */
152  public void setCode(int code) {
153    this.code = code;
154  }
155
156  /**
157   * @param headers the HTTP response headers
158   */
159  public void setHeaders(Header[] headers) {
160    this.headers = headers;
161  }
162
163  /**
164   * @param body the response body
165   */
166  public void setBody(byte[] body) {
167    this.body = body;
168  }
169}