import java.io.*;
import java.util.HashMap;
import java.util.Map;
import com.zimbra.cs.account.Provisioning;
import javax.servlet.http.*;
import javax.servlet.*;
public class UserRegistration extends HttpServlet {
private String errorPage;
public void init() {
ServletConfig config = getServletConfig();
errorPage = config.getInitParameter("errorPage");
}
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
String redirect = "";
boolean okFlag = true;
String fname = (String)getNullValue(req.getParameter("fname"));
String lname = (String)getNullValue(req.getParameter("lname"));
String login = (String)getNullValue(req.getParameter("login"));
String password = (String)getNullValue(req.getParameter("password")) ;
String password_conf = (String)getNullValue(req.getParameter("conf_passwo rd"));
String streetAddress = (String)getNullValue(req.getParameter("streetAddre ss"));
String phone = (String)getNullValue(req.getParameter("phone"));
String zip = (String)getNullValue(req.getParameter("zip"));
String state = (String)getNullValue(req.getParameter("state"));
String country = (String)getNullValue(req.getParameter("country"));
String city = (String)getNullValue(req.getParameter("city"));
PrintWriter out = res.getWriter();
//The reson it is broken down like this is if login is null statement (login.length() == 0) will
//throw a NUll pointer
if(login == null){
redirect = "errorMsg: Login field cannot be empty";
okFlag = false;
}else if(login.length() == 0){
redirect = "errorMsg: Login field cannot be empty";
okFlag = false;
}
if (password == null){
redirect = "errorMsg: Password must be at least 6 characters long";
okFlag = false;
}else if(password.length() < 6){
redirect = "errorMsg: Password must be at least 6 characters long";
okFlag = false;
}else{
if(!password.equals(password_conf)){
redirect = "errorMsg: Passwords do not match";
okFlag = false;
}
}
if(okFlag){
Map attrs = new HashMap();
attrs.put("zimbraMailHost", "oaxy.com");
attrs.put("cn", fname + " " + lname);
attrs.put("displayName", fname + " " + lname);
attrs.put("gn", fname);
attrs.put("sn", lname);
attrs.put("telephoneNumber", phone);
attrs.put("street", streetAddress);
attrs.put("l", city);
attrs.put("st", state);
attrs.put("co", country);
attrs.put("postalCode", zip);
try{
Provisioning.getInstance().createAccount(login + "@oaxy.com", password, attrs);
redirect = login;
}
catch(Exception e){
redirect = "errorMsg: Error adding account: " + e.getMessage();
}
}
out.println(redirect);
}
public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
doGet(req, res);
}
private Object getNullValue(Object o){
if(o == null){
return null;
}
String tmp = (String)o;
if (tmp == ""){
return null;
}else if(tmp.length() > 0){
return tmp;
}
return o;
}
}