(Charset.availableCharsets().values());
33 | }
34 |
35 | /**
36 | * Indicates whether the {@code Accept-Charset} should be written to any
37 | * outgoing request.
38 | *
39 | * Default is {@code true}.
40 | */
41 | public void setWriteAcceptCharset(boolean writeAcceptCharset) {
42 | this.writeAcceptCharset = writeAcceptCharset;
43 | }
44 |
45 | @Override
46 | public boolean supports(Class> clazz) {
47 | return String.class.equals(clazz);
48 | }
49 |
50 | @Override
51 | protected String readInternal(Class extends String> clazz, HttpInputMessage inputMessage) throws IOException {
52 | Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
53 | return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
54 | }
55 |
56 | @Override
57 | protected Long getContentLength(String s, MediaType contentType) {
58 | Charset charset = getContentTypeCharset(contentType);
59 | try {
60 | return (long) s.getBytes(charset.name()).length;
61 | } catch (UnsupportedEncodingException ex) {
62 | throw new InternalError(ex.getMessage());
63 | }
64 | }
65 |
66 | @Override
67 | protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
68 | if (writeAcceptCharset) {
69 | outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
70 | }
71 | Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
72 | FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));
73 | }
74 |
75 | /**
76 | * Return the list of supported {@link Charset}.
77 | *
78 | *
79 | * By default, returns {@link Charset#availableCharsets()}. Can be
80 | * overridden in subclasses.
81 | *
82 | * @return the list of accepted charsets
83 | */
84 | protected List getAcceptedCharsets() {
85 | return this.availableCharsets;
86 | }
87 |
88 | private Charset getContentTypeCharset(MediaType contentType) {
89 | if (contentType != null && contentType.getCharset() != null) {
90 | return contentType.getCharset();
91 | } else {
92 | return DEFAULT_CHARSET;
93 | }
94 | }
95 | }
96 |
--------------------------------------------------------------------------------