You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2019/11/28 09:37:33 UTC
svn commit: r1870549 - in /turbine/core/trunk/src/java/org/apache/turbine:
./ modules/screens/ pipeline/ services/rundata/ services/uniqueid/
services/velocity/ util/
Author: tv
Date: Thu Nov 28 09:37:33 2019
New Revision: 1870549
URL: http://svn.apache.org/viewvc?rev=1870549&view=rev
Log:
Replace charset strings with Charset objects where possible
Modified:
turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java
turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java
turbine/core/trunk/src/java/org/apache/turbine/modules/screens/PlainJSONScreen.java
turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java
turbine/core/trunk/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java
turbine/core/trunk/src/java/org/apache/turbine/services/uniqueid/TurbineUniqueIdService.java
turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java
turbine/core/trunk/src/java/org/apache/turbine/util/RunData.java
Modified: turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/TurbineConstants.java Thu Nov 28 09:37:33 2019
@@ -19,10 +19,10 @@ package org.apache.turbine;
* under the License.
*/
-import org.apache.turbine.pipeline.TurbinePipeline;
-
-
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import org.apache.turbine.pipeline.TurbinePipeline;
/**
* This interface contains all the constants used throughout
@@ -200,28 +200,22 @@ public interface TurbineConstants
String DEFAULT_DOCUMENT_TYPE_KEY = "default.doctype";
/** Default doctype root element. */
- String DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_KEY
- = "default.html.doctype.root.element";
+ String DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_KEY = "default.html.doctype.root.element";
/** Default value for the doctype root element */
- String DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_DEFAULT
- = "HTML";
+ String DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_DEFAULT = "HTML";
/** Default doctype dtd. */
- String DEFAULT_HTML_DOCTYPE_IDENTIFIER_KEY
- = "default.html.doctype.identifier";
+ String DEFAULT_HTML_DOCTYPE_IDENTIFIER_KEY = "default.html.doctype.identifier";
- /** Default Doctype dtd value */
- String DEFAULT_HTML_DOCTYPE_IDENTIFIER_DEFAULT
- = "-//W3C//DTD HTML 4.01 Transitional//EN";
+ /** Default Doctype dtd value (empty for HTML5) */
+ String DEFAULT_HTML_DOCTYPE_IDENTIFIER_DEFAULT = "";
/** Default doctype url. */
- String DEFAULT_HTML_DOCTYPE_URI_KEY
- = "default.html.doctype.url";
+ String DEFAULT_HTML_DOCTYPE_URI_KEY = "default.html.doctype.url";
- /** Default doctype url value. */
- String DEFAULT_HTML_DOCTYPE_URI_DEFAULT
- = "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd";
+ /** Default doctype url value. (empty for HTML5) */
+ String DEFAULT_HTML_DOCTYPE_URI_DEFAULT = "";
/** Define content types **/
String DEFAULT_HTML_CONTENT_TYPE = "text/html";
@@ -232,19 +226,19 @@ public interface TurbineConstants
String LOCALE_DEFAULT_LANGUAGE_KEY = "locale.default.language";
/** Default value for Language property */
- String LOCALE_DEFAULT_LANGUAGE_DEFAULT = "en";
+ String LOCALE_DEFAULT_LANGUAGE_DEFAULT = Locale.US.getLanguage();
/** Default Country property */
String LOCALE_DEFAULT_COUNTRY_KEY = "locale.default.country";
/** Default value for Country property */
- String LOCALE_DEFAULT_COUNTRY_DEFAULT = "US";
+ String LOCALE_DEFAULT_COUNTRY_DEFAULT = Locale.US.getCountry();
/** Default Charset property */
String LOCALE_DEFAULT_CHARSET_KEY = "locale.default.charset";
/** Default value for Charset property */
- String LOCALE_DEFAULT_CHARSET_DEFAULT = "ISO-8859-1";
+ String LOCALE_DEFAULT_CHARSET_DEFAULT = StandardCharsets.ISO_8859_1.name();
/** Override Charset property */
String LOCALE_OVERRIDE_CHARSET_KEY = "locale.override.charset";
@@ -292,7 +286,7 @@ public interface TurbineConstants
String PARAMETER_ENCODING_KEY = "input.encoding";
/** Default Encoding for Parameter Parser */
- String PARAMETER_ENCODING_DEFAULT = "ISO-8859-1";
+ String PARAMETER_ENCODING_DEFAULT = StandardCharsets.ISO_8859_1.name();
/** Default serverName for ServerData */
String DEFAULT_SERVER_NAME_KEY
Modified: turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/modules/screens/JSONScreen.java Thu Nov 28 09:37:33 2019
@@ -24,6 +24,8 @@ import java.io.CharArrayWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
@@ -111,7 +113,7 @@ public class JSONScreen extends RawScree
HttpServletRequest request = data.getRequest();
// we require utf-8, cft
- String charset = "UTF-8"; //request.getCharacterEncoding();
+ Charset charset = StandardCharsets.UTF_8; //request.getCharacterEncoding();
BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));
// Read the request
@@ -131,7 +133,7 @@ public class JSONScreen extends RawScree
Object json_res = jsonRpcService.processCall(cdata, json_bridge, request);
PrintWriter out = new PrintWriter(
- new OutputStreamWriter(data.getResponse().getOutputStream(),charset));
+ new OutputStreamWriter(data.getResponse().getOutputStream(), charset));
out.print(json_res.toString());
out.flush();
out.close();
Modified: turbine/core/trunk/src/java/org/apache/turbine/modules/screens/PlainJSONScreen.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/modules/screens/PlainJSONScreen.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/modules/screens/PlainJSONScreen.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/modules/screens/PlainJSONScreen.java Thu Nov 28 09:37:33 2019
@@ -21,6 +21,8 @@ package org.apache.turbine.modules.scree
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.apache.turbine.pipeline.PipelineData;
import org.apache.turbine.util.RunData;
@@ -100,9 +102,9 @@ public class PlainJSONScreen extends Raw
{
RunData data = pipelineData.getRunData();
// read in json!
- String charset = "UTF-8"; //request.getCharacterEncoding();
+ Charset charset = StandardCharsets.UTF_8; //request.getCharacterEncoding();
- String json_res =data.getMessage();
+ String json_res = data.getMessage();
log.debug( "json_res output: {}", json_res );
PrintWriter out = new PrintWriter(
Modified: turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/pipeline/DefaultSetEncodingValve.java Thu Nov 28 09:37:33 2019
@@ -23,6 +23,7 @@ package org.apache.turbine.pipeline;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import javax.servlet.http.HttpServletRequest;
@@ -77,13 +78,13 @@ public class DefaultSetEncodingValve
}
// Copy encoding charset to RunData to set a reasonable default for the response
- String outputEncoding = LocaleUtils.getOverrideCharSet();
+ Charset outputEncoding = LocaleUtils.getOverrideCharset();
if (outputEncoding == null)
{
- outputEncoding = requestEncoding;
+ outputEncoding = Charset.forName(requestEncoding);
}
- pipelineData.getRunData().setCharSet(outputEncoding);
+ pipelineData.getRunData().setCharset(outputEncoding);
// Pass control to the next Valve in the Pipeline
context.invokeNext(pipelineData);
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java Thu Nov 28 09:37:33 2019
@@ -21,6 +21,7 @@ package org.apache.turbine.services.rund
import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -59,10 +60,10 @@ import org.apache.turbine.util.template.
* RunData service, if another implementation is not defined in
* the default or specified RunData configuration.
* TurbineRunData is an extension to RunData, which
- * is an interface to run-rime information that is passed
+ * is an interface to run-time information that is passed
* within Turbine. This provides the threading mechanism for the
* entire system because multiple requests can potentially come in
- * at the same time. Thus, there is only one RunData implementation
+ * at the same time. Thus, there is only one RunData instance
* for each request that is being serviced.
*
* <p>DefaultTurbineRunData implements the Recyclable interface making
@@ -110,10 +111,10 @@ public class DefaultTurbineRunData
private PrintWriter out;
/** The HTTP charset. */
- private String charSet;
+ private Charset charSet;
/** The HTTP content type to return. */
- private String contentType = "text/html";
+ private String contentType = TurbineConstants.DEFAULT_HTML_CONTENT_TYPE;
/** If this is set, also set the status code to 302. */
private String redirectURI;
@@ -233,7 +234,7 @@ public class DefaultTurbineRunData
outSet = false;
out = null;
charSet = null;
- contentType = "text/html";
+ contentType = TurbineConstants.DEFAULT_HTML_CONTENT_TYPE;
redirectURI = null;
statusCode = HttpServletResponse.SC_OK;
errors.clear();
@@ -879,12 +880,38 @@ public class DefaultTurbineRunData
@Override
public String getCharSet()
{
- log.debug("getCharSet()");
+ return getCharset().name();
+ }
+
+ /**
+ * Sets the charset.
+ *
+ * @param charSet the name of the new charset.
+ */
+ @Override
+ public void setCharSet(String charSet)
+ {
+ setCharset(Charset.forName(charSet));
+ }
+
+ /**
+ * Gets the charset. If it has not already been defined with
+ * setCharSet(), then a property named "locale.default.charset"
+ * is checked from the Resource Service and returned. If this
+ * property is undefined, the default charset of the locale
+ * is returned. If the locale is undefined, null is returned.
+ *
+ * @return the charset or null.
+ */
+ @Override
+ public Charset getCharset()
+ {
+ log.debug("getCharset()");
- if (StringUtils.isEmpty(charSet))
+ if (charSet == null)
{
log.debug("Charset was null!");
- charSet = LocaleUtils.getDefaultCharSet();
+ charSet = LocaleUtils.getDefaultCharset();
}
return charSet;
@@ -893,12 +920,12 @@ public class DefaultTurbineRunData
/**
* Sets the charset.
*
- * @param charSet the name of the new charset.
+ * @param charSet the new charset.
*/
@Override
- public void setCharSet(String charSet)
+ public void setCharset(Charset charSet)
{
- log.debug("setCharSet({})", charSet);
+ log.debug("setCharset({})", charSet);
this.charSet = charSet;
}
@@ -918,18 +945,18 @@ public class DefaultTurbineRunData
{
if (StringUtils.isNotEmpty(contentType))
{
- if (StringUtils.isEmpty(charSet))
+ if (charSet == null)
{
if (contentType.startsWith("text/"))
{
- return contentType + "; charset=" + LocaleUtils.getDefaultCharSet();
+ return contentType + "; charset=" + LocaleUtils.getDefaultCharset();
}
return contentType;
}
else
{
- return contentType + "; charset=" + charSet;
+ return contentType + "; charset=" + charSet.name();
}
}
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/uniqueid/TurbineUniqueIdService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/uniqueid/TurbineUniqueIdService.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/uniqueid/TurbineUniqueIdService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/uniqueid/TurbineUniqueIdService.java Thu Nov 28 09:37:33 2019
@@ -53,7 +53,7 @@ public class TurbineUniqueIdService
private static String turbineURL = "UNKNOWN";
- private static AtomicInteger counter;
+ private static final AtomicInteger counter = new AtomicInteger();
/**
@@ -66,7 +66,7 @@ public class TurbineUniqueIdService
{
try
{
- counter = new AtomicInteger();
+ counter.set(0);
// This might be a problem if the unique Id Service runs
// before Turbine got its first request. In this case,
Modified: turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java Thu Nov 28 09:37:33 2019
@@ -25,11 +25,11 @@ import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.configuration2.Configuration;
-import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
@@ -95,10 +95,10 @@ public class TurbineVelocityService
private static final Logger log = LogManager.getLogger(TurbineVelocityService.class);
/** Encoding used when reading the templates. */
- private String defaultInputEncoding;
+ private Charset defaultInputEncoding;
/** Encoding used by the outputstream when handling the requests. */
- private String defaultOutputEncoding;
+ private Charset defaultOutputEncoding;
/** Is the pullModelActive? */
private boolean pullModelActive = false;
@@ -143,14 +143,24 @@ public class TurbineVelocityService
// Register with the template service.
registerConfiguration(VelocityService.VELOCITY_EXTENSION);
- defaultInputEncoding = getConfiguration().getString("input.encoding", LocaleUtils.getDefaultInputEncoding());
+ String inputEncoding = getConfiguration().getString("input.encoding", LocaleUtils.getDefaultInputEncoding());
+ defaultInputEncoding = Charset.forName(inputEncoding);
- String outputEncoding = LocaleUtils.getOverrideCharSet();
- if (outputEncoding == null)
+ String outputEncodingString = getConfiguration().getString("output.encoding");
+ if (outputEncodingString == null)
{
- outputEncoding = defaultInputEncoding;
+ Charset outputEncoding = LocaleUtils.getOverrideCharset();
+ if (outputEncoding == null)
+ {
+ outputEncoding = defaultInputEncoding;
+ }
+
+ defaultOutputEncoding = outputEncoding;
+ }
+ else
+ {
+ defaultOutputEncoding = Charset.forName(outputEncodingString);
}
- defaultOutputEncoding = getConfiguration().getString("output.encoding", defaultInputEncoding);
setInit(true);
}
@@ -281,7 +291,7 @@ public class TurbineVelocityService
{
String results = null;
OutputStreamWriter writer = null;
- String charset = getOutputCharSet(context);
+ Charset charset = getOutputCharSet(context);
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream())
{
@@ -289,7 +299,7 @@ public class TurbineVelocityService
executeRequest(context, filename, writer);
writer.flush();
- results = bytes.toString(charset);
+ results = bytes.toString(charset.name());
}
catch (Exception e)
{
@@ -316,7 +326,7 @@ public class TurbineVelocityService
OutputStream output)
throws TurbineException
{
- String charset = getOutputCharSet(context);
+ Charset charset = getOutputCharSet(context);
try (OutputStreamWriter writer = new OutputStreamWriter(output, charset))
{
@@ -385,14 +395,14 @@ public class TurbineVelocityService
Writer writer)
throws Exception
{
- String encoding = getTemplateEncoding(context);
+ Charset encoding = getTemplateEncoding(context);
if (encoding == null)
{
encoding = defaultOutputEncoding;
}
- velocity.mergeTemplate(filename, encoding, context, writer);
+ velocity.mergeTemplate(filename, encoding.name(), context, writer);
}
/**
@@ -401,17 +411,17 @@ public class TurbineVelocityService
* @param context A Context.
* @return The character set applied to the resulting String.
*/
- private String getOutputCharSet(Context context)
+ private Charset getOutputCharSet(Context context)
{
- String charset = null;
+ Charset charset = null;
Object data = context.get(VelocityService.RUNDATA_KEY);
if ((data != null) && (data instanceof RunData))
{
- charset = ((RunData) data).getCharSet();
+ charset = ((RunData) data).getCharset();
}
- return (StringUtils.isEmpty(charset)) ? defaultOutputEncoding : charset;
+ return charset == null ? defaultOutputEncoding : charset;
}
/**
@@ -420,14 +430,14 @@ public class TurbineVelocityService
* @param context A Context.
* @return The encoding applied to the resulting String.
*/
- private String getTemplateEncoding(Context context)
+ private Charset getTemplateEncoding(Context context)
{
- String encoding = null;
+ Charset encoding = null;
Object data = context.get(VelocityService.RUNDATA_KEY);
if ((data != null) && (data instanceof RunData))
{
- encoding = ((RunData) data).getTemplateEncoding();
+ encoding = Charset.forName(((RunData) data).getTemplateEncoding());
}
return encoding != null ? encoding : defaultInputEncoding;
Modified: turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/util/LocaleUtils.java Thu Nov 28 09:37:33 2019
@@ -1,5 +1,10 @@
package org.apache.turbine.util;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -29,6 +34,24 @@ import org.apache.turbine.Turbine;
import org.apache.turbine.TurbineConstants;
import org.apache.turbine.services.ServiceManager;
import org.apache.turbine.services.TurbineServices;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
/**
* This class provides utilities for handling locales and charsets
@@ -44,7 +67,7 @@ public class LocaleUtils
private static Locale defaultLocale = null;
/** The default charset. */
- private static String defaultCharSet = null;
+ private static Charset defaultCharSet = null;
/**
* Returns the default input encoding for the servlet.
@@ -98,20 +121,37 @@ public class LocaleUtils
*
* @return the name of the default charset or null.
*/
+ @Deprecated
public static String getDefaultCharSet()
{
+ return getDefaultCharset().name();
+ }
+
+ /**
+ * Gets the default charset defined by a property named
+ * "locale.default.charset"
+ *
+ * @return the default charset, never null.
+ */
+ public static Charset getDefaultCharset()
+ {
if (defaultCharSet == null)
{
/* Get the default charset and cache it in a static variable. */
- defaultCharSet = Turbine.getConfiguration()
+ String charSet = Turbine.getConfiguration()
.getString(TurbineConstants.LOCALE_DEFAULT_CHARSET_KEY,
TurbineConstants.LOCALE_DEFAULT_CHARSET_DEFAULT);
- log.debug("defaultCharSet = {} (From Properties)", defaultCharSet);
+
+ if (StringUtils.isNotEmpty(charSet))
+ {
+ defaultCharSet = charSetForName(charSet);
+ log.debug("defaultCharSet = {} (From Properties)", defaultCharSet);
+ }
}
- String charset = defaultCharSet;
+ Charset charset = defaultCharSet;
- if (StringUtils.isEmpty(charset)) // can happen if set explicitly in the configuration
+ if (charset == null) // can happen if set explicitly in the configuration
{
log.debug("Default charset is empty!");
/* Default charset isn't specified, get the locale specific one. */
@@ -122,18 +162,26 @@ public class LocaleUtils
{
log.debug("We don't have US Locale!");
ServiceManager serviceManager = TurbineServices.getInstance();
- MimeTypeService mimeTypeService = null;
- try
- {
- mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE);
- }
- catch (Exception e)
+ if (serviceManager.isRegistered(MimeTypeService.ROLE))
{
- throw new RuntimeException(e);
- }
- charset = mimeTypeService.getCharSet(locale);
+ try
+ {
+ MimeTypeService mimeTypeService = (MimeTypeService) serviceManager.getService(MimeTypeService.ROLE);
+ charset = charSetForName(mimeTypeService.getCharSet(locale));
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
- log.debug("Charset now {}", charset);
+ log.debug("Charset now {}", charset);
+ }
+ }
+
+ // The fallback to end all fallbacks
+ if (charset == null)
+ {
+ charset = StandardCharsets.ISO_8859_1;
}
}
@@ -148,9 +196,53 @@ public class LocaleUtils
*
* @return the name of the override charset or null.
*/
+ @Deprecated
public static String getOverrideCharSet()
{
return Turbine.getConfiguration()
.getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
}
+
+ /**
+ * Gets the charset defined by a property named "locale.override.charset"
+ * This property has no default. If it exists, the output charset is always
+ * set to its value
+ *
+ * @return the override charset or null.
+ */
+ public static Charset getOverrideCharset()
+ {
+ String charset = Turbine.getConfiguration()
+ .getString(TurbineConstants.LOCALE_OVERRIDE_CHARSET_KEY);
+
+ if (StringUtils.isEmpty(charset))
+ {
+ return null;
+ }
+
+ return charSetForName(charset);
+ }
+
+ /**
+ * Get a Charset object for a given name
+ * This method does not throw exceptions on illegal input but returns null.
+ *
+ * @param charSet the charset name
+ *
+ * @return the Charset or null if it does not exist
+ */
+ private static Charset charSetForName(String charSet)
+ {
+ try
+ {
+ return Charset.forName(charSet);
+ }
+ catch (IllegalCharsetNameException | UnsupportedCharsetException e)
+ {
+ log.error("Illegal default charset {}", charSet);
+ }
+
+ return null;
+ }
+
}
Modified: turbine/core/trunk/src/java/org/apache/turbine/util/RunData.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/util/RunData.java?rev=1870549&r1=1870548&r2=1870549&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/util/RunData.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/util/RunData.java Thu Nov 28 09:37:33 2019
@@ -23,6 +23,7 @@ package org.apache.turbine.util;
import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
@@ -413,6 +414,7 @@ public interface RunData extends Pipelin
*
* @return the name of the charset or null.
*/
+ @Deprecated
String getCharSet();
/**
@@ -420,9 +422,28 @@ public interface RunData extends Pipelin
*
* @param charset the name of the new charset.
*/
+ @Deprecated
void setCharSet(String charset);
/**
+ * Gets the charset. If it has not already been defined with
+ * setCharSet(), then a property named "locale.default.charset"
+ * is checked from the Resource Service and returned. If this
+ * property is undefined, the default charset of the locale
+ * is returned. If the locale is undefined, null is returned.
+ *
+ * @return the charset or null.
+ */
+ Charset getCharset();
+
+ /**
+ * Sets the charset.
+ *
+ * @param charset the new charset.
+ */
+ void setCharset(Charset charset);
+
+ /**
* Gets the HTTP content type to return. If a charset
* has been specified, it is included in the content type.
* If the charset has not been specified and the main type