Normalize Post Data
/*
JSPWiki - a JSP-based WikiWiki clone.
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.
*/
import java.security.SecureRandom;
import java.util.Properties;
import java.util.Random;
public class StringUtils
{
/**
* Makes sure that the POSTed data is conforms to certain rules. These
* rules are:
* <UL>
* <LI>The data always ends with a newline (some browsers, such
* as NS4.x series, does not send a newline at the end, which makes
* the diffs a bit strange sometimes.
* <LI>The CR/LF/CRLF mess is normalized to plain CRLF.
* </UL>
*
* The reason why we're using CRLF is that most browser already
* return CRLF since that is the closest thing to a HTTP standard.
*
* @param postData The data to normalize
* @return Normalized data
*/
public static String normalizePostData( String postData )
{
StringBuffer sb = new StringBuffer();
for( int i = 0; i < postData.length(); i++ )
{
switch( postData.charAt(i) )
{
case 0x0a: // LF, UNIX
sb.append( "\r\n" );
break;
case 0x0d: // CR, either Mac or MSDOS
sb.append( "\r\n" );
// If it's MSDOS, skip the LF so that we don't add it again.
if( i < postData.length()-1 && postData.charAt(i+1) == 0x0a )
{
i++;
}
break;
default:
sb.append( postData.charAt(i) );
break;
}
}
if( sb.length() < 2 || !sb.substring( sb.length()-2 ).equals("\r\n") )
{
sb.append( "\r\n" );
}
return sb.toString();
}
}
Related examples in the same category