package com.wesabe.grendel.resources.dto;

import java.util.Arrays;

/**
 * A data transfer object for a request to create a new user.
 * <p>
 * Example JSON:
 * <pre>
 * {
 *   "username": "Example User",
 *   "password": "snoopersneekrit"
 * }
 * </pre>
 * 
 * Both {@code username} and {@code password} properties are required.
 * 
 * @author coda
 *
 */
public class NewUserRequest implements Validatable {
	private String username;
	private char[] password;
	
	public char[] getPassword() {
		return password;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setPassword(char[] password) {
		this.password = Arrays.copyOf(password, password.length);
		Arrays.fill(password, '\0');
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public void sanitize() {
		Arrays.fill(password, '\0');
	}

	@Override
	public void validate() throws ValidationException {
		final ValidationException error = new ValidationException();
		
		if ((username == null) || username.isEmpty()) {
			error.missingRequiredProperty("username");
		}
		
		if ((password == null) || (password.length == 0)) {
			error.missingRequiredProperty("password");
		}
		
		if (error.hasReasons()) {
			throw error;
		}
	}
}

@@ -62,16 +62,115 @@
 rrays;%0A%0A
+import org.codehaus.jackson.annotate.JsonGetter;%0Aimport org.codehaus.jackson.annotate.JsonSetter;%0A%0A
 /**%0A * A
@@ -557,16 +557,41 @@
 word;%0A%09%0A
+%09@JsonGetter(%22password%22)%0A
 %09public 
@@ -633,24 +633,49 @@
 sword;%0A%09%7D%0A%09%0A
+%09@JsonGetter(%22username%22)%0A
 %09public Stri
@@ -709,32 +709,57 @@
  username;%0A%09%7D%0A%09%0A
+%09@JsonSetter(%22password%22)%0A
 %09public void set
@@ -876,26 +876,51 @@
 , '%5C0');%0A%09%7D%0A
-
 %09%0A
+%09@JsonSetter(%22username%22)%0A
 %09public void

Use Jackson annotations to tie down NewUserRequest properties.