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