001/*
002 * Copyright 2002-2017 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.http.converter;
018
019import java.io.IOException;
020import java.io.UnsupportedEncodingException;
021import java.nio.charset.Charset;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.springframework.http.HttpInputMessage;
026import org.springframework.http.HttpOutputMessage;
027import org.springframework.http.MediaType;
028import org.springframework.util.StreamUtils;
029
030/**
031 * Implementation of {@link HttpMessageConverter} that can read and write strings.
032 *
033 * <p>By default, this converter supports all media types ({@code &#42;&#47;&#42;}),
034 * and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
035 * by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
036 *
037 * @author Arjen Poutsma
038 * @author Juergen Hoeller
039 * @since 3.0
040 */
041public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
042
043        public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
044
045
046        private volatile List<Charset> availableCharsets;
047
048        private boolean writeAcceptCharset = true;
049
050
051        /**
052         * A default constructor that uses {@code "ISO-8859-1"} as the default charset.
053         * @see #StringHttpMessageConverter(Charset)
054         */
055        public StringHttpMessageConverter() {
056                this(DEFAULT_CHARSET);
057        }
058
059        /**
060         * A constructor accepting a default charset to use if the requested content
061         * type does not specify one.
062         */
063        public StringHttpMessageConverter(Charset defaultCharset) {
064                super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL);
065        }
066
067
068        /**
069         * Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
070         * <p>Default is {@code true}.
071         */
072        public void setWriteAcceptCharset(boolean writeAcceptCharset) {
073                this.writeAcceptCharset = writeAcceptCharset;
074        }
075
076
077        @Override
078        public boolean supports(Class<?> clazz) {
079                return String.class == clazz;
080        }
081
082        @Override
083        protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
084                Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
085                return StreamUtils.copyToString(inputMessage.getBody(), charset);
086        }
087
088        @Override
089        protected Long getContentLength(String str, MediaType contentType) {
090                Charset charset = getContentTypeCharset(contentType);
091                try {
092                        return (long) str.getBytes(charset.name()).length;
093                }
094                catch (UnsupportedEncodingException ex) {
095                        // should not occur
096                        throw new IllegalStateException(ex);
097                }
098        }
099
100        @Override
101        protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
102                if (this.writeAcceptCharset) {
103                        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
104                }
105                Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
106                StreamUtils.copy(str, charset, outputMessage.getBody());
107        }
108
109
110        /**
111         * Return the list of supported {@link Charset}s.
112         * <p>By default, returns {@link Charset#availableCharsets()}.
113         * Can be overridden in subclasses.
114         * @return the list of accepted charsets
115         */
116        protected List<Charset> getAcceptedCharsets() {
117                if (this.availableCharsets == null) {
118                        this.availableCharsets = new ArrayList<Charset>(
119                                        Charset.availableCharsets().values());
120                }
121                return this.availableCharsets;
122        }
123
124        private Charset getContentTypeCharset(MediaType contentType) {
125                if (contentType != null && contentType.getCharset() != null) {
126                        return contentType.getCharset();
127                }
128                else {
129                        return getDefaultCharset();
130                }
131        }
132
133}