001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.client; 018 019import java.io.UnsupportedEncodingException; 020import java.nio.charset.Charset; 021 022import org.springframework.http.HttpHeaders; 023 024/** 025 * Common base class for exceptions that contain actual HTTP response data. 026 * 027 * @author Rossen Stoyanchev 028 * @since 4.3 029 */ 030public class RestClientResponseException extends RestClientException { 031 032 private static final long serialVersionUID = -8803556342728481792L; 033 034 private static final String DEFAULT_CHARSET = "ISO-8859-1"; 035 036 037 private final int rawStatusCode; 038 039 private final String statusText; 040 041 private final byte[] responseBody; 042 043 private final HttpHeaders responseHeaders; 044 045 private final String responseCharset; 046 047 048 /** 049 * Construct a new instance of with the given response data. 050 * @param statusCode the raw status code value 051 * @param statusText the status text 052 * @param responseHeaders the response headers (may be {@code null}) 053 * @param responseBody the response body content (may be {@code null}) 054 * @param responseCharset the response body charset (may be {@code null}) 055 */ 056 public RestClientResponseException(String message, int statusCode, String statusText, 057 HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { 058 059 super(message); 060 this.rawStatusCode = statusCode; 061 this.statusText = statusText; 062 this.responseHeaders = responseHeaders; 063 this.responseBody = (responseBody != null ? responseBody : new byte[0]); 064 this.responseCharset = (responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET); 065 } 066 067 068 /** 069 * Return the raw HTTP status code value. 070 */ 071 public int getRawStatusCode() { 072 return this.rawStatusCode; 073 } 074 075 /** 076 * Return the HTTP status text. 077 */ 078 public String getStatusText() { 079 return this.statusText; 080 } 081 082 /** 083 * Return the HTTP response headers. 084 */ 085 public HttpHeaders getResponseHeaders() { 086 return this.responseHeaders; 087 } 088 089 /** 090 * Return the response body as a byte array. 091 */ 092 public byte[] getResponseBodyAsByteArray() { 093 return this.responseBody; 094 } 095 096 /** 097 * Return the response body as a string. 098 */ 099 public String getResponseBodyAsString() { 100 try { 101 return new String(this.responseBody, this.responseCharset); 102 } 103 catch (UnsupportedEncodingException ex) { 104 // should not occur 105 throw new IllegalStateException(ex); 106 } 107 } 108 109}