Project: * Clean-up (Eclipse cruft, unused files, etc...) * Git-specific changes * Maven POMs clean-up and changes for the build system * Version set to 1.0.0-0 in the development branches * Maven plug-ins updated to latest versions * Very partial dev. documentation added

This commit is contained in:
Emmanuel BENOîT 2011-12-09 08:07:33 +01:00
parent c74e30d5ba
commit 0665a760de
1439 changed files with 1020 additions and 1649 deletions
legacyworlds-server-utils/src

View file

@ -0,0 +1,91 @@
package com.deepclone.lw.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.apache.commons.codec.binary.Base64;
/**
* Helper class for serialisation to Base-64 and de-serialisation from Base-64.
*
* @author tseeker
*/
public final class Base64Serializer
{
/** Empty, private constructor that prevents instantiation */
private Base64Serializer( )
{
// EMPTY
}
/**
* Serialises an object and encodes it as Base-64.
*
* @param value
* object to serialise
* @return Base-64 encoded data resulting from the serialisation
*/
public static String encode( Serializable value )
{
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream( baos );
try {
oos.writeObject( value );
} finally {
try {
oos.close( );
} catch ( IOException e ) {
throw new RuntimeException( e );
}
}
} catch ( IOException e ) {
throw new RuntimeException( e );
}
return Base64.encodeBase64String( baos.toByteArray( ) );
}
/**
* De-serialises an object from a Base-64 encoded string.
*
* @param data
* Base-64 encoded string containing the serialised object
* @return de-serialised object
*/
public static Serializable decode( String data )
{
ByteArrayInputStream bais = new ByteArrayInputStream( Base64.decodeBase64( data ) );
ObjectInputStream ois;
try {
ois = new ObjectInputStream( bais );
try {
return (Serializable) ois.readObject( );
} catch ( ClassNotFoundException e ) {
throw new RuntimeException( e );
} finally {
try {
ois.close( );
} catch ( IOException e ) {
throw new RuntimeException( e );
}
}
} catch ( IOException e ) {
throw new RuntimeException( e );
}
}
}

View file

@ -0,0 +1,78 @@
package com.deepclone.lw.utils;
import java.util.regex.Pattern;
/**
* This class provides e-mail address validation and sanitation.
*
* @author tseeker
*/
public class EmailAddress
{
// RFC 2822 2.2.2 Structured Header Field Bodies
private static final String wsp = "[ \\t]"; // space or tab
private static final String fwsp = wsp + "*";
// RFC 2822 3.2.1 Primitive tokens
private static final String dquote = "\\\"";
// ASCII Control characters excluding white space:
private static final String noWsCtl = "\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F";
// all ASCII characters except CR and LF:
private static final String asciiText = "[\\x01-\\x09\\x0B\\x0C\\x0E-\\x7F]";
// RFC 2822 3.2.2 Quoted characters:
// single backslash followed by a text char
private static final String quotedPair = "(\\\\" + asciiText + ")";
// RFC 2822 3.2.4 Atom:
private static final String atext = "[a-zA-Z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~]";
private static final String dotAtomText = atext + "+" + "(" + "\\." + atext + "+)*";
private static final String dotAtom = fwsp + "(" + dotAtomText + ")" + fwsp;
// RFC 2822 3.2.5 Quoted strings:
// noWsCtl and the rest of ASCII except the doublequote and backslash characters:
private static final String qtext = "[" + noWsCtl + "\\x21\\x23-\\x5B\\x5D-\\x7E]";
private static final String qcontent = "(" + qtext + "|" + quotedPair + ")";
private static final String quotedString = dquote + "(" + fwsp + qcontent + ")*" + fwsp + dquote;
// RFC 1035 tokens for domain names:
private static final String letter = "[a-zA-Z]";
private static final String letDig = "[a-zA-Z0-9]";
private static final String letDigHyp = "[a-zA-Z0-9-]";
private static final String rfcLabel = letDig + "(" + letDigHyp + "{0,61}" + letDig + ")?";
private static final String domain = rfcLabel + "(\\." + rfcLabel + ")*\\." + letter + "{2,6}";
private static final String localPart = "((" + dotAtom + ")|(" + quotedString + "))";
private static final String addrSpec = localPart + "@" + domain;
/** The pattern used to validate email addresses */
public static final Pattern VALID_PATTERN = Pattern.compile( addrSpec );
/** The sanitised version of the original e-mail address */
private String cleanAddress;
/** Creates a sanitiser/validator from a string */
public EmailAddress( String address )
{
this.cleanAddress = address.toLowerCase( ).trim( );
}
/** @return the sanitised version of the address */
public String getAddress( )
{
return this.cleanAddress;
}
/** @return whether the specified address is valid or not */
public boolean isValid( )
{
return VALID_PATTERN.matcher( this.cleanAddress ).matches( );
}
}

View file

