if(type == ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE){ extcacertificate.add(((CmsCAServiceInfo) ca.getExtendedCAServiceInfo(ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE)).getCertificatePath().get(0)); }
    public void receiveResponse(Admin admin, int caid, IResponseMessage responsemessage) throws AuthorizationDeniedException, CertPathValidatorException, EjbcaException{    	// check authorization    	Certificate cacert = null;    	// Check authorization    	try{    		getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");    	}catch(AuthorizationDeniedException e){    		String msg = intres.getLocalizedMessage("caadmin.notauthorizedtocertresp", Integer.valueOf(caid));            	    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,msg,e);    		throw new AuthorizationDeniedException(msg);    	}    	// 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{    	    		String msg = intres.getLocalizedMessage("caadmin.errorcertrespillegalmsg");            	    				getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util. Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg);    				throw new EjbcaException(msg);    			}    			// 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()))){        	    		String msg = intres.getLocalizedMessage("caadmin.errorcertrespwrongdn", CertTools.getSubjectDN((X509Certificate) cacert), ca.getSubjectDN());            	    					getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg);    					throw new EjbcaException(msg);    				}    				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());    				}    				// activate External CA Services    				Iterator iter = ca.getExternalCAServiceTypes().iterator();    				while(iter.hasNext()){    				    int type = ((Integer) iter.next()).intValue();    				    try{    				        ca.initExternalService(type, ca);    				        ArrayList extcacertificate = new ArrayList();    				        if(type == ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE){    				        	extcacertificate.add(((OCSPCAServiceInfo) ca.getExtendedCAServiceInfo(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE)).getOCSPSignerCertificatePath().get(0));    				        }    				        if(type == ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE){    				        	extcacertificate.add(((XKMSCAServiceInfo) ca.getExtendedCAServiceInfo(ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE)).getXKMSSignerCertificatePath().get(0));    				        }    				        getSignSession().publishCACertificate(admin, extcacertificate, ca.getCRLPublishers(), CertificateDataBean.CERTTYPE_ENDENTITY);    				    }catch(CATokenOfflineException e){            	    		String msg = intres.getLocalizedMessage("caadmin.errorcreatecaservice", Integer.valueOf(caid));            	    				        getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,msg,e);    				        throw e;    				    }catch(Exception fe){            	    		String msg = intres.getLocalizedMessage("caadmin.errorcreatecaservice", Integer.valueOf(caid));            	    				        getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CACREATED,msg,fe);    				        throw new EJBException(fe);    				    }    				}                    // Save CA    				cadata.setCA(ca);                    //  create initial CRL                    this.getCRLCreateSession().run(admin,ca.getSubjectDN());    			}else{    	    		String msg = intres.getLocalizedMessage("caadmin.errorcreatecaservice", Integer.valueOf(caid));            	    				// Cannot create certificate request for internal CA    				getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg);    				throw new EjbcaException(msg);    			}    		}catch(CATokenOfflineException e){	    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            	    			getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);    			throw e;    		} catch (CertificateEncodingException e) {	    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            	        		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);        		throw new EjbcaException(e.getMessage());			} catch (CertificateException e) {	    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            		    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);	    		throw new EjbcaException(e.getMessage());			} catch (IOException e) {	    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            		    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);	    		throw new EjbcaException(e.getMessage());			}    	}catch(FinderException e){    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            	    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);    		throw new EjbcaException(e.getMessage());    	} catch (UnsupportedEncodingException e) {    		String msg = intres.getLocalizedMessage("caadmin.errorcertresp", Integer.valueOf(caid));            	    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,msg, e);    		throw new EjbcaException(e.getMessage());		}		String msg = intres.getLocalizedMessage("caadmin.certrespreceived", Integer.valueOf(caid));            	    	getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CAEDITED,msg);    } // recieveResponse