package org.owasp.appsensor;

import java.io.Serializable;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * The standard User object. The base implementation assumes the username is 
 * provided by the client application. It is up to the client application to 
 * manage the username. If the username doesn't exist, the base implementation 
 * will attempt to fall back to the IP address as an identifier.
 * 
 * @see java.io.Serializable
 *
 * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/
 */
public class User implements Serializable {

	private static final long serialVersionUID = 5084152023446797592L;

	private String username;

	private String ipAddress;
	
	public User() {}
	
	public User(String username, String ipAddress) {
		setUsername(username);
		setIpAddress(ipAddress);
		
		//attempt fallback to IP address
		if (username == null) {
			setUsername(ipAddress);
		}
	}
	
	public String getUsername() {
		return username;
	}

	public User setUsername(String username) {
		this.username = username;
		
		//attempt fallback to IP address
		if (username == null) {
			setUsername(ipAddress);
		}
				
		return this;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public User setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
		return this;
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder(17,31).
				append(username).
				append(ipAddress).
				toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		
		User other = (User) obj;
		
		return new EqualsBuilder().
				append(username, other.getUsername()).
				append(ipAddress, other.getIpAddress()).
				isEquals();
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this).
				append("username", username).
				append("ipAddress", ipAddress).
			    toString();
	}
	
}

@@ -344,16 +344,24 @@
 cation. 
+%0A * %0A * 
 It is up
@@ -390,20 +390,16 @@
 tion to 
-%0A * 
 manage t
@@ -415,12 +415,13 @@
 me. 
-If t
+%0A * T
 he u
@@ -432,111 +432,196 @@
 ame 
-doesn't exist, the base implementation %0A * will attempt to fall back to the IP address as an identifier
+could be anything, an actual username, an IP address, %0A * or any other identifier desired. The core notion is that any desired %0A * correlation on the user is done by comparing the username
 .%0A *
@@ -872,37 +872,8 @@
 e;%0A%0A
-%09private String ipAddress;%0A%09%0A
 %09pub
@@ -920,26 +920,8 @@
 name
-, String ipAddress
 ) %7B%0A
@@ -949,130 +949,8 @@
 e);%0A
-%09%09setIpAddress(ipAddress);%0A%09%09%0A%09%09//attempt fallback to IP address%0A%09%09if (username == null) %7B%0A%09%09%09setUsername(ipAddress);%0A%09%09%7D%0A
 %09%7D%0A%09
@@ -1085,273 +1085,24 @@
 %09%0A%09%09
-//attempt fallback to IP address%0A%09%09if (username == null) %7B%0A%09%09%09setUsername(ipAddress);%0A%09%09%7D%0A%09%09%09%09%0A%09%09return this;%0A%09%7D%0A%0A%09public String getIpAddress() %7B%0A%09%09return ipAddress;%0A%09%7D%0A%0A%09public User setIpAddress(String ipAddress) %7B%0A%09%09this.ipAddress = ipAddress;%0A%09%09return this;%0A%09%7D%0A%09
+return this;%0A%09%7D%0A
 %0A%09@O
@@ -1197,31 +1197,8 @@
 e).%0A
-%09%09%09%09append(ipAddress).%0A
 %09%09%09%09
@@ -1498,53 +1498,8 @@
 )).%0A
-%09%09%09%09append(ipAddress, other.getIpAddress()).%0A
 %09%09%09%09
@@ -1628,44 +1628,8 @@
 e).%0A
-%09%09%09%09append(%22ipAddress%22, ipAddress).%0A
 %09%09%09 

Clean up user object to remove IP address - client application can use anything as the "username" - it's a generic identifier.