TitleApplication name be garbled if there are Chinese characters in it
Date19-Jun-2005 12:36:31 EEST
Version2.2.20
Submitter219.125.196.14
Bug criticalityJSPWiki:BadBug
Browser versionFirefox 1.0.4
Bug statusClosedBug
PageProvider usedVersioningFileProvider
Servlet ContainerTomcat 5.0.28
Operating SystemWindows 2003
URLInstall.jsp and every page cantains application name
Java version1.5.0_02

If I set the application name contains Chinese character or Japanese character in Install.jsp,the application name garbled,in both web pages and jspwiki.properties file.

I found it's caused by 2 reasons in Install.jsp.

First,it doesn't do a encoding convertting when getting data from request.Just do like this: request.getParameter( "appname" )

Second,it doesn't do a Native2Ascii convertting when saving data to jspwiki.properties file.

I've made some change to Install.jsp,and it seems working well.The code is as bellow:


<?xml version="1.0" encoding="UTF-8"?>
<%@ taglib uri="/WEB-INF/jspwiki.tld" prefix="wiki" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.ecyrd.jspwiki.*" %>
<%@ page import="com.ecyrd.jspwiki.providers.*" %>

<%!
    String message = null;
    String propertyResult = null;
    
    public String sanitizePath( String s )
    {
        s = TextUtil.replaceString(s, "\\", "\\\\" );
        return s.trim();
    }
    
    /**
     *  Simply sanitizes any URL which contains backslashes (sometimes Windows users may have them)
     */
    public String sanitizeURL( String s )
    {
        s = TextUtil.replaceString(s, "\\", "/" );
        return s.trim();
    }
    
	public String setProperty( String propertyfile, String key, String value )
	{
//Dengber Add Start/////////////////////////////////////////////////////////
		value = native2Ascii(value);
//Dengber Add End///////////////////////////////////////////////////////////
        int idx = 0;
        
		while( (idx < propertyfile.length()) && ((idx = propertyfile.indexOf(key,idx)) != -1) )
		{
            int prevret = propertyfile.lastIndexOf("\n",idx);
            if( prevret != -1 )
            {
                // Commented lines are skipped
                if( propertyfile.charAt(prevret+1) == '#' ) 
                {
                    idx += key.length();
                    continue;
                }
            }
			int eqsign = propertyfile.indexOf("=",idx);
			
			if( eqsign != -1 )
			{
				int ret = propertyfile.indexOf("\n",eqsign);
				
				if( ret == -1 ) ret = propertyfile.length();
				
				propertyfile = TextUtil.replaceString( propertyfile, eqsign+1, ret, 
					value );
                
                return propertyfile;
			}
            // idx += key.length();
		}

        //
        //  If it was not found, we'll add it here.
        //
        
        propertyfile += "\n"+key+" = "+value+"\n";
		
		return propertyfile;
	}
	
    public void writeProperties( File propertyFile, String contents )
        throws IOException
    {
//Dengber Change Start/////////////////////////////////////////////////////////
//        byte[] bytes = contents.getBytes("ISO-8859-1");
        byte[] bytes = contents.getBytes();
//Dengber Change End///////////////////////////////////////////////////////////
        OutputStream out = null;
        
        try
        {
            out = new FileOutputStream( propertyFile );
            
            FileUtil.copyContents( new ByteArrayInputStream( bytes ),
                                   out );
        }
        finally
        {
            if( out != null ) out.close();
        }
    }
    
    public File findPropertyFile( ServletContext context )
    {
        String path = context.getRealPath("/");
        
        File f = new File( path, WikiEngine.DEFAULT_PROPERTYFILE );
        
        return f;
    }
    
	public String readProperties( ServletContext context )
		throws IOException
	{
        File f = findPropertyFile( context );
		FileReader in = null;
		String contents = null;
		try
		{
			in = new FileReader( f );
            contents = FileUtil.readContents( in );
		}
		finally
		{
			if( in != null ) in.close();
		}
		
		return contents;
	}
//Dengber Add Start/////////////////////////////////////////////////////////
    public String safeGetParameter( ServletRequest request, String name )
        throws UnsupportedEncodingException
    {
        String res = request.getParameter( name );
        if( res != null ) 
        {
            res = new String(res.getBytes("ISO-8859-1"),
                             "utf8" );
        }
        return res;
    }
    public String native2Ascii(String s)
    {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < s.length(); i++)
        {
            char aChar = s.charAt(i);
            if ((aChar < 0x0020) || (aChar > 0x007e)) {
                sb.append('\\');
                sb.append('u');
                sb.append(toHex((aChar >> 12) & 0xF));
                sb.append(toHex((aChar >>  8) & 0xF));
                sb.append(toHex((aChar >>  4) & 0xF));
                sb.append(toHex( aChar        & 0xF));
            } else {
                sb.append(aChar);
            }
        }
        return sb.toString();
    }
    public static char toHex(int nibble) {
        final char[] hexDigit = {
            '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
        };
        return hexDigit[(nibble & 0xF)];
    }
//Dengber Add End///////////////////////////////////////////////////////////

%>

<%
    String propertyString = readProperties( getServletContext() );
    
    Properties props = new Properties();
//Dengber Change Start/////////////////////////////////////////////////////////
//    props.load( new ByteArrayInputStream(propertyString.getBytes("ISO-8859-1")) );
    props.load( new ByteArrayInputStream(propertyString.getBytes()) );
    
//    String appname = request.getParameter( "appname" );
//    String baseurl = request.getParameter( "baseurl" );
//    String dir = request.getParameter( "dir" );
//    String logdir = request.getParameter( "logdir" );
//    String workdir = request.getParameter( "workdir" );
//    String password1 = request.getParameter( "password1" );
//    String password2 = request.getParameter( "password2" );
//    String password  = request.getParameter( "password" );
    String appname = safeGetParameter(request, "appname" );
    String baseurl = safeGetParameter(request, "baseurl" );
    String dir = safeGetParameter(request, "dir" );
    String logdir = safeGetParameter(request, "logdir" );
    String workdir = safeGetParameter(request, "workdir" );
    String password1 = safeGetParameter(request, "password1" );
    String password2 = safeGetParameter(request, "password2" );
    String password  = safeGetParameter(request, "password" );


    String oldpassword = props.getProperty( "jspwiki.auth.masterPassword", null );
    
//    if( request.getParameter("submit") != null )
    if( safeGetParameter(request, "submit") != null )

//Dengber Change End///////////////////////////////////////////////////////////

//No change bellow......


Fixed in 2.2.26

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-5) was last changed on 19-Jun-2005 14:33 by Administrator