| 1 16 package org.apache.jetspeed.modules.actions.portlets.email; 17 18 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 19 import org.apache.jetspeed.services.JetspeedSecurity; 20 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 21 import org.apache.jetspeed.services.logging.JetspeedLogger; 22 import org.apache.jetspeed.util.PortletConfigState; 23 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction; 24 25 import org.apache.turbine.util.RunData; 26 import org.apache.torque.util.Criteria; 27 import org.apache.turbine.util.upload.FileItem; 28 29 import org.apache.velocity.context.Context; 30 31 import java.util.Vector ; 33 import java.util.Hashtable ; 34 35 import java.util.List ; 36 import java.util.Enumeration ; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.ByteArrayInputStream ; 41 42 44 import javax.mail.Folder ; 45 import javax.mail.AuthenticationFailedException ; 46 import javax.mail.NoSuchProviderException ; 47 import javax.mail.MessagingException ; 48 import javax.mail.Message ; 49 import javax.mail.Multipart ; 50 import javax.mail.Part ; 51 import javax.mail.Header ; 52 53 import javax.servlet.http.HttpServletResponse ; 55 56 import org.apache.jetspeed.om.apps.email.EmailInboxPeer; 58 import org.apache.jetspeed.om.apps.email.EmailInbox; 59 60 import org.apache.jetspeed.util.PortletSessionState; 61 67 68 public class EmailPortlet extends VelocityPortletAction 69 { 70 71 private static final JetspeedLogger log = JetspeedLogFactoryService 72 .getLogger(EmailPortlet.class.getName()); 73 74 private final int maxPerPage = 10; 75 76 81 protected void buildMaximizedContext(VelocityPortlet portlet, 82 Context context, RunData rundata) throws Exception  83 { 84 buildNormalContext(portlet, context, rundata); 85 86 } 87 88 protected static final String CUSTOMIZE_TEMPLATE = "customizeTemplate"; 89 90 94 protected void buildConfigureContext(VelocityPortlet portlet, 95 Context context, RunData rundata) 96 { 97 try 98 { 99 super.buildConfigureContext(portlet, context, rundata); 100 } catch (Exception ex) 101 { 102 log.error("Exception", ex); 103 } 104 String template = PortletConfigState.getParameter(portlet, rundata, 105 CUSTOMIZE_TEMPLATE, null); 106 setTemplate(rundata, template); 107 } 108 109 113 protected void buildNormalContext(VelocityPortlet portlet, Context context, 114 RunData rundata) throws Exception  115 { 116 117 118 Hashtable userInfo = this.getEmailUserInfo(rundata, context); 119 Email email = null; 120 121 log.info("BuildNormalContext in EmailPortlet"); 122 String host = getPortletParameter(rundata, context, "hostname"); 123 context.put("host", host); 124 125 String user = (String ) userInfo.get("username"); 126 String pass = (String ) userInfo.get("password"); 127 String emailAdd = (String ) userInfo.get("email"); 128 129 context.put("emailAdd", emailAdd); 130 String message_prompt = (String )getPortletSession(rundata, context, 131 "message_prompt"); 132 133 context.put("message_prompt", message_prompt); 134 setPortletSession(rundata, context, "message_prompt", ""); 135 136 137 138 139 try 140 { 141 email = new Email(user, pass, 142 getPortletParameters(rundata, context)); 143 context.put("hasLoggedIn", "yes"); 145 } catch (AuthenticationFailedException ae) 146 { 147 message_prompt = "Please Enter a Valid Username and Password."; 148 context.put("message_prompt", message_prompt); 149 context.put("hasLoggedIn", "no"); 150 log.error(ae); 151 return; 152 } catch (NoSuchProviderException np) 153 { 154 message_prompt = "Please Check Email parameters... Protocol(imap/pop3) is case-sensitive. "; 155 context.put("message_prompt", message_prompt); 156 context.put("hasLoggedIn", "no"); 157 log.error(np); 158 return; 159 } catch (Exception e) 160 { 161 message_prompt = e.getMessage(); 162 context.put("message_prompt", message_prompt); 163 context.put("hasLoggedIn", "no"); 164 log.error(e); 165 return; 166 } 167 168 169 String showNumNewmessages = (String )getPortletSession(rundata, context, 170 "showNumNewmessages"); 171 172 if (showNumNewmessages == null || showNumNewmessages.equals("")) 173 { 177 String protocol = getPortletParameter(rundata, context, "protocol"); 178 context.put("protocol", protocol); 179 180 int numNewmessages = email.num_Newmessages(); 181 182 String showInbox = (String )getPortletSession(rundata, context, 183 "showInbox"); 184 185 log.info("showInbox " + showInbox); 186 if (showInbox == null || showInbox.equals("")) { 189 if (numNewmessages != 0) 190 { 191 context.put("numNewmessages", String 192 .valueOf(numNewmessages)); } 197 } 198 context.put("num_Newmessages", String.valueOf(numNewmessages)); setPortletSession(rundata, context, "showNumNewmessages", "yes"); 206 setPortletSession(rundata, context, "numNewmessages", String 207 .valueOf(numNewmessages)); 208 209 } else if (showNumNewmessages.equals("yes")) 210 { 211 String num_Newmessages = (String )getPortletSession(rundata, context, 212 "numNewmessages"); 213 context.put("num_Newmessages", num_Newmessages); 214 } 215 216 String compose = (String )getPortletSession(rundata, context, "compose"); 217 context.put("compose", compose); 218 219 String showInbox = (String )getPortletSession(rundata, context, "showInbox"); 220 context.put("showInbox", showInbox); 221 222 String showContent = (String )getPortletSession(rundata, context, 223 "showContent"); 224 context.put("showContent", showContent); 225 226 String createfolder = (String )getPortletSession(rundata, context, 227 "createfolder"); 228 context.put("createfolder", createfolder); 229 230 String showFolders = (String )getPortletSession(rundata, context, 231 "showFolders"); 232 context.put("showFolders", showFolders); 233 234 String protocol = (String )getPortletParameter(rundata, context, "protocol"); 236 if (protocol.equals("imap")) 237 { 238 String folder_name = (String )getPortletSession(rundata, context, 239 "folder_name"); 240 241 if (folder_name == null || folder_name.equals("")) 242 { 243 context.put("folder_name", "Inbox"); 244 } else 245 { 246 context.put("folder_name", folder_name); 247 } 248 249 String showmessagefolder = (String )getPortletSession(rundata, context, 251 "showmessagefolder"); 252 context.put("showmessagefolder", showmessagefolder); 253 } 254 255 String inboxMessages = (String )getPortletSession(rundata, context, 256 "inboxMessages"); 257 258 checkMessages(rundata, context,email); 259 260 String msgeContent = (String )getPortletSession(rundata, context, 261 "msgeContent"); 262 263 log.info("msgecontent " + msgeContent); 264 265 if (msgeContent == null || msgeContent.equals("")) 266 { 267 log.info("null"); 268 269 Vector inContent = (Vector ) getPortletSession(rundata, context, "inContent"); 272 273 context.put("inContent", inContent); 274 } else if (msgeContent.equals("yes")) 275 { 276 doShowcontent(rundata, context,email); 277 setPortletSession(rundata, context, "msgeContent", null); 278 279 } 280 281 String reply = (String )getPortletSession(rundata, context, "reply"); 282 context.put("reply", reply); 283 String forward = (String )getPortletSession(rundata, context, "forward"); 284 context.put("forward", forward); 285 String rsubject = (String )getPortletSession(rundata, context, "rsubject"); 286 context.put("rsubject", rsubject); 287 288 String msg = (String )getPortletSession(rundata, context, "msg"); 289 context.put("msg", msg); 290 291 293 String msgctr = (String )getPortletSession(rundata, context, "msgcount"); 294 String msgcount = ""; 295 if (msgctr != null && !msgctr.equals("")) 296 { 297 Integer imsgctr = new Integer (msgctr); 298 context.put("msgcount", imsgctr); 299 } else 300 { 301 context.put("msgcount", ""); 302 } 303 304 String pages = (String )getPortletSession(rundata, context, 305 "total_no_of_pages"); 306 String total_no_of_pages = ""; 307 if (pages != null && !pages.equals("")) 308 { 309 Integer ipages = new Integer (pages); 310 context.put("total_no_of_pages", ipages); 311 } else 312 { 313 context.put("total_no_of_pages", ""); 314 } 315 316 String cpage = (String )getPortletSession(rundata, context, "cur_page"); 317 String cur_page = ""; 318 if (cpage != null && !cpage.equals("")) 319 { 320 Integer icur_page = new Integer (cpage); 321 context.put("cur_page", icur_page); 322 } else 323 { 324 context.put("cur_page", ""); 325 } 326 327 String in_index = (String )getPortletSession(rundata, context, "start_index"); 328 String start_index = ""; 329 if (in_index != null && !in_index.equals("")) 330 { 331 Integer iin_index = new Integer (in_index); 332 context.put("start_index", iin_index); 333 } else 334 { 335 context.put("start_index", ""); 336 } 337 338 String cRange = (String )getPortletSession(rundata, context, "range_per_page"); 339 String range_per_page = ""; 340 if (cRange != null && !cRange.equals("")) 341 { 342 Integer irange_per_page = new Integer (cRange); 343 context.put("range_per_page", irange_per_page); 344 } else 345 { 346 context.put("range_per_page", ""); 347 } 348 349 if (protocol.equals("imap")) 350 { 351 context.put("protocol", protocol); 352 353 Vector vFolders = (Vector ) email.allFolders(); 354 if (vFolders != null) 355 { 356 357 context.put("vFolders", vFolders); 358 } 359 360 Vector message_folder = (Vector ) getPortletSession(rundata, context, "message_folder"); 362 363 if (message_folder != null) 364 { 365 366 context.put("message_folder", message_folder); 367 } 368 369 } 370 email.close(); 371 System.gc(); 372 373 } 375 public void doAuth(RunData data, Context context) 376 { 377 log.info("doAuth in emailportlet"); 378 Hashtable userInfo = this.getEmailUserInfo(data, context); 379 380 String user = (String ) userInfo.get("username"); 381 String pass = (String ) userInfo.get("password"); 382 383 try 384 { 385 Email email = new Email(user, pass, getPortletParameters(data, 386 context)); 387 setPortletSession(data, context, "showFolders", "yes"); 389 setPortletSession(data, context, "hasLoggedIn", "yes"); 390 setPortletSession(data, context, "showNumNewmessages", null); 392 setPortletSession(data, context, "showInbox", null); 393 setPortletSession(data, context, "message_prompt", null); 394 email.close(); 395 396 } catch (AuthenticationFailedException ae) 397 { 398 String message_prompt = "Authentication Failed... Bad LogIn."; 399 setPortletSession(data, context, "hasLoggedIn", "no"); 400 setPortletSession(data, context, "message_prompt", message_prompt); 401 } catch (NoSuchProviderException np) 402 { 403 String message_prompt = "Please Check Email parameters... Protocol(imap/pop3) is case-sensitive."; 404 setPortletSession(data, context, "hasLoggedIn", "no"); 405 setPortletSession(data, context, "message_prompt", message_prompt); 406 } catch (Exception e) 407 { 408 String message_prompt = e.getMessage(); 409 setPortletSession(data, context, "message_prompt", message_prompt); 410 log.info("error : " + e.getMessage()); 411 } 412 413 } 415 416 public void doSignout(RunData data, Context context) 417 { 418 log.info("sign out"); 419 setPortletSession(data, context, "hasLoggedIn", "no"); 420 setPortletSession(data, context, "showFolders", "no"); 421 setPortletSession(data, context, "showInbox", "no"); 422 setPortletSession(data, context, "compose", "no"); 423 setPortletSession(data, context, "showContent", "no"); 424 setPortletSession(data, context, "reply", "no"); 425 setPortletSession(data, context, "forward", "no"); 426 setPortletSession(data, context, "createfolder", "no"); 427 setPortletSession(data, context, "message_prompt", null); 428 } 429 430 public void doCompose(RunData data, Context context) throws Exception  431 { 432 getEmailUserInfo(data, context); 433 log.info("docompose"); 434 setPortletSession(data, context, "showFolders", "no"); 435 setPortletSession(data, context, "showInbox", "no"); 436 setPortletSession(data, context, "compose", "yes"); 437 setPortletSession(data, context, "showContent", "no"); 438 setPortletSession(data, context, "reply", "no"); 439 setPortletSession(data, context, "forward", "no"); 440 setPortletSession(data, context, "createfolder", "no"); 441 setPortletSession(data, context, "showmessagefolder", "no"); 442 setPortletSession(data, context, "message_prompt", null); 443 444 } 445 446 public int checkNewmessage(Message message[], int current_index) 448 throws Exception  449 { 450 log.info("### check new message"); 451 452 String msgeId = getMessageId(message[current_index]); 454 455 Criteria cr = new Criteria(); 456 cr.add(EmailInboxPeer.MESSAGE_ID, msgeId); 457 458 List vMsge = EmailInboxPeer.doSelect(cr); if (vMsge.isEmpty()) 460 { return 1; 462 } else 463 { 465 EmailInbox email = (EmailInbox) EmailInboxPeer.doSelect(cr).get(0); 466 if (email.getReadflag() == 0) 468 { 469 return 1; } else 471 return 0; 472 } 473 } 474 475 public void doSend(RunData data, Context context) 476 { 477 try 478 { 479 Hashtable userInfo = this.getEmailUserInfo(data, context); 480 481 log.info("do send in email portlet"); 482 String user = (String ) userInfo.get("username"); 483 String pass = (String ) userInfo.get("password"); 484 485 String addressTo = data.getParameters().getString("addressTo"); 486 String addressFrom = data.getParameters().getString("addressFrom"); 487 setPortletSession(data, context, "addressFrom", addressFrom); 488 489 String subject = data.getParameters().getString("subject"); 490 String msg = data.getParameters().getString("msg"); 491 FileItem fileItem = data.getParameters().getFileItem("newfile"); 492 493 Email email = new Email(user, pass, getPortletParameters(data, 494 context)); 495 496 email.doSendEmail(addressTo, addressFrom, subject, msg, fileItem); 497 email.close(); 498 String message_prompt = "Message Sent"; 499 setPortletSession(data, context, "message_prompt", message_prompt); 500 setPortletSession(data, context, "msgeIndex", null); 501 502 } catch (Exception e) 503 { 504 setPortletSession(data, context, "message_prompt", 505 "Message Sending Failed." + e.getMessage()); 506 507 log.error("doSend()", e); 508 } 509 } 510 511 public void doReply(RunData data, Context context) throws Exception  512 { 513 514 log.info("doReply"); 515 516 setPortletSession(data, context, "showContent", "no"); 517 setPortletSession(data, context, "forward", "no"); 518 setPortletSession(data, context, "reply", "yes"); 519 520 Hashtable h = (Hashtable ) getPortletSession(data, context, "hcontent"); 521 522 String subject = (String ) h.get("Subject").toString(); 524 525 String msg = (String ) h.get("message").toString(); 527 setPortletSession(data, context, "msg", "Original Message: " + "\n" 528 + "From: " + h.get("From") + "\n" + "Subject: " 529 + h.get("Subject") + "\n" + msg); 530 531 if (subject.length() > 3) 534 { 535 if (subject.substring(0, 3).equals("Re:")) 536 { 537 setPortletSession(data, context, "rsubject", subject); 538 539 } else 540 { 541 setPortletSession(data, context, "rsubject", "Re:" + subject); 542 log.info("subject" + subject); 543 } 544 } else 545 { 546 setPortletSession(data, context, "rsubject", "Re:" + subject); 547 } 548 setPortletSession(data, context, "msgeIndex", null); 549 } 550 551 public void doSendreply(RunData data, Context context) 552 { 553 String recipient = null; 554 555 try 556 { 557 log.info("doSendReply"); 558 559 Hashtable userInfo = this.getEmailUserInfo(data, context); 560 String user = (String ) userInfo.get("username"); 561 String pass = (String ) userInfo.get("password"); 562 String emailAdd = (String ) userInfo.get("email"); 563 564 String index = (String )getPortletSession(data, context, "index"); 565 566 int current_index = Integer.parseInt(index); 567 log.info("index" + current_index); 568 569 String msg = (String ) data.getParameters().getString("msg"); 570 log.info("reply message &&&&&&&&&&&&&&&& " + msg); 571 setPortletSession(data, context, "msg", msg); 572 574 String from = data.getParameters().getString("addressFrom"); 575 576 String addressTo = data.getParameters().getString("addressTo"); 579 580 String subject = data.getParameters().getString("subject"); 582 583 msg = data.getParameters().getString("msg"); 585 586 String newmsge = checkFormat(msg); 587 setPortletSession(data, context, "msg", newmsge); 588 if (subject.equals("")) 590 { 591 subject = "none"; 592 } 593 594 if (subject.substring(0, 3).equals("Re:")) 597 { 598 setPortletSession(data, context, "rsubject", subject); 599 } else 600 { 601 setPortletSession(data, context, "rsubject", "Re:" + subject); 602 } 603 604 String newSubj = (String )getPortletSession(data, context, "rsubject"); 605 msg = (String )getPortletSession(data, context, "msg"); 606 607 String msgecontent = convertMessage(msg); 608 609 FileItem fileItem = data.getParameters().getFileItem( 610 "attachmentReply"); 611 612 Email email = new Email(user, pass, getPortletParameters(data, 613 context)); 614 Message message = email.getMessage(current_index); 616 email.reply(from, addressTo, msgecontent, newSubj, fileItem, 617 message); 618 email.close(); 619 620 String message_prompt = "Reply Sent"; 621 setPortletSession(data, context, "message_prompt", message_prompt); 622 setPortletSession(data, context, "msgeIndex", null); 623 } catch (Exception e) 624 { 625 626 setPortletSession(data, context, "message_prompt", 627 "Message Sending Failed." + e.getMessage()); 628 log.error("Error in doSendReply()", e); 629 } 630 } 631 632 public void doForward(RunData data, Context context) 633 { 634 log.info("doforward"); 635 setPortletSession(data, context, "showContent", "no"); 636 setPortletSession(data, context, "reply", "no"); 637 setPortletSession(data, context, "createfolder", "no"); 638 setPortletSession(data, context, "forward", "yes"); 639 640 Hashtable h = (Hashtable ) getPortletSession(data, context, "hcontent"); 641 642 String subject = (String ) h.get("Subject").toString(); 644 log.info("subj in doforward" + subject); 645 646 if (subject.length() > 3) 649 { 650 if (subject.substring(0, 4).equals("Fwd:")) 651 { 652 setPortletSession(data, context, "rsubject", subject); 653 log.info("subject" + subject); 654 } else 655 { 656 setPortletSession(data, context, "rsubject", "Fwd:" + subject); 657 } 658 } else 659 { 660 setPortletSession(data, context, "rsubject", "Fwd:" + subject); 661 log.info("subject" + subject); 662 } 663 664 String msg = (String ) h.get("message").toString(); 666 setPortletSession(data, context, "msg", msg); 667 setPortletSession(data, context, "msgeIndex", null); 668 669 } 670 671 public void doForwardsend(RunData data, Context context) 672 { 673 674 log.info("forwardsend"); 675 676 Hashtable userInfo = this.getEmailUserInfo(data, context); 677 String user = (String ) userInfo.get("username"); 678 String pass = (String ) userInfo.get("password"); 679 String  |