getLogSession().log(admin, admin.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Initialize ExternalCAService.",fe);
getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Initialize ExternalCAService.",fe);
    public void receiveResponse(Admin admin, int caid, IResponseMessage responsemessage) throws CADoesntExistsException, AuthorizationDeniedException, CertPathValidatorException, CATokenOfflineException{    	// check authorization    	Certificate cacert = null;    	// Check authorization    	try{    		getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");    	}catch(AuthorizationDeniedException e){    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Not authorized to recieve certificate responce for CA",e);    		throw new AuthorizationDeniedException("Not authorized to recieve certificate responce for CA with caid = " + caid);    	}    	// Get CA info.    	CADataLocal cadata = null;    	try{    		cadata = this.cadatahome.findByPrimaryKey(new Integer(caid));    		CA ca = cadata.getCA();    		try{    			if(responsemessage instanceof X509ResponseMessage){    				cacert = ((X509ResponseMessage) responsemessage).getCertificate();    			}else{    				getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util. Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: illegal response message.");    				throw new EJBException(new EjbcaException("Error: illegal response message."));    			}    			// if issuer is insystem CA or selfsigned, then generate new certificate.    			if(ca.getSignedBy() == CAInfo.SIGNEDBYEXTERNALCA){    				// check the validity of the certificate chain.    				// Check that DN is the equals the request.    				if(!CertTools.getSubjectDN((X509Certificate) cacert).equals(CertTools.stringToBCDNString(ca.getSubjectDN()))){    					getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: Subject DN of recieved certificate doesn't match request");    					throw new EJBException(new EjbcaException("Error: Subject DN of recieved certificate doesn't match request"));    				}    				ArrayList cachain = new ArrayList();    				cachain.add(cacert);    				cachain.addAll(ca.getRequestCertificateChain());    				ca.setCertificateChain(createCertChain(cachain));    				// Set statuses.    				cadata.setStatus(SecConst.CA_ACTIVE);    				// Publish CA Cert    				int certtype = CertificateDataBean.CERTTYPE_SUBCA;    		           if(ca.getSignedBy() == CAInfo.SELFSIGNED)    		          	  certtype = CertificateDataBean.CERTTYPE_ROOTCA;    		        ArrayList cacertcol = new ArrayList();    		        cacertcol.add(cacert);    				getSignSession().publishCACertificate(admin, cacertcol, ca.getCRLPublishers(), certtype);    				if(ca instanceof X509CA){    					cadata.setExpireTime(((X509Certificate) cacert).getNotAfter().getTime());    				}    				if(cadata.getStatus() ==SecConst.CA_ACTIVE){    					// activate External CA Services    					Iterator iter = ca.getExternalCAServiceTypes().iterator();    					while(iter.hasNext()){    						int type = ((Integer) iter.next()).intValue();    						try{    							ca.initExternalService(type, ca);    							ArrayList ocspcertificate = new ArrayList();    							ocspcertificate.add(((OCSPCAServiceInfo) ca.getExtendedCAServiceInfo(OCSPCAService.TYPE)).getOCSPSignerCertificatePath().get(0));    							getSignSession().publishCACertificate(admin, ocspcertificate, ca.getCRLPublishers(), CertificateDataBean.CERTTYPE_ENDENTITY);    						}catch(CATokenOfflineException e){    							ca.setStatus(SecConst.CA_OFFLINE);    							getLogSession().log(admin, admin.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Initialize ExternalCAService.",e);    							throw e;    						}catch(Exception fe){    							getLogSession().log(admin, admin.getCAId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,"Couldn't Initialize ExternalCAService.",fe);    							throw new EJBException(fe);    						}    					}    					//  create initial CRL    					this.getCRLCreateSession().run(admin,ca.getSubjectDN());    				}    				cadata.setCA(ca);    			}else{    				// Cannot create certificate request for internal CA    				getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: can't recieve certificate responce for internal CA");    				throw new EJBException(new EjbcaException("Error: can't recieve certificate responce for internal CA"));    			}    		}catch(CATokenOfflineException e){    			ca.setStatus(SecConst.CA_OFFLINE);    			getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: can't recieve certificate responce for internal CA", e);    			throw e;    		}    	}catch(Exception e){    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: can't recieve certificate responce for internal CA", e);    		throw new EJBException(e);    	}    	getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CAEDITED,"Certificate responce recieved successfully");    } // recieveResponse