cat.debug("Created certificate (PKCS7) for " + username);
log.debug("Created certificate (PKCS7) for " + username);
  public void doPost(HttpServletRequest request, HttpServletResponse response)    throws IOException, ServletException  {    // Check if authorized    EjbcaWebBean ejbcawebbean= getEjbcaWebBean(request);    try{      ejbcawebbean.initialize(request, "/ra_functionallity/create_end_entity");    } catch(Exception e){       throw new java.io.IOException("Authorization Denied");    }    X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");    if (certs == null) {        throw new ServletException("This servlet requires certificate authentication!");    }    Admin admin = new Admin(certs[0]);    byte[] buffer = pkcs10Bytes(request.getParameter("pkcs10req"));    if (buffer == null) {      // TODO: abort here, no PKCS#10 received    }    RAInterfaceBean rabean = getRaBean(request);    // Decompose the PKCS#10 request, and create the user.    PKCS10RequestMessage p10 = new PKCS10RequestMessage(buffer);    String dn = p10.getCertificationRequest().getCertificationRequestInfo().getSubject().toString();    String username = request.getParameter("username");    if (username == null || username.trim().length() == 0) {      username = dn;    }    // need null check here?    // Before doing anything else, check if the user name is unique and ok.    username = checkUsername(rabean, username);    UserView newuser = new UserView();    newuser.setUsername(username);    newuser.setSubjectDN(dn);    newuser.setTokenType(SecConst.TOKEN_SOFT_BROWSERGEN);    newuser.setAdministrator(false);    newuser.setKeyRecoverable(false);    String email = CertTools.getPartFromDN(dn, "E"); // BC says VeriSign    if (email == null) email = CertTools.getPartFromDN(dn, "EMAILADDRESS");    // TODO: get values from subject altname, lookup email as well?    // newuser.setSubjectAltName(...)    if (email != null) {      newuser.setEmail(email);    }    String tmp = null;    int eProfileId = SecConst.EMPTY_ENDENTITYPROFILE;    if ((tmp = request.getParameter("entityprofile")) != null) {      int reqId = rabean.getEndEntityProfileId(tmp);      if (reqId == 0) {        throw new ServletException("No such end entity profile: " + tmp);      } else {        eProfileId = reqId;      }    }    // 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) {      CAInterfaceBean cabean = getCaBean(request);      int reqId = cabean.getCertificateProfileId(tmp);      if (reqId == 0) {        throw new ServletException("No such certificate profile: " + tmp);      } else {        cProfileId = reqId;      }    }    // TODO: check that we're authorized to use the profile?    newuser.setCertificateProfileId(cProfileId);    // TODO: figure out if we can manage without a password.    String password = request.getParameter("password");    if (password == null) password = "";    newuser.setPassword(password);    newuser.setClearTextPassword(false);    try {      rabean.addUser(newuser);    } catch (Exception e) {      throw new ServletException("Error adding user: " + e.toString(), e);    }    ISignSessionRemote ss;    try {      ss = signhome.create();    } catch (CreateException e) {      throw new ServletException(e);    }    byte[] pkcs7;    try {      X509Certificate cert =        (X509Certificate) ss.createCertificate(admin, username, password, p10);      pkcs7 = ss.createPKCS7(admin, cert);    } catch (ObjectNotFoundException e) {      // User not found      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.      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.      throw new ServletException(e);    } catch (IllegalKeyException e) {      // Malformed key (?)      throw new ServletException(e);    } catch (SignRequestException e) {      // Invalid request      throw new ServletException(e);    } catch (SignRequestSignatureException e) {      // Invalid signature in certificate request      throw new ServletException(e);    }    cat.debug("Created certificate (PKCS7) for " + username);    sendNewB64Cert(Base64.encode(pkcs7), response);  }