cat.debug("Received IE request:"+new String(reqBytes));
log.debug("Received IE request:"+new String(reqBytes));
  public void doPost(HttpServletRequest request, HttpServletResponse response)    throws IOException, ServletException  {    ServletDebug debug = new ServletDebug(request, response);    ISignSessionRemote signsession = null;    ICertificateStoreSessionRemote storesession = null;    IUserAdminSessionRemote useradminsession = null;    IRaAdminSessionRemote raadminsession = null;    try {        useradminsession = useradminsessionhome.create();        raadminsession = raadminsessionhome.create();        signsession = signsessionhome.create();        storesession = storesessionhome.create();    } catch (CreateException e) {      throw new ServletException(e);    }     Admin admin = new Admin(Admin.TYPE_PUBLIC_WEB_USER, request.getRemoteAddr());     RequestHelper helper = new RequestHelper(admin, debug);      String dn = null;      dn = request.getParameter("user");      byte[] reqBytes = null;      int type = 0;      if (request.getParameter("keygen") != null) {          reqBytes=request.getParameter("keygen").getBytes();          cat.debug("Received NS request:"+new String(reqBytes));          if (reqBytes != null) {              type = 1;          }      } else if (request.getParameter("pkcs10req") != null) {          // if not netscape, check if it's IE          reqBytes=request.getParameter("pkcs10req").getBytes();          cat.debug("Received IE request:"+new String(reqBytes));          if (reqBytes != null) {              type = 2;          }      }    if (reqBytes == null) {      // abort here, no request received      throw new ServletException("A certification request must be provided!");    }    String username = request.getParameter("username");    if (username == null || username.trim().length() == 0) {        username = CertTools.getPartFromDN(dn, "CN");    }    username = username + "("+(new Date()).toString()+")";    // need null check here?    // Before doing anything else, check if the user name is unique and ok.    boolean check = checkUsername(admin,username, useradminsession);    if (check == false) {        String msg = "User '"+username+"' already exist.";        cat.error(msg);        debug.printMessage(msg);        debug.printDebugInfo();        return;    }    String includeEmail = request.getParameter("includeemail");    cat.debug("includeEmail="+includeEmail);    UserView newuser = new UserView();    newuser.setUsername(username);    newuser.setSubjectDN(dn);    newuser.setTokenType(SecConst.TOKEN_SOFT_BROWSERGEN);    newuser.setAdministrator(false);    newuser.setKeyRecoverable(false);    String email = request.getParameter("email");    if (email == null) email = CertTools.getPartFromDN(dn, "EMAILADDRESS");    if (email != null) {      newuser.setEmail(email);      if (includeEmail != null) {          newuser.setSubjectAltName("email="+email);      }    }    String tmp = null;    int eProfileId = SecConst.EMPTY_ENDENTITYPROFILE;    if ((tmp=request.getParameter("entityprofile")) != null) {        eProfileId = raadminsession.getEndEntityProfileId(admin, request.getParameter("entityprofile"));        if (eProfileId == 0) {            throw new ServletException("No such end entity profile: " + tmp);        }    }    // TODO: check that we're authorized to use the profile    newuser.setEndEntityProfileId(eProfileId);    int cProfileId = SecConst.CERTPROFILE_FIXED_ENDUSER;    if ((tmp=request.getParameter("certificateprofile")) != null) {        cProfileId = storesession.getCertificateProfileId(admin, request.getParameter("certificateprofile"));        if (cProfileId == 0) {            throw new ServletException("No such certificate profile: " + tmp);        }    }    // TODO: check that we're authorized to use the profile    newuser.setCertificateProfileId(cProfileId);    String password = request.getParameter("password");    if (password == null) password = "demo";    newuser.setPassword(password);    newuser.setClearTextPassword(false);    try {        useradminsession.addUser(admin, newuser.getUsername(), newuser.getPassword(), newuser.getSubjectDN(), newuser.getSubjectAltName()                               ,newuser.getEmail(), newuser.getClearTextPassword(), newuser.getEndEntityProfileId(),                                newuser.getCertificateProfileId(), newuser.getAdministrator(),                                newuser.getKeyRecoverable(), newuser.getTokenType(), newuser.getHardTokenIssuerId());    } catch (Exception e) {      throw new ServletException("Error adding user: ", e);    }    byte[] pkcs7;    try {        if (type == 1) {              byte[] certs = helper.nsCertRequest(signsession, reqBytes, username, password);              RequestHelper.sendNewCertToNSClient(certs, response);        }        if (type == 2) {              byte[] b64cert=helper.pkcs10CertRequest(signsession, reqBytes, username, password);              debug.ieCertFix(b64cert);              RequestHelper.sendNewCertToIEClient(b64cert, response.getOutputStream(), getServletContext(), getInitParameter("responseTemplate"));        }    //} catch (java.security.cert.CertificateEncodingException e) {    //  throw new ServletException(e);    } catch (ObjectNotFoundException e) {      // User not found      cat.error(e);      throw new ServletException(e);    } catch (AuthStatusException e) {      // Wrong user status, shouldn't really happen.  The user needs to have      // status of NEW, FAILED or INPROCESS.      cat.error(e);      throw new ServletException(e);    } catch (AuthLoginException e) {      // Wrong username or password, hmm... wasn't the wrong username caught      // in the objectnotfoundexception above... and this shouldn't happen.      cat.error(e);      throw new ServletException(e);    } catch (IllegalKeyException e) {      // Malformed key (?)      cat.error(e);      throw new ServletException(e);    } catch (SignRequestException e) {      // Invalid request      cat.error(e);      throw new ServletException(e);    } catch (SignRequestSignatureException e) {      // Invalid signature in certificate request      cat.error(e);      throw new ServletException(e);    } catch (Exception e) {        cat.error(e);        throw new ServletException(e);    }  }