1 13 14 package org.ejbca.core.ejb.approval; 15 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.io.ObjectOutputStream ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Date ; 22 import java.util.Iterator ; 23 24 import javax.ejb.CreateException ; 25 import javax.ejb.EJBException ; 26 27 import org.apache.log4j.Logger; 28 import org.ejbca.core.ejb.BaseEntityBean; 29 import org.ejbca.core.model.approval.Approval; 30 import org.ejbca.core.model.approval.ApprovalDataUtil; 31 import org.ejbca.core.model.approval.ApprovalDataVO; 32 import org.ejbca.core.model.approval.ApprovalException; 33 import org.ejbca.core.model.approval.ApprovalRequest; 34 import org.ejbca.core.model.approval.ApprovalRequestExecutionException; 35 import org.ejbca.core.model.approval.ApprovalRequestExpiredException; 36 import org.ejbca.util.Base64; 37 import org.ejbca.util.CertTools; 38 39 117 public abstract class ApprovalDataBean extends BaseEntityBean { 118 119 private static final Logger log = Logger.getLogger(ApprovalDataBean.class); 120 121 122 129 public abstract Integer getId(); 130 131 135 public abstract void setId(Integer id); 136 137 144 public abstract int getApprovalid(); 145 146 151 public abstract void setApprovalid(int approvalid); 152 153 154 160 public abstract int getApprovaltype(); 161 162 167 public abstract void setApprovaltype(int approvaltype); 168 169 170 177 public abstract int getEndentityprofileid(); 178 179 180 185 public abstract void setEndentityprofileid(int endentityprofileid); 186 187 194 public abstract int getCaid(); 195 196 197 202 public abstract void setCaid(int caid); 203 204 209 public abstract String getReqadmincertissuerdn(); 210 211 215 public abstract void setReqadmincertissuerdn(String reqadmincertissuerdn); 216 217 222 public abstract String getReqadmincertsn(); 223 224 228 public abstract void setReqadmincertsn(String reqadmincertsn); 229 230 237 public abstract int getStatus(); 238 239 244 public abstract void setStatus(int status); 245 246 251 public abstract String getApprovaldata(); 252 253 256 public abstract void setApprovaldata(String approvaldata); 257 258 263 public abstract String getRequestdata(); 264 265 268 public abstract void setRequestdata(String requestdata); 269 270 275 public abstract long getRequestdate(); 276 277 281 public abstract void setRequestdate(long requestdate); 282 283 289 public abstract long getExpiredate(); 290 291 296 public abstract void setExpiredate(long expiredate); 297 298 303 public abstract int getRemainingapprovals(); 304 305 306 310 public abstract void setRemainingapprovals(int remainingapprovals); 311 312 313 314 private Collection getApprovals() { 315 return ApprovalDataUtil.getApprovals(getApprovaldata()); 316 } 317 318 323 private void setApprovals(Collection approvals){ 324 try{ 325 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 326 ObjectOutputStream oos = new ObjectOutputStream (baos); 327 328 int size = approvals.size(); 329 oos.writeInt(size); 330 Iterator iter = approvals.iterator(); 331 while(iter.hasNext()){ 332 Approval next = (Approval) iter.next(); 333 oos.writeObject(next); 334 } 335 oos.flush(); 336 337 setApprovaldata(new String (Base64.encode(baos.toByteArray(),false))); 338 } catch (IOException e) { 339 log.error("Error building approvals.",e); 340 throw new EJBException (e); 341 } 342 } 343 344 private ApprovalRequest getApprovalRequest() { 345 return ApprovalDataUtil.getApprovalRequest(getRequestdata()); 346 } 347 348 private void setApprovalRequest(ApprovalRequest approvalRequest){ 349 try{ 350 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 351 ObjectOutputStream oos = new ObjectOutputStream (baos); 352 oos.writeObject(approvalRequest); 353 oos.flush(); 354 setRequestdata(new String (Base64.encode(baos.toByteArray(),false))); 355 }catch(IOException e){ 356 log.error("Error building approval request.",e); 357 throw new EJBException (e); 358 } 359 } 360 361 private Date getRequestDate(){ 362 return new Date (getRequestdate()); 363 } 364 365 private void setRequestDate(Date requestDate){ 366 setRequestdate(requestDate.getTime()); 367 } 368 369 private Date getExpireDate(){ 370 return new Date (getExpiredate()); 371 } 372 373 377 public void setExpireDate(Date expireDate){ 378 setExpiredate(expireDate.getTime()); 379 } 380 381 386 private boolean haveRequestOrApprovalExpired(){ 387 Date currentDate = new Date (); 388 boolean retval = false; 389 if(currentDate.after(getExpireDate())){ 390 if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL || 391 getStatus() == ApprovalDataVO.STATUS_APPROVED || 392 getStatus() == ApprovalDataVO.STATUS_REJECTED){ 393 setStatus(ApprovalDataVO.STATUS_EXPIRED); 394 } 395 retval=true; 396 } 397 398 return retval; 399 400 } 401 402 407 public ApprovalDataVO getApprovalDataVO() { 408 haveRequestOrApprovalExpired(); 409 410 return new ApprovalDataVO(getId().intValue(),getApprovalid(),getApprovaltype(), 411 getEndentityprofileid(),getCaid(),getReqadmincertissuerdn(), 412 getReqadmincertsn(), getStatus(),getApprovals(), getApprovalRequest(), 413 getRequestDate(),getExpireDate(),getRemainingapprovals()); 414 415 } 416 417 427 public void approve(Approval approval) throws ApprovalRequestExpiredException, ApprovalRequestExecutionException, ApprovalException { 428 if(haveRequestOrApprovalExpired()){ 429 throw new ApprovalRequestExpiredException(); 430 } 431 432 if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ 433 throw new ApprovalException("Wrong status of approval request."); 434 } 435 436 int numberofapprovalsleft = getRemainingapprovals() -1; 437 if(numberofapprovalsleft < 0){ 438 throw new ApprovalException("Error already enough approvals have been done on this request."); 439 } 440 441 setRemainingapprovals(numberofapprovalsleft); 442 Collection approvals = getApprovals(); 443 approvals.add(approval); 444 setApprovals(approvals); 445 446 if(numberofapprovalsleft == 0){ 447 ApprovalRequest approvalRequest = getApprovalRequest(); 448 if(approvalRequest.isExecutable()){ 449 try{ 450 approvalRequest.execute(); 451 setStatus(ApprovalDataVO.STATUS_EXECUTED); 452 } catch(ApprovalRequestExecutionException e){ 453 setStatus(ApprovalDataVO.STATUS_EXECUTIONFAILED); 454 throw e; 455 } 456 setStatus(ApprovalDataVO.STATUS_EXECUTED); 457 setExpireDate(new Date ()); 458 }else{ 459 setStatus(ApprovalDataVO.STATUS_APPROVED); 460 setExpiredate((new Date ()).getTime() + approvalRequest.getApprovalValidity()); 461 } 462 } 463 464 } 465 466 476 public void reject(Approval approval) throws ApprovalRequestExpiredException, ApprovalException { 477 if(haveRequestOrApprovalExpired()){ 478 throw new ApprovalRequestExpiredException(); 479 } 480 481 if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ 482 throw new ApprovalException("Wrong status of approval request."); 483 } 484 485 int numberofapprovalsleft = getRemainingapprovals() -1; 486 if(numberofapprovalsleft < 0){ 487 throw new ApprovalException("Error already enough approvals have been done on this request."); 488 } 489 490 setRemainingapprovals(0); 491 Collection approvals = getApprovals(); 492 approvals.add(approval); 493 setApprovals(approvals); 494 495 if(getApprovalRequest().isExecutable()){ 496 setStatus(ApprovalDataVO.STATUS_EXECUTIONDENIED); 497 setExpireDate(new Date ()); 498 }else{ 499 setStatus(ApprovalDataVO.STATUS_REJECTED); 500 setExpiredate((new Date ()).getTime() + getApprovalRequest().getApprovalValidity()); 501 } 502 503 504 } 505 506 514 public int isApproved() throws ApprovalRequestExpiredException { 515 if(haveRequestOrApprovalExpired()){ 516 if(getStatus() != ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED && 517 getStatus() != ApprovalDataVO.STATUS_EXECUTED && 518 getStatus() != ApprovalDataVO.STATUS_EXECUTIONDENIED && 519 getStatus() != ApprovalDataVO.STATUS_EXECUTIONFAILED){ 520 setStatus(ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED); 521 throw new ApprovalRequestExpiredException(); 522 } 523 return ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED; 524 525 } 526 527 if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ 528 return getRemainingapprovals(); 529 } 530 531 return getStatus(); 532 533 } 534 535 536 542 public void ejbPassivate() { 543 } 544 545 546 552 553 554 public Integer ejbCreate(Integer id, ApprovalRequest approvalRequest) throws CreateException { 555 setId(id); 556 setApprovalid(approvalRequest.generateApprovalId()); 557 setApprovaltype(approvalRequest.getApprovalType()); 558 setEndentityprofileid(approvalRequest.getEndEntityProfileId()); 559 setCaid(approvalRequest.getCAId()); 560 561 if(approvalRequest.getRequestAdminCert() != null){ 562 setReqadmincertissuerdn(CertTools.getIssuerDN(approvalRequest.getRequestAdminCert())); 563 setReqadmincertsn(approvalRequest.getRequestAdminCert().getSerialNumber().toString(16)); 564 } 565 setStatus(ApprovalDataVO.STATUS_WAITINGFORAPPROVAL); 566 setApprovals(new ArrayList ()); 567 setApprovalRequest(approvalRequest); 568 setRequestDate(new Date ()); 569 setExpiredate((new Date ()).getTime() + approvalRequest.getRequestValidity()); 570 setRemainingapprovals(approvalRequest.getNumOfRequiredApprovals()); 571 572 573 log.debug("Created approval with id " + id); 574 return id; 575 } 576 577 public void ejbPostCreate(Integer id, ApprovalRequest approvalRequest) { 578 } 580 } 581 | Popular Tags |