1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.catalina.util;
19
20
21 import java.io.InputStream;
22 import java.util.Locale;
23 import java.util.Properties;
24
25 import org.apache.tomcat.util.ExceptionUtils;
26 import org.apache.tomcat.util.compat.JreCompat;
27
28
29
30 /**
31 * Utility class that attempts to map from a Locale to the corresponding
32 * character set to be used for interpreting input text (or generating
33 * output text) when the Content-Type header does not include one. You
34 * can customize the behavior of this class by modifying the mapping data
35 * it loads, or by subclassing it (to change the algorithm) and then using
36 * your own version for a particular web application.
37 *
38 * @author Craig R. McClanahan
39 */
40 public class CharsetMapper {
41
42
43 // ---------------------------------------------------- Manifest Constants
44
45
46 /**
47 * Default properties resource name.
48 */
49 public static final String DEFAULT_RESOURCE =
50 "/org/apache/catalina/util/CharsetMapperDefault.properties";
51
52
53 // ---------------------------------------------------------- Constructors
54
55
56 /**
57 * Construct a new CharsetMapper using the default properties resource.
58 */
59 public CharsetMapper() {
60 this(DEFAULT_RESOURCE);
61 }
62
63
64 /**
65 * Construct a new CharsetMapper using the specified properties resource.
66 *
67 * @param name Name of a properties resource to be loaded
68 *
69 * @exception IllegalArgumentException if the specified properties
70 * resource could not be loaded for any reason.
71 */
72 public CharsetMapper(String name) {
73 if (JreCompat.isGraalAvailable()) {
74 map.put("en", "ISO-8859-1");
75 } else {
76 try (InputStream stream = this.getClass().getResourceAsStream(name)) {
77 map.load(stream);
78 } catch (Throwable t) {
79 ExceptionUtils.handleThrowable(t);
80 throw new IllegalArgumentException(t);
81 }
82 }
83 }
84
85
86 // ---------------------------------------------------- Instance Variables
87
88
89 /**
90 * The mapping properties that have been initialized from the specified or
91 * default properties resource.
92 */
93 private Properties map = new Properties();
94
95
96 // ------------------------------------------------------- Public Methods
97
98
99 /**
100 * Calculate the name of a character set to be assumed, given the specified
101 * Locale and the absence of a character set specified as part of the
102 * content type header.
103 *
104 * @param locale The locale for which to calculate a character set
105 * @return the charset name
106 */
107 public String getCharset(Locale locale) {
108 // Match full language_country_variant first, then language_country,
109 // then language only
110 String charset = map.getProperty(locale.toString());
111 if (charset == null) {
112 charset = map.getProperty(locale.getLanguage() + "_"
113 + locale.getCountry());
114 if (charset == null) {
115 charset = map.getProperty(locale.getLanguage());
116 }
117 }
118 return charset;
119 }
120
121
122 /**
123 * The deployment descriptor can have a
124 * locale-encoding-mapping-list element which describes the
125 * webapp's desired mapping from locale to charset. This method
126 * gets called when processing the web.xml file for a context
127 *
128 * @param locale The locale for a character set
129 * @param charset The charset to be associated with the locale
130 */
131 public void addCharsetMappingFromDeploymentDescriptor(String locale, String charset) {
132 map.put(locale, charset);
133 }
134
135
136 }
137