001/*
002 * Copyright 2002-2020 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.lang.reflect.Type;
020import java.nio.charset.StandardCharsets;
021
022import org.springframework.http.HttpHeaders;
023import org.springframework.http.MediaType;
024import org.springframework.lang.Nullable;
025
026/**
027 * Raised when no suitable
028 * {@link org.springframework.http.converter.HttpMessageConverter} could be
029 * found to extract the response.
030 *
031 * @author Rossen Stoyanchev
032 * @since 5.2.7
033 */
034public class UnknownContentTypeException extends RestClientException {
035
036        private static final long serialVersionUID = 2759516676367274084L;
037
038
039        private final Type targetType;
040
041        private final MediaType contentType;
042
043        private final int rawStatusCode;
044
045        private final String statusText;
046
047        private final byte[] responseBody;
048
049        private final HttpHeaders responseHeaders;
050
051
052        /**
053         * Construct a new instance of with the given response data.
054         * @param targetType the expected target type
055         * @param contentType the content type of the response
056         * @param statusCode the raw status code value
057         * @param statusText the status text
058         * @param responseHeaders the response headers (may be {@code null})
059         * @param responseBody the response body content (may be {@code null})
060         */
061        public UnknownContentTypeException(Type targetType, MediaType contentType,
062                        int statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody) {
063
064                super("Could not extract response: no suitable HttpMessageConverter found " +
065                                "for response type [" + targetType + "] and content type [" + contentType + "]");
066
067                this.targetType = targetType;
068                this.contentType = contentType;
069                this.rawStatusCode = statusCode;
070                this.statusText = statusText;
071                this.responseHeaders = responseHeaders;
072                this.responseBody = responseBody;
073        }
074
075
076        /**
077         * Return the target type expected for the response.
078         */
079        public Type getTargetType() {
080                return this.targetType;
081        }
082
083        /**
084         * Return the content type of the response, or "application/octet-stream".
085         */
086        public MediaType getContentType() {
087                return this.contentType;
088        }
089
090        /**
091         * Return the raw HTTP status code value.
092         */
093        public int getRawStatusCode() {
094                return this.rawStatusCode;
095        }
096
097        /**
098         * Return the HTTP status text.
099         */
100        public String getStatusText() {
101                return this.statusText;
102        }
103
104        /**
105         * Return the HTTP response headers.
106         */
107        @Nullable
108        public HttpHeaders getResponseHeaders() {
109                return this.responseHeaders;
110        }
111
112        /**
113         * Return the response body as a byte array.
114         */
115        public byte[] getResponseBody() {
116                return this.responseBody;
117        }
118
119        /**
120         * Return the response body converted to String using the charset from the
121         * response "Content-Type" or {@code "UTF-8"} otherwise.
122         */
123        public String getResponseBodyAsString() {
124                return new String(this.responseBody, this.contentType.getCharset() != null ?
125                                this.contentType.getCharset() : StandardCharsets.UTF_8);
126        }
127
128}