@ -0,0 +1,110 @@
package com.deepclone.lw.utils;
import java.util.regex.Pattern;
/**
* This class provide password-related services, such as strength evaluation and hashing.
*
* @author tseeker
*/
public class Password
{
/** The various regular expressions used to evaluate a password's strength */
private static final String regexps[] = {
"[a-z]" , "[A-Z]" , "\\d" , ".*\\d.+\\d.+\\d" , "[^a-zA-Z\\d]" , "[^a-zA-Z\\d].*[^a-zA-Z\\d]" ,
"([a-zA-Z].*\\d)|(\\d.*[a-zA-Z])" , "([a-z].*[A-Z])|([A-Z].*[a-z])" ,
"([^a-zA-Z\\d][a-zA-Z\\d])|([a-zA-Z\\d][^a-zA-Z\\d])"
};
/** The scores associated with each regular expression */
private static final int reScores[] = {
1 , 5 , 5 , 5 , 5 , 5 , 2 , 2 , 2
};
/** Compiled regular expressions for strength evaluation */
private static Pattern patterns[] = null;
/**
* This method makes sure that strength evaluation RE's have been compiled and can be used.
*/
private static void initPatterns( )
{
synchronized ( Password.class ) {
if ( Password.patterns != null ) {
return;
}
Password.patterns = new Pattern[ Password.regexps.length ];
for ( int i = 0 ; i < Password.regexps.length ; i++ ) {
Password.patterns[ i ] = Pattern.compile( Password.regexps[ i ] );
}
}
}
/** The original password string */
private String password;
/** Initialise the instance with a specific password string */
public Password( String password )
{
this.password = password;
}
/** @return the password's strength */
public int getStrength( )
{
return this.getLengthStrength( ) + this.getPatternStrength( );
}
/** @return the password's SHA-1 hash (hex encoded) */
public String getSha1( )
{
return DigestHelper.digest( "sha-1" , this.password );
}
/** @return the password's MD5 hash (hex encoded) */
public String getMd5( )
{
return DigestHelper.digest( "md5" , this.password );
}
/** @return the password's strength based on regular expressions */
private int getPatternStrength( )
{
int strength = 0;
Password.initPatterns( );
for ( int i = 0 ; i < Password.regexps.length ; i++ ) {
if ( Password.patterns[ i ].matcher( this.password ).find( ) ) {
strength += Password.reScores[ i ];
} else {
}
}
return strength;
}
/** @return the password's strength based on length */
private int getLengthStrength( )
{
int len = this.password.length( );
if ( len < 5 ) {
return 3;
} else if ( len < 8 ) {
return 6;
} else if ( len < 16 ) {
return 12;
}
return 18;
}
}

View file

@ -0,0 +1,95 @@
package com.deepclone.lw.utils;
import java.util.HashSet;
import java.util.Set;
/**
* This class implements a random string generator, for use by stuff like validation tokens, planet
* names, etc. It can be used either directly or as a bean.
*
* @author tseeker
*/
public class RandomStringGenerator
{
/** The set of characters that can be used */
private Character[] characters = {
'0' , '1'
};
/** The length of the stings to generate */
private int length = 2;
/**
* Updates the instance's character set.
*
* @param characters
* a string that contains the new set of characters.
* @throws IllegalArgumentException
* if there are less than 2 characters in the set
*/
public void setCharacterSet( String characters )
throws IllegalArgumentException
{
Set< Character > chSet = new HashSet< Character >( );
for ( Character c : characters.toCharArray( ) ) {
chSet.add( c );
}
if ( chSet.size( ) < 2 ) {
throw new IllegalArgumentException( );
}
this.characters = chSet.toArray( new Character[ 1 ] );
}
/**
* Sets the instance's string length.
*
* @param length
* the new string length
* @throws IllegalArgumentException
* if the specified length is lower than 2
*/
public void setLength( int length )
throws IllegalArgumentException
{
if ( length < 2 ) {
throw new IllegalArgumentException( );
}
this.length = length;
}
/**
* Generates a random string. The string will have a fixed length and contain only characters
* from the generator's character set.
*
* @return the generated string
*/
public String generate( )
{
String str = "";
for ( int i = 0 ; i < this.length ; i++ ) {
str += this.getRandomChar( );
}
return str;
}
/**
* @return a random character from the character set.
*/
private Character getRandomChar( )
{
Double d = Math.random( );
d *= this.characters.length;
return this.characters[ (int) Math.floor( d ) ];
}
}

View file

@ -0,0 +1,54 @@
package com.deepclone.lw.utils;
import java.sql.Types;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
public class StoredProc
{
private SimpleJdbcCall storedProc;
public StoredProc( DataSource dataSource , String schema , String function )
{
this.storedProc = new SimpleJdbcCall( dataSource );
this.storedProc.withCatalogName( schema ).withFunctionName( function );
this.storedProc.withoutProcedureColumnMetaDataAccess( );
}
public StoredProc addParameter( String name , int sqlType )
{
this.storedProc.addDeclaredParameter( new SqlParameter( name , sqlType ) );
return this;
}
public StoredProc addParameter( String name , String typeName )
{
this.storedProc.addDeclaredParameter( new SqlParameter( name , Types.OTHER , typeName ) );
return this;
}
public StoredProc addOutput( String name , int sqlType )
{
this.storedProc.addDeclaredParameter( new SqlOutParameter( name , sqlType ) );
return this;
}
public Map< String , Object > execute( Object... args )
{
return this.storedProc.execute( args );
}
}