1 56 package org.opencrx.mail.servlet; 57 58 import java.io.IOException ; 59 import java.util.ArrayList ; 60 import java.util.Date ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import javax.activation.MimeType ; 65 import javax.activation.MimeTypeParseException ; 66 import javax.jmi.reflect.RefException; 67 import javax.mail.Message ; 68 import javax.naming.Context ; 69 import javax.naming.InitialContext ; 70 import javax.naming.NamingException ; 71 import javax.servlet.ServletConfig ; 72 import javax.servlet.ServletException ; 73 import javax.servlet.http.HttpServlet ; 74 import javax.servlet.http.HttpServletRequest ; 75 import javax.servlet.http.HttpServletResponse ; 76 77 import org.opencrx.kernel.account1.cci.EMailAddress; 78 import org.opencrx.kernel.account1.cci.EMailAddressFilter; 79 import org.opencrx.kernel.account1.cci.account1Package; 80 import org.opencrx.kernel.activity1.cci.ActivityCreator; 81 import org.opencrx.kernel.activity1.cci.ActivityCreatorFilter; 82 import org.opencrx.kernel.activity1.cci.ActivityProcess; 83 import org.opencrx.kernel.activity1.cci.ActivityProcessState; 84 import org.opencrx.kernel.activity1.cci.ActivityProcessTransition; 85 import org.opencrx.kernel.activity1.cci.ActivityTracker; 86 import org.opencrx.kernel.activity1.cci.ActivityType; 87 import org.opencrx.kernel.activity1.cci.EMail; 88 import org.opencrx.kernel.activity1.cci.EMailFilter; 89 import org.opencrx.kernel.activity1.cci.EMailRecipient; 90 import org.opencrx.kernel.activity1.cci.NewActivityParams; 91 import org.opencrx.kernel.activity1.cci.NewActivityResult; 92 import org.opencrx.kernel.activity1.cci.SetActualEndAction; 93 import org.opencrx.kernel.activity1.cci.SetActualStartAction; 94 import org.opencrx.kernel.activity1.cci.SetAssignedToAction; 95 import org.opencrx.kernel.activity1.cci.WfAction; 96 import org.opencrx.kernel.activity1.cci.activity1Package; 97 import org.opencrx.kernel.generic.SecurityKeys; 98 import org.opencrx.kernel.generic.cci.Media; 99 import org.opencrx.kernel.generic.cci.Note; 100 import org.opencrx.kernel.generic.cci.genericPackage; 101 import org.opencrx.kernel.workflow.servlet.WorkflowControllerServlet; 102 import org.opencrx.kernel.workflow1.cci.WfProcess; 103 import org.openmdx.application.log.AppLog; 104 import org.openmdx.base.accessor.generic.view.Manager_1; 105 import org.openmdx.base.accessor.jmi.cci.JmiServiceException; 106 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0; 107 import org.openmdx.base.accessor.jmi.spi.RefRootPackage_1; 108 import org.openmdx.base.exception.ServiceException; 109 import org.openmdx.compatibility.application.dataprovider.transport.ejb.cci.Dataprovider_1ConnectionFactoryImpl; 110 import org.openmdx.compatibility.base.dataprovider.cci.QualityOfService; 111 import org.openmdx.compatibility.base.dataprovider.cci.RequestCollection; 112 import org.openmdx.compatibility.base.dataprovider.cci.ServiceHeader; 113 import org.openmdx.compatibility.base.dataprovider.transport.adapter.Provider_1; 114 import org.openmdx.compatibility.base.dataprovider.transport.cci.Dataprovider_1_1Connection; 115 import org.openmdx.compatibility.base.dataprovider.transport.cci.Provider_1_0; 116 import org.openmdx.compatibility.base.dataprovider.transport.delegation.Connection_1; 117 import org.openmdx.compatibility.base.naming.Path; 118 import org.openmdx.compatibility.base.query.FilterOperators; 119 import org.openmdx.kernel.exception.BasicException; 120 import org.openmdx.kernel.id.UUIDs; 121 import org.openmdx.kernel.id.cci.UUIDGenerator; 122 import org.openmdx.model1.accessor.basic.spi.Model_1; 123 124 130 public class MailImporterServlet 131 extends HttpServlet { 132 133 137 public void init( 138 ServletConfig config 139 ) throws ServletException { 140 141 super.init(config); 142 143 int i = 0; 145 List modelPackages = new ArrayList (); 146 while(getInitParameter("modelPackage[" + i + "]") != null) { 147 modelPackages.add( 148 getInitParameter("modelPackage[" + i + "]") 149 ); 150 i++; 151 } 152 try { 153 new Model_1().addModels(modelPackages); 154 } 155 catch(Exception e) { 156 AppLog.warning("Can not initialize model repository", e); 157 } 158 159 Context initialContext; 160 try { 161 initialContext = new InitialContext (); 162 } 163 catch (NamingException e) { 164 throw new ServletException ("can not get the initial context", e); 165 } 166 try { 168 this.connectionData = Dataprovider_1ConnectionFactoryImpl.createGenericConnection( 169 initialContext.lookup("java:comp/env/ejb/data") 170 ); 171 } 172 catch(Exception e) { 173 throw new ServletException ("can not get connection to data provider", e); 174 } 175 } 176 177 187 private RefPackage_1_0 createDataPkg( 188 ServiceHeader header 189 ) throws ServiceException { 190 Provider_1_0 provider = new Provider_1( 191 new RequestCollection( 192 header, 193 this.connectionData 194 ), 195 false 196 ); 197 Manager_1 manager = new Manager_1( 198 new Connection_1( 199 provider, 200 false 201 ) 202 ); 203 RefPackage_1_0 rootPkg = new RefRootPackage_1(manager); 204 return rootPkg; 205 } 206 207 211 protected static org.opencrx.kernel.activity1.cci.activity1Package getActivityPkg( 212 RefPackage_1_0 rootPkg 213 ) { 214 return (org.opencrx.kernel.activity1.cci.activity1Package)rootPkg.refPackage( 215 org.opencrx.kernel.activity1.cci.activity1Package.class.getName() 216 ); 217 } 218 219 223 protected static org.opencrx.kernel.activity1.cci.Segment getActivitySegment( 224 RefPackage_1_0 rootPkg, 225 String providerName, 226 String segmentName 227 ) { 228 return (org.opencrx.kernel.activity1.cci.Segment) rootPkg.refObject( 229 "xri:@openmdx:org.opencrx.kernel.activity1/provider/" 230 + providerName + "/segment/" + segmentName 231 ); 232 } 233 234 238 protected static account1Package getAccountPkg( 239 RefPackage_1_0 rootPkg 240 ) { 241 return (account1Package) rootPkg.refPackage( 242 account1Package.class.getName() 243 ); 244 } 245 246 250 protected static org.opencrx.kernel.account1.cci.Segment getAccountSegment( 251 RefPackage_1_0 rootPkg, 252 String providerName, 253 String segmentName 254 ) { 255 return (org.opencrx.kernel.account1.cci.Segment) rootPkg.refObject( 256 "xri:@openmdx:org.opencrx.kernel.account1/provider/" 257 + providerName + "/segment/" + segmentName 258 ); 259 } 260 261 protected static genericPackage getGenericPkg( 263 RefPackage_1_0 rootPkg 264 ) { 265 return (org.opencrx.kernel.generic.cci.genericPackage) rootPkg.refPackage( 266 org.opencrx.kernel.generic.cci.genericPackage.class.getName() 267 ); 268 } 269 270 protected void initEMailCreator( 272 RefPackage_1_0 rootPkg, 273 String providerName, 274 String segmentName 275 ) { 276 org.opencrx.kernel.activity1.cci.activity1Package activityPkg = getActivityPkg( 277 rootPkg 278 ); 279 org.opencrx.kernel.activity1.cci.Segment activitySegment = getActivitySegment( 280 rootPkg, 281 providerName, 282 segmentName 283 ); 284 ActivityCreator emailCreator = null; 286 try { 287 emailCreator = activitySegment.getActivityCreator(DEFAULT_EMAIL_CREATOR_ID); 288 } 289 catch(JmiServiceException e) { 290 if(e.getExceptionCode() != BasicException.Code.NOT_FOUND) { 292 throw e; 293 } 294 try { 295 rootPkg.refBegin(); 297 emailCreator = activityPkg.getActivityCreatorClass().createActivityCreator(); 298 emailCreator.setName("Default E-Mail Creator"); 299 emailCreator.setPriority((short)0); 300 emailCreator.getOwningGroup().addAll( 301 activitySegment.getOwningGroup() 302 ); 303 activitySegment.addActivityCreator( 304 DEFAULT_EMAIL_CREATOR_ID, 305 emailCreator 306 ); 307 rootPkg.refCommit(); 308 309 rootPkg.refBegin(); 311 ActivityProcess emailProcess = activityPkg.getActivityProcessClass().createActivityProcess(); 312 emailProcess.setName("E-Mail Process"); 313 emailProcess.getOwningGroup().addAll( 314 activitySegment.getOwningGroup() 315 ); 316 activitySegment.addActivityProcess( 317 emailProcess 318 ); 319 ActivityProcessState newState = activityPkg.getActivityProcessStateClass().createActivityProcessState(); 321 newState.setName("New"); 322 newState.getOwningGroup().addAll( 323 activitySegment.getOwningGroup() 324 ); 325 emailProcess.addState(newState); 326 ActivityProcessState openState = activityPkg.getActivityProcessStateClass().createActivityProcessState(); 328 openState.setName("Open"); 329 openState.getOwningGroup().addAll( 330 activitySegment.getOwningGroup() 331 ); 332 emailProcess.addState(openState); 333 ActivityProcessState closedState = activityPkg.getActivityProcessStateClass().createActivityProcessState(); 335 closedState.setName("Closed"); 336 closedState.getOwningGroup().addAll( 337 activitySegment.getOwningGroup() 338 ); 339 emailProcess.addState(closedState); 340 rootPkg.refCommit(); 341 rootPkg.refBegin(); 343 emailProcess.setStartState(newState); 344 ActivityProcessTransition processTransition = activityPkg.getActivityProcessTransitionClass().createActivityProcessTransition(); 346 processTransition.setName("Assign"); 347 processTransition.setPrevState(newState); 348 processTransition.setNextState(openState); 349 processTransition.setNewActivityState((short)10); 350 processTransition.setNewPercentComplete(new Short ((short)20)); 351 processTransition.getOwningGroup().addAll( 352 activitySegment.getOwningGroup() 353 ); 354 emailProcess.addTransition(processTransition); 355 SetAssignedToAction setAssignedToAction = activityPkg.getSetAssignedToActionClass().createSetAssignedToAction(); 357 setAssignedToAction.setName("Set assignedTo"); 358 setAssignedToAction.setDescription("Set assignedTo to current user"); 359 setAssignedToAction.getOwningGroup().addAll( 360 activitySegment.getOwningGroup() 361 ); 362 processTransition.addAction(setAssignedToAction); 363 SetActualStartAction setActualStartAction = activityPkg.getSetActualStartActionClass().createSetActualStartAction(); 365 setActualStartAction.setName("Set actual start"); 366 setActualStartAction.setDescription("Set actual start on activity assignment"); 367 setActualStartAction.getOwningGroup().addAll( 368 activitySegment.getOwningGroup() 369 ); 370 processTransition.addAction(setActualStartAction); 371 processTransition = activityPkg.getActivityProcessTransitionClass().createActivityProcessTransition(); 373 processTransition.setName("Add Note"); 374 processTransition.setPrevState(openState); 375 processTransition.setNextState(openState); 376 processTransition.setNewActivityState((short)10); 377 processTransition.setNewPercentComplete(new Short ((short)50)); 378 processTransition.getOwningGroup().addAll( 379 activitySegment.getOwningGroup() 380 ); 381 emailProcess.addTransition(processTransition); 382 processTransition = activityPkg.getActivityProcessTransitionClass().createActivityProcessTransition(); 384 processTransition.setName("Export as mail attachment"); 385 processTransition.setPrevState(openState); 386 processTransition.setNextState(openState); 387 processTransition.setNewActivityState((short)10); 388 processTransition.setNewPercentComplete(new Short ((short)50)); 389 processTransition.getOwningGroup().addAll( 390 activitySegment.getOwningGroup() 391 ); 392 emailProcess.addTransition(processTransition); 393 WfAction wfAction = activityPkg.getWfActionClass().createWfAction(); 395 wfAction.setName("Export Mail"); 396 wfAction.setName("Export Mail as attachment to current user"); 397 wfAction.setWfProcess( 398 (WfProcess)rootPkg.refObject( 399 new Path("xri:@openmdx:org.opencrx.kernel.workflow1/provider/" + providerName + "/segment/" + segmentName + "/wfProcess/" + WorkflowControllerServlet.WORKFLOW_EXPORT_MAIL).toXri() 400 ) 401 ); 402 wfAction.getOwningGroup().addAll( 403 activitySegment.getOwningGroup() 404 ); 405 processTransition.addAction(wfAction); 406 processTransition = activityPkg.getActivityProcessTransitionClass().createActivityProcessTransition(); 408 processTransition.setName("Send as mail"); 409 processTransition.setPrevState(openState); 410 processTransition.setNextState(openState); 411 processTransition.setNewActivityState((short)10); 412 processTransition.setNewPercentComplete(new Short ((short)50)); 413 processTransition.getOwningGroup().addAll( 414 activitySegment.getOwningGroup() 415 ); 416 emailProcess.addTransition(processTransition); 417 wfAction = activityPkg.getWfActionClass().createWfAction(); 419 wfAction.setName("Send Mail"); 420 wfAction.setName("Send as mail"); 421 wfAction.setWfProcess( 422 (WfProcess)rootPkg.refObject( 423 new Path("xri:@openmdx:org.opencrx.kernel.workflow1/provider/" + providerName + "/segment/" + segmentName + "/wfProcess/" + WorkflowControllerServlet.WORKFLOW_SEND_MAIL).toXri() 424 ) 425 ); 426 wfAction.getOwningGroup().addAll( 427 activitySegment.getOwningGroup() 428 ); 429 processTransition.addAction(wfAction); 430 processTransition = activityPkg.getActivityProcessTransitionClass().createActivityProcessTransition(); 432 processTransition.setName("Close"); 433 processTransition.setPrevState(openState); 434 processTransition.setNextState(closedState); 435 processTransition.setNewActivityState((short)20); 436 processTransition.setNewPercentComplete(new Short ((short)100)); 437 processTransition.getOwningGroup().addAll( 438 activitySegment.getOwningGroup() 439 ); 440 emailProcess.addTransition(processTransition); 441 SetActualEndAction setActualEndAction = activityPkg.getSetActualEndActionClass().createSetActualEndAction(); 443 setActualEndAction.setName("Set actual end"); 444 setActualEndAction.setName("Set actual end to current dateTime"); 445 setActualEndAction.getOwningGroup().addAll( 446 activitySegment.getOwningGroup() 447 ); 448 processTransition.addAction(setActualEndAction); 449 rootPkg.refCommit(); 451 452 rootPkg.refBegin(); 454 ActivityType emailActivityType = activityPkg.getActivityTypeClass().createActivityType(); 455 emailActivityType.setName("E-Mails"); 456 emailActivityType.setActivityClass(ACTIVITY_CLASS_EMAIL); 457 emailActivityType.setControlledBy(emailProcess); 458 emailActivityType.getOwningGroup().addAll( 459 activitySegment.getOwningGroup() 460 ); 461 activitySegment.addActivityType(emailActivityType); 462 rootPkg.refCommit(); 463 464 rootPkg.refBegin(); 466 ActivityTracker emailGroup = activityPkg.getActivityTrackerClass().createActivityTracker(); 467 emailGroup.setName("E-Mails"); 468 emailGroup.getOwningGroup().addAll( 469 activitySegment.getOwningGroup() 470 ); 471 activitySegment.addActivityTracker(emailGroup); 472 rootPkg.refCommit(); 473 474 rootPkg.refBegin(); 476 emailCreator.getActivityGroup().add(emailGroup); 477 emailCreator.setActivityType(emailActivityType); 478 rootPkg.refCommit(); 479 480 } 481 catch(JmiServiceException e0) { 482 AppLog.info("Can not create default configuration", e0.getMessage()); 483 throw e0; 484 } 485 } 486 } 487 488 498 private List lookupEMailAddress( 499 RefPackage_1_0 rootPkg, 500 String providerName, 501 String segmentName, 502 String emailAddress 503 ) { 504 EMailAddressFilter filter = getAccountPkg(rootPkg).createEMailAddressFilter(); 505 org.opencrx.kernel.account1.cci.Segment accountSegment = 506 getAccountSegment( 507 rootPkg, 508 providerName, 509 segmentName 510 ); 511 filter.thereExistsIdentity( 512 FilterOperators.IS_LIKE, 513 new String []{ 514 accountSegment.refMofId() + "/account/:*/address/:*" 515 } 516 ); 517 filter.thereExistsEmailAddress( 518 FilterOperators.IS_IN, 519 new String []{emailAddress} 520 ); 521 return accountSegment.getExtent(filter); 522 } 523 524 535 private List lookupEMailActivity( 536 RefPackage_1_0 rootPkg, 537 String providerName, 538 String segmentName, 539 String externalLink 540 ) { 541 EMailFilter filter = getActivityPkg(rootPkg).createEMailFilter(); 542 org.opencrx.kernel.activity1.cci.Segment activitySegment = 543 getActivitySegment( 544 rootPkg, 545 providerName, 546 segmentName 547 ); 548 filter.thereExistsExternalLink( 549 FilterOperators.IS_IN, 550 new String []{ 551 externalLink 552 } 553 ); 554 return activitySegment.getActivity(filter); 555 } 556 557 563 private void addMedia( 564 SimpleMimeMessage mimeMsg, 565 RefPackage_1_0 rootPkg, 566 EMail emailActivity 567 ) { 568 if(mimeMsg.containsAttachments()) { 574 Iterator contentsIter = mimeMsg.getContents().iterator(); 575 boolean emailContainsMedia = emailActivity.getMedia().size() > 0; 576 while (contentsIter.hasNext()) { 577 MessageContent content = (MessageContent)contentsIter.next(); 578 if (emailContainsMedia) { 579 Iterator attachments = emailActivity.getMedia().iterator(); 580 while (attachments.hasNext()) { 581 Media attachment = (Media)attachments.next(); 582 if(content.getId() != null && content.getId().equals(attachment.getContentName())) { 583 AppLog.trace("Attachment already linked, '" 584 + mimeMsg.getSubject() + "', " 585 + mimeMsg.getMessageID() + "', " 586 + content.getId()); 587 } 588 else { 589 addMedia(rootPkg,emailActivity,content); 591 } 592 } 593 } 594 else { 595 this.addMedia( 597 rootPkg, 598 emailActivity, 599 content 600 ); 601 } 602 } 603 604 } 605 } 606 607 616 private void importSimpleMimeMessage( 617 RefPackage_1_0 rootPkg, 618 String providerName, 619 String segmentName, 620 String creatorCriteria, 621 SimpleMimeMessage mimeMsg 622 ) throws ServiceException { 623 624 try { 625 List activities = this.lookupEMailActivity( 626 rootPkg, 627 providerName, 628 segmentName, 629 mimeMsg.getMessageID() 630 ); 631 if (activities.size() == 0) { 632 AppLog.trace("create a new EMailActivity"); 633 634 ActivityCreator emailCreator = this.getEMailCreator( 635 rootPkg, 636 providerName, 637 segmentName, 638 creatorCriteria 639 ); 640 641 rootPkg.refBegin(); 642 643 activity1Package activityPkg = getActivityPkg(rootPkg); 644 NewActivityParams newActParam = activityPkg.createNewActivityParams( 645 null, 646 null, 647 null, mimeMsg.getSubject(), 649 mimeMsg.getPriority(), 650 null, null, null ); 654 NewActivityResult newActivityResult = emailCreator.newActivity( 655 newActParam 656 ); 657 rootPkg.refCommit(); 658 659 EMail emailActivity = (EMail)activityPkg.refObject(newActivityResult.getActivity().refMofId()); 661 rootPkg.refBegin(); 662 emailActivity.setMessageSubject(mimeMsg.getSubject()); 663 emailActivity.setMessageBody(mimeMsg.getBody()); 664 emailActivity.getExternalLink().clear(); 665 emailActivity.getExternalLink().add(mimeMsg.getMessageID()); 666 emailActivity.setSendDate(mimeMsg.getDate()); 667 rootPkg.refCommit(); 668 669 String fromAddress = mimeMsg.getFrom()[0]; 671 List addresses = this.lookupEMailAddress( 672 rootPkg, 673 providerName, 674 segmentName, 675 fromAddress 676 ); 677 EMailAddress from = null; 678 if (addresses.size() == 1) { 679 from = (EMailAddress) addresses.iterator().next(); 680 rootPkg.refBegin(); 681 emailActivity.setSender(from); 682 rootPkg.refCommit(); 683 } else { 684 AppLog.trace("lookup " + fromAddress + " finds " 685 + addresses.size() + " addresses"); 686 } 687 this.addRecipientToEMailActivity( 689 rootPkg, 690 providerName, 691 segmentName, 692 emailActivity, 693 mimeMsg.getRecipients(), 694 Message.RecipientType.TO 695 ); 696 this.addRecipientToEMailActivity( 697 rootPkg, 698 providerName, 699 segmentName, 700 emailActivity, 701 mimeMsg.getRecipients(Message.RecipientType.CC), 702 Message.RecipientType.CC 703 ); 704 this.addNote( 706 rootPkg, 707 emailActivity, 708 "Recipients", 709 this.addRecipientsAsNote( 710 rootPkg, 711 providerName, 712 segmentName, 713 mimeMsg 714 ) 715 ); 716 717 this.addNote( 719 rootPkg, 720 emailActivity, 721 "Message-Header", 722 mimeMsg.getAllHeaderLinesAsString() 723 ); 724 this.addMedia( 726 mimeMsg, 727 rootPkg, 728 emailActivity 729 ); 730 } 731 else if (activities.size() == 1) { 732 AppLog.warning( 733 "Import of email message skipped, an email with this message id exists already, " 734 + mimeMsg.getMessageID() + ", " + mimeMsg.getSubject()); 735 } 736 else { 737 AppLog.warning( 738 "Import of email message skipped, found " 739 + activities.size() + " email with this message id, " 740 + mimeMsg.getMessageID() + ", " + mimeMsg.getSubject()); 741 } 742 } 743 catch (RefException e) { 744 AppLog.info("Can not create email activity", e.getMessage()); 745 throw new ServiceException(e); 746 } 747 } 748 749 763 private void addRecipientToEMailActivity( 764 RefPackage_1_0 rootPkg, 765 String providerName, 766 String segmentName, 767 EMail emailActivity, 768 String [] addresses, 769 Message.RecipientType type 770 ) { 771 if (addresses == null || addresses.length == 0) { 772 AppLog.trace("Message does not contain any recipient of type '" + type.toString() + "'"); 773 } 774 for (int i = 0; i < addresses.length; i++) { 775 String address = addresses[i]; 776 List emailAddresses = this.lookupEMailAddress( 777 rootPkg, 778 providerName, 779 segmentName, 780 address 781 ); 782 if(emailAddresses.size() > 0) { 783 this.addEMailRecipient( 784 rootPkg, 785 emailActivity, 786 (EMailAddress)emailAddresses.iterator().next(), 787 type 788 ); 789 } 790 } 791 } 792 793 806 private String addRecipientsAsNote( 807 RefPackage_1_0 rootPkg, 808 String providerName, 809 String segmentName, 810 SimpleMimeMessage mimeMsg 811 ) { 812 StringBuffer text = new StringBuffer (); 813 814 String addresses[] = mimeMsg.getFrom(); 816 for (int i = 0; i < addresses.length; i++) { 817 List emailAddresses = lookupEMailAddress( 818 rootPkg, 819 providerName, 820 segmentName, 821 addresses[i] 822 ); 823 text.append("FROM: " + addresses[i] + " [" 824 + ((emailAddresses == null || emailAddresses.size() == 0) ? "UNMATCHED" : "MATCHED") + "]\n"); 825 } 826 827 addresses = mimeMsg.getRecipients(Message.RecipientType.TO); 829 for (int i = 0; i < addresses.length; i++) { 830 List emailAddresses = lookupEMailAddress( 831 rootPkg, 832 providerName, 833 segmentName, 834 addresses[i] 835 ); 836 text.append("TO: " + addresses[i] + " [" 837 + ((emailAddresses == null || emailAddresses.size() == 0) ? "UNMATCHED" : "MATCHED") + "]\n"); 838 } 839 840 addresses = mimeMsg.getRecipients(Message.RecipientType.CC); 842 for (int i = 0; i < addresses.length; i++) { 843 List emailAddresses = lookupEMailAddress( 844 rootPkg, 845 providerName, 846 segmentName, 847 addresses[i] 848 ); 849 text.append("CC: " + addresses[i] + " [" 850 + ((emailAddresses == null || emailAddresses.size() == 0) ? "UNMATCHED" : "MATCHED") + "]\n"); 851 } 852 853 addresses = mimeMsg.getRecipients(Message.RecipientType.CC); 855 for (int i = 0; i < addresses.length; i++) { 856 List emailAddresses = lookupEMailAddress( 857 rootPkg, 858 providerName, 859 segmentName, 860 addresses[i] 861 ); 862 text.append("BCC: " + addresses[i] + " [" 863 + ((emailAddresses == null || emailAddresses.size() == 0) ? "UNMATCHED" : "MATCHED") + "]\n"); 864 } 865 return text.toString(); 866 } 867 868 877 private void addNote( 878 RefPackage_1_0 rootPkg, 879 EMail emailActivity, 880 String title, 881 String content 882 ) { 883 rootPkg.refBegin(); 884 Note note = getGenericPkg(rootPkg).getNoteClass().createNote(); 885 emailActivity.addNote(uuidGenerator.next().toString(), note); 886 note.setTitle(title); 887 note.setText(content); 888 note.getOwningGroup().addAll( 890 emailActivity.getOwningGroup() 891 ); 892 rootPkg.refCommit(); 893 } 894 895 905 private void addMedia( 906 RefPackage_1_0 rootPkg, 907 EMail emailActivity, 908 MessageContent content 909 ) { 910 rootPkg.refBegin(); 911 Media media = getGenericPkg(rootPkg).getMediaClass().createMedia(); 912 emailActivity.addMedia(uuidGenerator.next().toString(), media); 913 String contentType = content.getContentType(); 914 if(contentType != null && contentType.length() > 50) { 915 MimeType mt = null; 916 try { 917 mt = new MimeType (contentType); 918 contentType = mt.getBaseType(); 919 AppLog.trace("MimeType: " + mt.toString()); 920 } 921 catch (MimeTypeParseException e) { 922 contentType = contentType.substring(0,49); 926 } 927 } 928 media.setContentName(content.getId()); 929 media.setContent(content.getInputStream()); 930 media.setContentMimeType(contentType); 931 if(AppLog.isTraceOn()) { 932 AppLog.trace("Media to add: " + content.toString()); 933 } 934 media.getOwningGroup().addAll( 936 emailActivity.getOwningGroup() 937 ); 938 rootPkg.refCommit(); 939 } 940 941 951 private void addEMailRecipient( 952 RefPackage_1_0 rootPkg, 953 EMail emailActivity, 954 EMailAddress address, 955 Message.RecipientType type 956 ) { 957 activity1Package activityPkg = getActivityPkg(rootPkg); 958 activityPkg.refBegin(); 959 EMailRecipient recipient = activityPkg.getEMailRecipientClass().createEMailRecipient(); 960 emailActivity.addEmailRecipient( 961 uuidGenerator.next().toString(), 962 recipient 963 ); 964 recipient.setParty(address); 965 if(Message.RecipientType.TO.toString().equalsIgnoreCase(type.toString())) { 966 recipient.setPartyType(PARTY_TYPE_EMAIL_TO); 967 } 968 else if(Message.RecipientType.CC.toString().equalsIgnoreCase(type.toString())) { 969 recipient.setPartyType(PARTY_TYPE_EMAIL_CC); 970 } 971 else if(Message.RecipientType.BCC.toString().equalsIgnoreCase(type.toString())) { 972 recipient.setPartyType(PARTY_TYPE_EMAIL_BCC); 973 } 974 recipient.getOwningGroup().addAll( 976 emailActivity.getOwningGroup() 977 ); 978 activityPkg.refCommit(); 979 } 980 981 992 private ActivityCreator getEMailCreator( 993 RefPackage_1_0 rootPkg, 994 String providerName, 995 String segmentName, 996 String criteria 997 ) { 998 try { 999 org.opencrx.kernel.activity1.cci.activity1Package activityPkg = getActivityPkg( 1000 rootPkg 1001 ); 1002 org.opencrx.kernel.activity1.cci.Segment activitySegment = getActivitySegment( 1003 rootPkg, 1004 providerName, 1005 segmentName 1006 ); 1007 1008 ActivityCreator emailCreator = null; 1009 ActivityCreatorFilter filter = activityPkg.createActivityCreatorFilter(); 1011 filter.thereExistsName( 1012 FilterOperators.IS_LIKE, 1013 new String []{criteria} 1014 ); 1015 for(Iterator i = activitySegment.getActivityCreator(filter).iterator(); i.hasNext(); ) { 1016 ActivityCreator creator = (ActivityCreator)i.next(); 1017 if((creator.getActivityType() != null) && (creator.getActivityType().getActivityClass() == ACTIVITY_CLASS_EMAIL)) { 1018 emailCreator = creator; 1019 break; 1020 } 1021 } 1022 if(emailCreator == null) { 1024 emailCreator = activitySegment.getActivityCreator(DEFAULT_EMAIL_CREATOR_ID); 1025 } 1026 return emailCreator; 1027 } 1028 catch(Exception e) { 1029 AppLog.info("Can not retrieve config from external task configuration", e.getMessage()); 1030 } 1031 return null; 1032 } 1033 1034 1043 private void importNestedMessages( 1044 RefPackage_1_0 rootPkg, 1045 String providerName, 1046 String segmentName, 1047 SimpleMimeMessage message 1048 ) { 1049 AppLog.trace("Importing Message (" + providerName + "/" + segmentName + "): " + message.toString()); 1050 try { 1051 AppLog.trace("Import email activities based on the mail attachments"); 1053 if (message.containsNestedMessage()) { 1054 AppLog.trace("Create new EMail activities for nested email messages"); 1055 for(Iterator i = message.getBinaryContents().iterator(); i.hasNext(); ) { 1056 MessageContent content = (MessageContent)i.next(); 1057 if(content.getContent() instanceof SimpleMimeMessage) { 1058 this.importSimpleMimeMessage( 1059 rootPkg, 1060 providerName, 1061 segmentName, 1062 message.getSubject(), 1063 (SimpleMimeMessage)content.getContent() 1064 ); 1065 } 1066 } 1067 } 1068 else { 1069 AppLog.trace("Email does not contain any nested email message, ignore it"); 1070 } 1071 } 1072 catch (ServiceException e) { 1073 AppLog.warning("Error importing EmailActivity", e); 1074 } 1075 } 1076 1077 1086 protected void importMessages( 1087 String providerName, 1088 String segmentName 1089 ) throws IOException { 1090 try { 1091 RefPackage_1_0 rootPkg = this.createDataPkg( 1092 new ServiceHeader(SecurityKeys.ADMIN_PRINCIPAL + SecurityKeys.ID_SEPARATOR + segmentName, null, false, new QualityOfService()) 1093 ); 1094 WorkflowControllerServlet.initWorkflows( 1095 rootPkg, 1096 providerName, 1097 segmentName 1098 ); 1099 this.initEMailCreator( 1100 rootPkg, 1101 providerName, 1102 segmentName 1103 ); 1104 MailServerConfig config = new MailServerConfig( 1105 rootPkg, 1106 providerName, 1107 segmentName, 1108 this.getServletContext() 1109 ); 1110 MailStore mailStore = new MailStore(config); 1111 mailStore.openStore(); 1112 mailStore.openFolder( 1113 config.getMailbox() 1114 ); 1115 SimpleMimeMessage[] messages = mailStore.getMessages(); 1116 for (int i = 0; i < messages.length; i++) { 1117 this.importNestedMessages( 1118 rootPkg, 1119 providerName, 1120 segmentName, 1121 messages[i] 1122 ); 1123 if(config.deleteImportedMessages()) { 1124 messages[i].markAsDeleted(); 1125 } 1126 } 1127 mailStore.closeFolder(); 1128 mailStore.closeStore(); 1129 } 1130 catch (ServiceException e) { 1131 AppLog.warning(new Date () + ": openCRX/EMailImporter: " + providerName + "/" + segmentName + ": exception occured " + e.getMessage() + ". Continuing"); 1132 new ServiceException(e).log(); 1133 } 1134 } 1135 1136 1145 protected void handleRequest( 1146 HttpServletRequest req, 1147 HttpServletResponse res 1148 ) throws ServletException , IOException { 1149 String segmentName = req.getParameter("segment"); 1150 String providerName = req.getParameter("provider"); 1151 String id = providerName + "/" + segmentName; 1152 if( 1154 COMMAND_EXECUTE.equals(req.getPathInfo()) && 1155 !this.runningSegments.contains(id) 1156 ) { 1157 try { 1158 this.runningSegments.add(id); 1159 this.importMessages( 1160 providerName, 1161 segmentName 1162 ); 1163 } 1164 catch(Exception e) { 1165 new ServiceException(e).log(); 1166 } 1167 finally { 1168 this.runningSegments.remove(id); 1169 } 1170 } 1171 } 1172 1173 1177 protected void doGet( 1178 HttpServletRequest req, 1179 HttpServletResponse res 1180 ) throws ServletException , IOException { 1181 res.setStatus(HttpServletResponse.SC_OK); 1182 res.flushBuffer(); 1183 this.handleRequest( 1184 req, 1185 res 1186 ); 1187 } 1188 1189 1193 protected void doPost( 1194 HttpServletRequest req, 1195 HttpServletResponse res 1196 ) throws ServletException , IOException { 1197 res.setStatus(HttpServletResponse.SC_OK); 1198 res.flushBuffer(); 1199 this.handleRequest( 1200 req, 1201 res 1202 ); 1203 } 1204 1205 private static final long serialVersionUID = -7260829387268368633L; 1209 1210 private static final short PARTY_TYPE_EMAIL_TO = 220; 1211 private static final short PARTY_TYPE_EMAIL_CC = 230; 1212 private static final short PARTY_TYPE_EMAIL_BCC = 240; 1213 1214 private static final short ACTIVITY_CLASS_EMAIL = 0; 1215 1216 private static final String COMMAND_EXECUTE = "/execute"; 1217 private static final String DEFAULT_EMAIL_CREATOR_ID = "EMailCreator"; 1218 1219 private static final UUIDGenerator uuidGenerator = UUIDs.getGenerator(); 1220 1221 private Dataprovider_1_1Connection connectionData = null; 1222 1223 private final List runningSegments = new ArrayList (); 1224 1225} 1226 1227 | Popular Tags |