dbarra / xdocreport

Automatically exported from code.google.com/p/xdocreport
1 stars 0 forks source link

Problem with encoding on XDocReport Servlet: Error report generation #215

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Trying to document.insertBefore in MacroButtonName preprocessor
2. AbstractProcessXDocReportServlet using function protected void error( 
HttpServletRequest request, HttpServletResponse response, Exception cause )
3. error in 

What is the expected output? What do you see instead?
Error 500--Internal Server Error

java.io.CharConversionException: Not an ISO 8859-1 character: у
    at javax.servlet.ServletOutputStream.print(ServletOutputStream.java:99)
    at view.DocXtoPdf.error(DocXtoPdf.java:83)
    at fr.opensagres.xdocreport.document.web.AbstractProcessXDocReportServlet.doGenerateReport(AbstractProcessXDocReportServlet.java:255)
    at fr.opensagres.xdocreport.document.web.AbstractProcessXDocReportServlet.processRequest(AbstractProcessXDocReportServlet.java:111)
    at fr.opensagres.xdocreport.document.web.BaseXDocReportServlet.doGet(BaseXDocReportServlet.java:78)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

What version of the product are you using? On what operating system?
1.0.1
win

Please provide any additional information below.
Error message in not in ISO 8859-1 charset

Original issue reported on code.google.com by MrSo...@gmail.com on 16 Jan 2013 at 8:09

GoogleCodeExporter commented 8 years ago
Solved it by using HtmlEncoder:
---------------------------------------
import java.io.IOException;

public class HtmlEncoder {
    public static final HtmlEncoder INSTANCE = new HtmlEncoder();

    public void encode(CharSequence sequence, Appendable out) throws IOException {
        for (int i = 0; i < sequence.length(); i++) {
            char ch = sequence.charAt(i);
            if (Character.UnicodeBlock.of(ch) == Character.UnicodeBlock.BASIC_LATIN) {
                out.append(ch);
            } else {
                int codepoint = Character.codePointAt(sequence, i);
                // handle supplementary range chars
                i += Character.charCount(codepoint) - 1;
                // emit entity
                out.append("&#x");
                out.append(Integer.toHexString(codepoint));
                out.append(";");
            }
        }
    }
}
---------------------------------
---------------------------------
    @Override
    protected void error( HttpServletRequest request, HttpServletResponse response, Exception cause )
        throws ServletException, IOException
    {
        if ( response.isCommitted() )
        {
            throw new ServletException( cause );
        }
        response.setContentType( "text/html; charset=windows-1251" );
        StringBuilder html = new StringBuilder();
        html.append( "<html>" );
        html.append( "<title>Error</title>" );
        html.append( "<body bgcolor=\"#ffffff\">" );
        html.append( "<h2>XDocReport Servlet: Error report generation</h2>" );
        html.append( "<pre>" );
        String why = cause.getMessage();

        if ( why != null && why.trim().length() > 0 )
        {
            //html.append( why );
            HtmlEncoder.INSTANCE.encode(why, html);
            html.append( "<br>" );
        }

        StringWriter sw = new StringWriter();
        cause.printStackTrace( new PrintWriter( sw ) );

        //html.append( sw.toString() );
        HtmlEncoder.INSTANCE.encode(sw.toString(), html);

        html.append( "</pre>" );
        html.append( "</body>" );
        html.append( "</html>" );
        response.getOutputStream().print( html.toString() );
    }
-------------------------------

Now it is in UTF-8

Original comment by MrSo...@gmail.com on 16 Jan 2013 at 8:11

GoogleCodeExporter commented 8 years ago
Hi,

Many thank's to have fixed this problem. Just one question, do you need force 
the encoding with "charset=windows-1251" ?

I will do apply your fix, as soon as possible.

Regards Angelo

Original comment by angelo.z...@gmail.com on 16 Jan 2013 at 8:19

GoogleCodeExporter commented 8 years ago
You mean this?
response.setContentType( "text/html; charset=windows-1251" );
No I don't really need this, it displays correct without this charset.
But if you have time see Issue 212.

Original comment by MrSo...@gmail.com on 16 Jan 2013 at 9:22