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.nio.charset.Charset;
020
021import org.springframework.http.HttpHeaders;
022import org.springframework.http.HttpStatus;
023
024/**
025 * Abstract base class for exceptions based on an {@link HttpStatus}.
026 *
027 * @author Arjen Poutsma
028 * @author Chris Beams
029 * @author Rossen Stoyanchev
030 * @since 3.0
031 */
032public abstract class HttpStatusCodeException extends RestClientResponseException {
033
034        private static final long serialVersionUID = 5696801857651587810L;
035
036
037        private final HttpStatus statusCode;
038
039
040        /**
041         * Construct a new instance with an {@link HttpStatus}.
042         * @param statusCode the status code
043         */
044        protected HttpStatusCodeException(HttpStatus statusCode) {
045                this(statusCode, statusCode.name(), null, null, null);
046        }
047
048        /**
049         * Construct a new instance with an {@link HttpStatus} and status text.
050         * @param statusCode the status code
051         * @param statusText the status text
052         */
053        protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
054                this(statusCode, statusText, null, null, null);
055        }
056
057        /**
058         * Construct instance with an {@link HttpStatus}, status text, and content.
059         * @param statusCode the status code
060         * @param statusText the status text
061         * @param responseBody the response body content, may be {@code null}
062         * @param responseCharset the response body charset, may be {@code null}
063         * @since 3.0.5
064         */
065        protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
066                        byte[] responseBody, Charset responseCharset) {
067
068                this(statusCode, statusText, null, responseBody, responseCharset);
069        }
070
071        /**
072         * Construct instance with an {@link HttpStatus}, status text, content, and
073         * a response charset.
074         * @param statusCode the status code
075         * @param statusText the status text
076         * @param responseHeaders the response headers, may be {@code null}
077         * @param responseBody the response body content, may be {@code null}
078         * @param responseCharset the response body charset, may be {@code null}
079         * @since 3.1.2
080         */
081        protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
082                        HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
083
084                super(statusCode.value() + " " + statusText, statusCode.value(), statusText,
085                                responseHeaders, responseBody, responseCharset);
086                this.statusCode = statusCode;
087        }
088
089
090        /**
091         * Return the HTTP status code.
092         */
093        public HttpStatus getStatusCode() {
094                return this.statusCode;
095        }
096
097}