11/*
2- * Copyright 2002-2011 the original author or authors.
2+ * Copyright 2002-2012 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3232/**
3333 * Implementation of {@link HttpMessageConverter} that can read and write strings.
3434 *
35- * <p>By default, this converter supports all media types (<code>*/*</code>), and writes with a {@code
36- * Content-Type} of {@code text/plain}. This can be overridden by setting the {@link
37- * #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
35+ * <p>By default, this converter supports all media types (<code>*/*</code>),
36+ * and writes with a {@code Content-Type} of {@code text/plain}. This can be overridden
37+ * by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
3838 *
3939 * @author Arjen Poutsma
4040 * @since 3.0
@@ -43,12 +43,28 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
4343
4444 public static final Charset DEFAULT_CHARSET = Charset .forName ("ISO-8859-1" );
4545
46+ private final Charset defaultCharset ;
47+
4648 private final List <Charset > availableCharsets ;
4749
4850 private boolean writeAcceptCharset = true ;
4951
52+
53+ /**
54+ * A default constructor that uses {@code "ISO-8859-1"} as the default charset.
55+ * @see #StringHttpMessageConverter(Charset)
56+ */
5057 public StringHttpMessageConverter () {
51- super (new MediaType ("text" , "plain" , DEFAULT_CHARSET ), MediaType .ALL );
58+ this (DEFAULT_CHARSET );
59+ }
60+
61+ /**
62+ * A constructor accepting a default charset to use if the requested content
63+ * type does not specify one.
64+ */
65+ public StringHttpMessageConverter (Charset defaultCharset ) {
66+ super (new MediaType ("text" , "plain" , defaultCharset ), MediaType .ALL );
67+ this .defaultCharset = defaultCharset ;
5268 this .availableCharsets = new ArrayList <Charset >(Charset .availableCharsets ().values ());
5369 }
5470
@@ -60,6 +76,7 @@ public void setWriteAcceptCharset(boolean writeAcceptCharset) {
6076 this .writeAcceptCharset = writeAcceptCharset ;
6177 }
6278
79+
6380 @ Override
6481 public boolean supports (Class <?> clazz ) {
6582 return String .class .equals (clazz );
@@ -79,13 +96,13 @@ protected Long getContentLength(String s, MediaType contentType) {
7996 }
8097 catch (UnsupportedEncodingException ex ) {
8198 // should not occur
82- throw new InternalError (ex . getMessage () );
99+ throw new IllegalStateException (ex );
83100 }
84101 }
85102
86103 @ Override
87104 protected void writeInternal (String s , HttpOutputMessage outputMessage ) throws IOException {
88- if (writeAcceptCharset ) {
105+ if (this . writeAcceptCharset ) {
89106 outputMessage .getHeaders ().setAcceptCharset (getAcceptedCharsets ());
90107 }
91108 Charset charset = getContentTypeCharset (outputMessage .getHeaders ().getContentType ());
@@ -94,9 +111,7 @@ protected void writeInternal(String s, HttpOutputMessage outputMessage) throws I
94111
95112 /**
96113 * Return the list of supported {@link Charset}.
97- *
98114 * <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
99- *
100115 * @return the list of accepted charsets
101116 */
102117 protected List <Charset > getAcceptedCharsets () {
@@ -108,7 +123,7 @@ private Charset getContentTypeCharset(MediaType contentType) {
108123 return contentType .getCharSet ();
109124 }
110125 else {
111- return DEFAULT_CHARSET ;
126+ return this . defaultCharset ;
112127 }
113128 }
114129
0 commit comments