markt 2005/04/04 13:57:02 Modified: catalina/src/conf web.xml catalina/src/share/org/apache/catalina/ssi SSIServlet.java SSIServletExternalResolver.java webapps/docs changelog.xml ssi-howto.xml Log: Fix bug 10385. Improve support for files that use character encodings other than the platform default with the SSI servlet. - Ported from TC4 Revision Changes Path 1.56 +7 -0 jakarta-tomcat-catalina/catalina/src/conf/web.xml Index: web.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/conf/web.xml,v retrieving revision 1.55 retrieving revision 1.56 diff -u -r1.55 -r1.56 --- web.xml 18 Dec 2004 12:04:40 -0000 1.55 +++ web.xml 4 Apr 2005 20:57:02 -0000 1.56 @@ -226,6 +226,13 @@ <!-- relative to the context root, instead of --> <!-- the server root? (0=false, 1=true) [0] --> <!-- --> + <!-- inputEncoding The encoding to assume for SSI resources if --> + <!-- one is not available from the resource. --> + <!-- [Platform default] --> + <!-- --> + <!-- outputEncoding The encoding to use for the page that results --> + <!-- from the SSI processing. [UTF-8] --> + <!-- --> <!-- --> <!-- IMPORTANT: To use the SSI servlet, you also need to rename the --> <!-- $CATALINA_HOME/server/lib/servlets-ssi.renametojar file --> 1.9 +34 -6 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ssi/SSIServlet.java Index: SSIServlet.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ssi/SSIServlet.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- SSIServlet.java 1 Sep 2004 18:33:33 -0000 1.8 +++ SSIServlet.java 4 Apr 2005 20:57:02 -0000 1.9 @@ -44,6 +44,10 @@ protected Long expires = null; /** virtual path can be webapp-relative */ protected boolean isVirtualWebappRelative = false; + /** Input encoding. If not specified, uses platform default */ + protected String inputEncoding = null; + /** Output encoding. If not specified, uses platform default */ + protected String outputEncoding = "UTF-8"; //----------------- Public methods. @@ -83,6 +87,19 @@ } catch (Throwable t) { ; } + try { + inputEncoding = getServletConfig().getInitParameter("inputEncoding"); + } catch (Throwable t) { + ; + } + try { + value = getServletConfig().getInitParameter("outputEncoding"); + if (value != null) { + outputEncoding = value; + } + } catch (Throwable t) { + ; + } if (debug > 0) log("SSIServlet.init() SSI invoker started with 'debug'=" + debug); } @@ -159,9 +176,9 @@ } String resourceMimeType = servletContext.getMimeType(path); if (resourceMimeType == null) { - resourceMimeType = "text/html;charset=UTF-8"; + resourceMimeType = "text/html"; } - res.setContentType(resourceMimeType); + res.setContentType(resourceMimeType + ";charset=" + outputEncoding); if (expires != null) { res.setDateHeader("Expires", (new java.util.Date()).getTime() + expires.longValue() * 1000); @@ -174,7 +191,7 @@ protected void processSSI(HttpServletRequest req, HttpServletResponse res, URL resource) throws IOException { SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver( - this, req, res, isVirtualWebappRelative, debug); + this, req, res, isVirtualWebappRelative, debug, inputEncoding); SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug); PrintWriter printWriter = null; @@ -185,10 +202,21 @@ } else { printWriter = res.getWriter(); } + URLConnection resourceInfo = resource.openConnection(); InputStream resourceInputStream = resourceInfo.getInputStream(); - BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(resourceInputStream)); + String encoding = resourceInfo.getContentEncoding(); + if (encoding == null) { + encoding = inputEncoding; + } + InputStreamReader isr; + if (encoding == null) { + isr = new InputStreamReader(resourceInputStream); + } else { + isr = new InputStreamReader(resourceInputStream, encoding); + } + BufferedReader bufferedReader = new BufferedReader(isr); + Date lastModifiedDate = new Date(resourceInfo.getLastModified()); ssiProcessor.process(bufferedReader, lastModifiedDate, printWriter); if (buffered) { 1.5 +15 -12 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ssi/SSIServletExternalResolver.java Index: SSIServletExternalResolver.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/ssi/SSIServletExternalResolver.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- SSIServletExternalResolver.java 1 Sep 2004 18:33:33 -0000 1.4 +++ SSIServletExternalResolver.java 4 Apr 2005 20:57:02 -0000 1.5 @@ -42,16 +42,17 @@ protected HttpServletResponse res; protected boolean isVirtualWebappRelative; protected int debug; - + protected String inputEncoding; public SSIServletExternalResolver(HttpServlet servlet, HttpServletRequest req, HttpServletResponse res, - boolean isVirtualWebappRelative, int debug) { + boolean isVirtualWebappRelative, int debug, String inputEncoding) { this.servlet = servlet; this.req = req; this.res = res; this.isVirtualWebappRelative = isVirtualWebappRelative; this.debug = debug; + this.inputEncoding = inputEncoding; } @@ -355,11 +356,8 @@ //We are making lots of unnecessary copies of the included data here. If - // someone ever - // complains that this - //is slow, we should connect the included stream to the print writer that - // SSICommand - // uses. + //someone ever complains that this is slow, we should connect the included + // stream to the print writer that SSICommand uses. public String getFileText(String originalPath, boolean virtual) throws IOException { try { @@ -379,10 +377,15 @@ //We can't assume the included servlet flushed its output responseIncludeWrapper.flushOutputStreamOrWriter(); byte[] bytes = basos.toByteArray(); - //Assume that the default encoding is what was used to encode the - // bytes. - // Questionable. - String retVal = new String(bytes); + + //Assume platform default encoding unless otherwise specified + String retVal; + if (inputEncoding == null) { + retVal = new String( bytes ); + } else { + retVal = new String (bytes, inputEncoding); + } + //make an assumption that an empty response is a failure. This is // a problem // if a truly empty file 1.283 +4 -0 jakarta-tomcat-catalina/webapps/docs/changelog.xml Index: changelog.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/webapps/docs/changelog.xml,v retrieving revision 1.282 retrieving revision 1.283 diff -u -r1.282 -r1.283 --- changelog.xml 4 Apr 2005 13:57:49 -0000 1.282 +++ changelog.xml 4 Apr 2005 20:57:02 -0000 1.283 @@ -75,6 +75,10 @@ Store principal to be exposed for Request.getUserPrincipal inside the GenericPrincipal, to remove hacks from the JAAS realm (remm) </update> + <fix> + <bug>10385</bug>: SSI Servlet now includes better support for files that use character + encodings other than the platform default.(markt) + </fix> </changelog> </subsection> 1.3 +5 -0 jakarta-tomcat-catalina/webapps/docs/ssi-howto.xml Index: ssi-howto.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/webapps/docs/ssi-howto.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ssi-howto.xml 15 Jan 2003 03:40:43 -0000 1.2 +++ ssi-howto.xml 4 Apr 2005 20:57:02 -0000 1.3 @@ -65,6 +65,11 @@ <li><strong>isVirtualWebappRelative</strong> - Should "virtual" SSI directive paths be interpreted as relative to the context root, instead of the server root? (0=false, 1=true) Default 0 (false).</li> +<li><strong>inputEncoding</strong> - The encoding to be assumed for SSI +resources if one cannot be determined from the resource itself. Default is +the default platform encoding.</li> +<li><strong>outputEncoding</strong> - The encoding to be used for the result +of the SSI processing. Default is UTF-8.</li> </ul> </p>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]