1 32 33 package scioworks.imap.presentation.imapWeb; 34 35 import java.util.Vector ; 36 import java.io.*; 37 38 import javax.mail.*; 39 import javax.mail.internet.*; 40 41 import org.w3c.dom.*; 42 import org.w3c.dom.html.*; 43 import org.enhydra.xml.xmlc.XMLCUtil; 44 45 import com.lutris.appserver.server.httpPresentation.*; 46 import com.lutris.util.KeywordValueException; 47 48 import scioworks.imap.presentation.base.*; 49 import scioworks.imap.presentation.imapWeb.*; 50 import scioworks.imap.spec.beans.*; 51 import scioworks.imap.spec.ImapWebException; 52 import scioworks.imap.spec.ImapWebConstant; 53 54 public class mailAction extends BasePO { 55 56 private String send() throws HttpPresentationException { 57 String fTo = super.getStringParameter(PARAM_to); 59 String fSubject = super.getStringParameter(PARAM_subject); 60 String fCc = super.getStringParameter(PARAM_cc); 61 String fBcc = super.getStringParameter(PARAM_bcc); 62 String fMessage = super.getStringParameter(PARAM_message); 63 String [] fSaveCopy = super.getStringParameterArray(PARAM_savecopy); 64 String fOrigFolder = super.getStringParameter(PARAM_origFolder); 65 long fOrigMsgid = super.getLongParameter(PARAM_origMsgid); 66 67 boolean saveCopy = (fSaveCopy.length != 0); 68 69 boolean isForwardAttach; 70 if (fOrigMsgid != -1) { 71 isForwardAttach = true; 72 } else { 73 isForwardAttach = false; 74 } 75 76 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 77 78 try { 79 80 Object obj = super.getComms().sessionData.get(FileUploadSessionData.SESSION_KEY); 81 Vector attachments; 82 if (obj != null) { 83 attachments = ((FileUploadSessionData) obj).getFileList(); 84 85 } else { 86 attachments = new Vector (); 88 } 89 90 iwMsg.sendMessage(super.getImapWebSessionData().getImapStore(), 91 super.getImapWebSessionData().getImapSession(), 92 super.getImapWebSessionData().getImapURL(), 93 ImapWebConstant.singleton().domain(), 94 fTo, fSubject, fCc, fBcc, fMessage, 95 attachments, 96 true, saveCopy, 98 isForwardAttach, 99 fOrigFolder, 100 fOrigMsgid); 101 102 if (obj != null) { 104 String tmpFilename; 106 for (int i=0; i<attachments.size(); i++) { 107 tmpFilename = ((FileUploadSessionData)obj).getTmpFilename(i); 108 File file = new File(tmpFilename); 109 if (!file.delete()) { 110 System.err.println("Cannot delete tmp file: "+ tmpFilename); 111 } 112 } 113 super.getComms().sessionData.remove(FileUploadSessionData.SESSION_KEY); 114 } 115 116 117 return showCompletionPage(MSG_OPERATION_COMPLETED, 118 "Your email has been sent.", "", ""); 119 } catch(NullPointerException ex) { 120 124 return super.showCompletionPage(MSG_OPERATION_FAILED, 125 "You cannot send massage while runing imapWeb_pres! That was a default HTML page", 126 "", ""); 127 } catch (ImapWebException e) { 128 return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), "", ""); 130 131 } catch (KeywordValueException e) { 132 return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), "", ""); 133 } 134 } 135 136 private String forward() throws HttpPresentationException { 137 String fFolder = super.getStringParameter(PARAM_folder); 139 long fMsgid = super.getLongParameter(PARAM_msgid); 140 String fForwardType = super.getStringParameter(PARAM_forwardType); 141 142 boolean isForwardInline; 143 if (fForwardType.equalsIgnoreCase("INLINE")) { 144 isForwardInline = true; 145 } else { 146 isForwardInline = false; 147 } 148 149 boolean quoteOrig = true; 150 151 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 152 153 try { 154 Message forwardMsg = iwMsg.forwardMessage(super.getImapWebSessionData().getImapSession(), 155 super.getImapWebSessionData().getImapStore(), 156 fFolder, fMsgid, isForwardInline); 157 158 composeHTML page = (composeHTML)m_comms.xmlcFactory.create(composeHTML.class); 159 160 Object obj = getComms().sessionData.get(FileUploadSessionData.SESSION_KEY); 162 if (obj != null) { 163 obj = null; 164 } 165 166 if (isForwardInline) { 167 ((HTMLInputElement)page.getElementOrigFolder()).setValue("-1"); 168 ((HTMLInputElement)page.getElementOrigMsgid()).setValue("-1"); 169 } else { 170 ((HTMLInputElement)page.getElementOrigFolder()).setValue(fFolder); 171 ((HTMLInputElement)page.getElementOrigMsgid()).setValue(Long.toString(fMsgid)); 172 } 173 174 page.getElementTo().setValue(InternetAddress.toString(forwardMsg.getAllRecipients())); 176 page.getElementSubject().setValue(forwardMsg.getSubject()); 178 179 HTMLTextAreaElement messageEle = page.getElementMessage(); 180 181 182 183 String quote = iwMsg.getMessageBody(forwardMsg); 184 185 186 187 188 189 if (XMLCUtil.findFirstText(messageEle) != null) { 190 XMLCUtil.findFirstText(messageEle).setData(quote); 191 } else { 192 messageEle.appendChild(page.createTextNode(quote)); 193 } 194 195 return page.toDocument(); 196 197 } catch(NullPointerException ex) { 198 202 return super.showCompletionPage(MSG_OPERATION_FAILED, 203 "You cannot forward massage while runing imapWeb_pres! That was a default HTML page", 204 "", ""); 205 } catch (MessagingException e) { 206 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 207 "", ""); 208 209 } catch (ImapWebException e) { 210 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 211 "", ""); 212 213 } catch (KeywordValueException e) { 214 return super.showErrorPage(MSG_OPERATION_FAILED, 215 "Problem getting session data from session. " + e.getMessage(), 216 "", ""); 217 } 218 } 219 220 private String reply(boolean replyAll) throws HttpPresentationException { 221 String fFolder = super.getStringParameter(PARAM_folder); 223 long fMsgid = super.getLongParameter(PARAM_msgid); 224 225 boolean quoteOrig = true; 226 227 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 228 229 try { 230 Message replyMsg = iwMsg.replyMessage(super.getImapWebSessionData().getImapSession(), 231 super.getImapWebSessionData().getImapStore(), 232 fFolder, fMsgid, replyAll, quoteOrig); 233 234 composeHTML page = (composeHTML)m_comms.xmlcFactory.create(composeHTML.class); 235 236 Object obj = getComms().sessionData.get(FileUploadSessionData.SESSION_KEY); 238 if (obj != null) { 239 obj = null; 240 } 241 242 page.getElementTo().setValue(InternetAddress.toString(replyMsg.getAllRecipients())); 244 page.getElementSubject().setValue(replyMsg.getSubject()); 246 247 if (quoteOrig) { 249 HTMLTextAreaElement messageEle = page.getElementMessage(); 250 251 252 String quote = iwMsg.getMessageBody(replyMsg); 253 254 255 if (XMLCUtil.findFirstText(messageEle) != null) { 256 XMLCUtil.findFirstText(messageEle).setData(quote); 257 } else { 258 messageEle.appendChild(page.createTextNode(quote)); 259 } 260 } 261 262 return page.toDocument(); 263 } catch(NullPointerException ex) { 264 268 return super.showCompletionPage(MSG_OPERATION_FAILED, 269 "You cannot reply massage while runing imapWeb_pres! That was a default HTML page", 270 "", ""); 271 272 } catch (MessagingException e) { 273 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 274 "", ""); 275 276 } catch (ImapWebException e) { 277 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 278 "", ""); 279 280 } catch (KeywordValueException e) { 281 return super.showErrorPage(MSG_OPERATION_FAILED, 282 "Problem getting session data from session. " + e.getMessage(), 283 "", ""); 284 } 285 } 286 287 private String delete() throws HttpPresentationException { 288 String fFolder = super.getStringParameter(PARAM_folder); 290 long[] fMsgidArr = super.getLongParameterArray(PARAM_msgid); 291 292 try { 293 294 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 295 296 297 iwMsg.deleteMessage(super.getImapWebSessionData().getImapStore(), 298 fFolder, fMsgidArr); 299 300 301 return super.showCompletionPage(MSG_OPERATION_COMPLETED, 302 "The selected messages has been deleted.", 303 "", ""); 304 } catch(NullPointerException ex) { 305 309 return super.showCompletionPage(MSG_OPERATION_FAILED, 310 "You cannot delete massage while runing imapWeb_pres! That was a default HTML page", 311 "", ""); 312 313 } catch (ImapWebException e) { 314 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 315 "", ""); 316 } 317 318 } 319 320 private String move() throws HttpPresentationException { 321 String fFolder = super.getStringParameter(PARAM_folder); 323 long[] fMsgidArr = super.getLongParameterArray(PARAM_msgid); 324 String fTargetFolder = super.getStringParameter(PARAM_targetFolder); 325 326 try { 327 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 328 329 iwMsg.moveMessage(super.getImapWebSessionData().getImapStore(), 330 fFolder, fMsgidArr, fTargetFolder); 331 332 333 return super.showCompletionPage(MSG_OPERATION_COMPLETED, 334 "The selected messages has been moved.", 335 "", ""); 336 } catch(NullPointerException ex) { 337 341 return super.showCompletionPage(MSG_OPERATION_FAILED, 342 "You cannot move massage while runing imapWeb_pres! That was a default HTML page", 343 "", ""); 344 } catch (ImapWebException e) { 345 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 346 "", ""); 347 } 348 } 349 350 private String downloadAttachment() throws HttpPresentationException { 351 String fFolder = super.getStringParameter(PARAM_folder); 353 long fMsgid = super.getLongParameter(PARAM_msgid); 354 String fPart = super.getStringParameter(PARAM_part); 355 356 try { 357 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 358 359 Part part = iwMsg.getMessagePart(super.getImapWebSessionData().getImapSession(), 360 super.getImapWebSessionData().getImapStore(), 361 fFolder, fMsgid, fPart); 362 363 InputStream is = part.getInputStream(); 365 366 byte[] buffer = new byte[1024]; 368 int length = -1; 369 370 this.getComms().response.setContentType(part.getContentType()); 371 this.getComms().response.setHeader("Content-disposition", 372 "filename=" + part.getFileName()); 373 374 while ((length = is.read(buffer)) != -1) { 376 this.getComms().response.getOutputStream().write(buffer, 0, length); 377 } 378 379 is.close(); 381 382 this.getComms().response.flush(); 384 385 return "?"; 386 387 } catch(NullPointerException ex) { 388 392 return super.showCompletionPage(MSG_OPERATION_FAILED, 393 "You cannot download attachment while runing imapWeb_pres! That was a default HTML page", 394 "", ""); 395 } catch (IOException e) { 396 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 397 "", ""); 398 } catch (MessagingException e) { 399 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 400 "", ""); 401 402 } catch (ImapWebException e) { 403 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 404 "", ""); 405 } 406 } 407 408 private String createFolder() throws HttpPresentationException { 409 String fName = super.getStringParameter(PARAM_name); 411 412 try { 413 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 414 415 iwMsg.createFolder(super.getImapWebSessionData().getImapStore(), 416 fName); 417 418 419 return super.showCompletionPage(MSG_OPERATION_COMPLETED, 420 "Folder created.","", ""); 421 } catch(NullPointerException ex) { 422 426 return super.showCompletionPage(MSG_OPERATION_FAILED, 427 "You cannot create folder while runing imapWeb_pres! That was a default HTML page", 428 "", ""); 429 } catch (ImapWebException e) { 430 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 431 "", ""); 432 } 433 } 434 435 private String renameFolder() throws HttpPresentationException { 436 String fFolder = super.getStringParameter(PARAM_folder); 438 String fName = super.getStringParameter(PARAM_name); 439 440 try { 441 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 442 443 iwMsg.renameFolder(super.getImapWebSessionData().getImapStore(), 444 fFolder, fName); 445 446 447 return super.showCompletionPage(MSG_OPERATION_COMPLETED, 448 "Folder renamed.", 449 "", ""); 450 } catch (ImapWebException e) { 451 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 452 "", ""); 453 } 454 } 455 456 private String removeFolder() throws HttpPresentationException { 457 String fFolder = super.getStringParameter(PARAM_folder); 459 460 try { 461 IWMessage iwMsg = IWMessageFactory.getIWMessage("scioworks.imap.business.beans.IWMessageImpl"); 462 463 iwMsg.removeFolder(super.getImapWebSessionData().getImapStore(), 464 fFolder); 465 466 467 return super.showCompletionPage(MSG_OPERATION_COMPLETED, 468 "Folder removed.","", ""); 469 } catch(NullPointerException ex) { 470 474 return super.showCompletionPage(MSG_OPERATION_FAILED, 475 "You cannot remove folder while runing imapWeb_pres! That was a default HTML page", 476 "", ""); 477 } catch (ImapWebException e) { 478 return super.showErrorPage(MSG_OPERATION_FAILED, e.getMessage(), 479 "", ""); 480 } 481 } 482 483 public String handleDefault() 484 throws HttpPresentationException { 485 486 String event = getStringParameter(PARAM_event); 487 String returnHTML = null; 488 489 if (event.equals(EVENT_send)) { 490 returnHTML = send(); 491 492 } else if (event.equals(EVENT_reply)) { 493 returnHTML = reply(false); 494 495 } else if (event.equals(EVENT_replyAll)) { 496 returnHTML = reply(true); 497 498 } else if (event.equals(EVENT_delete)) { 499 returnHTML = delete(); 500 501 } else if (event.equals(EVENT_forward)) { 502 returnHTML = forward(); 503 504 } else if (event.equals(EVENT_downloadAttachment)) { 505 returnHTML = downloadAttachment(); 506 507 } else if (event.equals(EVENT_move)) { 508 returnHTML = move(); 509 510 } else if (event.equals(EVENT_createFolder)) { 511 returnHTML = createFolder(); 512 513 } else if (event.equals(EVENT_renameFolder)) { 514 returnHTML = renameFolder(); 515 516 } else if (event.equals(EVENT_removeFolder)) { 517 returnHTML = removeFolder(); 518 519 } else { 520 return showErrorPage(ERROR_UNDEFINE_EVENT, 521 "Event " + event + " is undefined", 522 "", ""); 523 } 524 525 return returnHTML; 526 } 527 528 } | Popular Tags |