001/*
002 * Copyright 2002-2019 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;
023import org.springframework.lang.Nullable;
024import org.springframework.util.StringUtils;
025
026/**
027 * Abstract base class for exceptions based on an {@link HttpStatus}.
028 *
029 * @author Arjen Poutsma
030 * @author Chris Beams
031 * @author Rossen Stoyanchev
032 * @since 3.0
033 */
034public abstract class HttpStatusCodeException extends RestClientResponseException {
035
036        private static final long serialVersionUID = 5696801857651587810L;
037
038
039        private final HttpStatus statusCode;
040
041
042        /**
043         * Construct a new instance with an {@link HttpStatus}.
044         * @param statusCode the status code
045         */
046        protected HttpStatusCodeException(HttpStatus statusCode) {
047                this(statusCode, statusCode.name(), null, null, null);
048        }
049
050        /**
051         * Construct a new instance with an {@link HttpStatus} and status text.
052         * @param statusCode the status code
053         * @param statusText the status text
054         */
055        protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
056                this(statusCode, statusText, null, null, null);
057        }
058
059        /**
060         * Construct instance with an {@link HttpStatus}, status text, and content.
061         * @param statusCode the status code
062         * @param statusText the status text
063         * @param responseBody the response body content, may be {@code null}
064         * @param responseCharset the response body charset, may be {@code null}
065         * @since 3.0.5
066         */
067        protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
068                        @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
069
070                this(statusCode, statusText, null, responseBody, responseCharset);
071        }
072
073        /**
074         * Construct instance with an {@link HttpStatus}, status text, content, and
075         * a response charset.
076         * @param statusCode the status code
077         * @param statusText the status text
078         * @param responseHeaders the response headers, may be {@code null}
079         * @param responseBody the response body content, may be {@code null}
080         * @param responseCharset the response body charset, may be {@code null}
081         * @since 3.1.2
082         */
083        protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
084                        @Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
085
086                this(getMessage(statusCode, statusText),
087                                statusCode, statusText, responseHeaders, responseBody, responseCharset);
088        }
089
090        /**
091         * Construct instance with an {@link HttpStatus}, status text, content, and
092         * a response charset.
093         * @param message the exception message
094         * @param statusCode the status code
095         * @param statusText the status text
096         * @param responseHeaders the response headers, may be {@code null}
097         * @param responseBody the response body content, may be {@code null}
098         * @param responseCharset the response body charset, may be {@code null}
099         * @since 5.2.2
100         */
101        protected HttpStatusCodeException(String message, HttpStatus statusCode, String statusText,
102                        @Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
103
104                super(message, statusCode.value(), statusText, responseHeaders, responseBody, responseCharset);
105                this.statusCode = statusCode;
106        }
107
108        private static String getMessage(HttpStatus statusCode, String statusText) {
109                if (!StringUtils.hasLength(statusText)) {
110                        statusText = statusCode.getReasonPhrase();
111                }
112                return statusCode.value() + " " + statusText;
113        }
114
115        /**
116         * Return the HTTP status code.
117         */
118        public HttpStatus getStatusCode() {
119                return this.statusCode;
120        }
121
122}