/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.servlets;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.scotlandyard.engine.GameException;
import org.scotlandyard.engine.User;
import org.scotlandyard.engine.json.JsonContainerAdapter;
import org.scotlandyard.impl.engine.GameEngine;
import org.scotlandyard.impl.engine.UserImpl;
/**
* TODO add a description for this class
*
*
* @author Hussain Al-Mutawa
* @version 2.0
* @since Sun Sep 17, 2011
*/
public class Login extends AbstractServlet implements IRequestProcessor{
private /*transient*/ String email ;
private /*transient*/ String name ;
private /*transient*/ User user;
//TODO add documentation about the action performed by getting the output of this servlet
@Override
public Object getOutput(
final HttpServletRequest request,
final GameEngine engine) throws GameException {
user = new UserImpl(name, email);
engine.getUsers().put(getSessionId(), user);
return (new JsonContainerAdapter("done").toJson());
}
@Override
public void validateRequest(
final HttpServletRequest request,
final GameEngine engine) throws GameException {
email = request.getParameter("email");
name = request.getParameter("name");
if(email==null || "".equals(email.trim())){
throw new GameException("email is not provided");
}
if(name==null || "".equals(name.trim())){
throw new GameException("name is not provided");
}
user = engine.getUsers().get(getSessionId());
if(user!=null){
throw new GameException("user name is not null");
}
user = engine.getUser(email);
if(user!=null){
throw new GameException("this email is already in use now, try to login by another email");
}
}
}