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 */ 019package org.apache.hadoop.hbase.rest.client; 020 021import java.io.IOException; 022import java.io.InputStream; 023 024import org.apache.http.Header; 025import org.apache.http.HttpResponse; 026 027import org.apache.yetus.audience.InterfaceAudience; 028 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032/** 033 * The HTTP result code, response headers, and body of an HTTP response. 034 */ 035@InterfaceAudience.Public 036public class Response { 037 private static final Logger LOG = LoggerFactory.getLogger(Response.class); 038 039 private int code; 040 private Header[] headers; 041 private byte[] body; 042 private HttpResponse resp; 043 private InputStream stream; 044 045 /** 046 * Constructor 047 * @param code the HTTP response code 048 */ 049 public Response(int code) { 050 this(code, null, null); 051 } 052 053 /** 054 * Constructor 055 * @param code the HTTP response code 056 * @param headers the HTTP response headers 057 */ 058 public Response(int code, Header[] headers) { 059 this(code, headers, null); 060 } 061 062 /** 063 * Constructor 064 * @param code the HTTP response code 065 * @param headers the HTTP response headers 066 * @param body the response body, can be null 067 */ 068 public Response(int code, Header[] headers, byte[] body) { 069 this.code = code; 070 this.headers = headers; 071 this.body = body; 072 } 073 074 /** 075 * Constructor. Note: this is not thread-safe 076 * 077 * @param code the HTTP response code 078 * @param headers headers the HTTP response headers 079 * @param resp the response 080 * @param in Inputstream if the response had one. 081 */ 082 public Response(int code, Header[] headers, HttpResponse resp, InputStream in) { 083 this.code = code; 084 this.headers = headers; 085 this.body = null; 086 this.resp = resp; 087 this.stream = in; 088 } 089 090 /** 091 * @return the HTTP response code 092 */ 093 public int getCode() { 094 return code; 095 } 096 097 /** 098 * Gets the input stream instance. 099 * 100 * @return an instance of InputStream class. 101 */ 102 public InputStream getStream(){ 103 return this.stream; 104 } 105 106 /** 107 * @return the HTTP response headers 108 */ 109 public Header[] getHeaders() { 110 return headers; 111 } 112 113 public String getHeader(String key) { 114 for (Header header : headers) { 115 if (header.getName().equalsIgnoreCase(key)) { 116 return header.getValue(); 117 } 118 } 119 return null; 120 } 121 122 /** 123 * @return the value of the Location header 124 */ 125 public String getLocation() { 126 return getHeader("Location"); 127 } 128 129 /** 130 * @return true if a response body was sent 131 */ 132 public boolean hasBody() { 133 return body != null; 134 } 135 136 /** 137 * @return the HTTP response body 138 */ 139 public byte[] getBody() { 140 if (body == null) { 141 try { 142 body = Client.getResponseBody(resp); 143 } catch (IOException ioe) { 144 LOG.debug("encountered ioe when obtaining body", ioe); 145 } 146 } 147 return body; 148 } 149 150 /** 151 * @param code the HTTP response code 152 */ 153 public void setCode(int code) { 154 this.code = code; 155 } 156 157 /** 158 * @param headers the HTTP response headers 159 */ 160 public void setHeaders(Header[] headers) { 161 this.headers = headers; 162 } 163 164 /** 165 * @param body the response body 166 */ 167 public void setBody(byte[] body) { 168 this.body = body; 169 } 170}