001: /* -----------------------------------------------------------------------------
002: * Copyright (c) 2001, Low Kin Onn
003: * All rights reserved.
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: *
008: * Redistributions of source code must retain the above copyright notice, this
009: * list of conditions and the following disclaimer.
010: *
011: * Redistributions in binary form must reproduce the above copyright notice,
012: * this list of conditions and the following disclaimer in the documentation
013: * and/or other materials provided with the distribution.
014: *
015: * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
016: * may beused to endorse or promote products derived from this software without
017: * specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
023: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
024: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
026: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
028: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * -----------------------------------------------------------------------------
031: */
032:
033: package scioworks.imap.presentation.imapWeb;
034:
035: import java.util.Vector;
036: import java.io.*;
037:
038: import javax.mail.*;
039: import javax.mail.internet.*;
040:
041: import org.w3c.dom.*;
042: import org.w3c.dom.html.*;
043: import org.enhydra.xml.xmlc.XMLCUtil;
044:
045: import com.lutris.appserver.server.httpPresentation.*;
046: import com.lutris.util.KeywordValueException;
047:
048: import scioworks.imap.presentation.base.*;
049: import scioworks.imap.presentation.imapWeb.*;
050: import scioworks.imap.spec.beans.*;
051: import scioworks.imap.spec.ImapWebException;
052: import scioworks.imap.spec.ImapWebConstant;
053:
054: public class mailAction extends BasePO {
055:
056: private String send() throws HttpPresentationException {
057: // get input parameters
058: String fTo = super .getStringParameter(PARAM_to);
059: String fSubject = super .getStringParameter(PARAM_subject);
060: String fCc = super .getStringParameter(PARAM_cc);
061: String fBcc = super .getStringParameter(PARAM_bcc);
062: String fMessage = super .getStringParameter(PARAM_message);
063: String[] fSaveCopy = super
064: .getStringParameterArray(PARAM_savecopy);
065: String fOrigFolder = super .getStringParameter(PARAM_origFolder);
066: long fOrigMsgid = super .getLongParameter(PARAM_origMsgid);
067:
068: boolean saveCopy = (fSaveCopy.length != 0);
069:
070: boolean isForwardAttach;
071: if (fOrigMsgid != -1) {
072: isForwardAttach = true;
073: } else {
074: isForwardAttach = false;
075: }
076:
077: IWMessage iwMsg = IWMessageFactory
078: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
079:
080: try {
081:
082: Object obj = super .getComms().sessionData
083: .get(FileUploadSessionData.SESSION_KEY);
084: Vector attachments;
085: if (obj != null) {
086: attachments = ((FileUploadSessionData) obj)
087: .getFileList();
088:
089: } else {
090: // create empty attachment vector
091: attachments = new Vector();
092: }
093:
094: iwMsg.sendMessage(super .getImapWebSessionData()
095: .getImapStore(), super .getImapWebSessionData()
096: .getImapSession(), super .getImapWebSessionData()
097: .getImapURL(),
098: ImapWebConstant.singleton().domain(), fTo,
099: fSubject, fCc, fBcc, fMessage, attachments, true, // send as inline attachments for now
100: saveCopy, isForwardAttach, fOrigFolder, fOrigMsgid);
101:
102: // remove the attachment for the session data
103: if (obj != null) {
104: // delete the tmp files
105: String tmpFilename;
106: for (int i = 0; i < attachments.size(); i++) {
107: tmpFilename = ((FileUploadSessionData) obj)
108: .getTmpFilename(i);
109: File file = new File(tmpFilename);
110: if (!file.delete()) {
111: System.err.println("Cannot delete tmp file: "
112: + tmpFilename);
113: }
114: }
115: super .getComms().sessionData
116: .remove(FileUploadSessionData.SESSION_KEY);
117: }
118:
119: /** @todo : set target url */
120: return showCompletionPage(MSG_OPERATION_COMPLETED,
121: "Your email has been sent.", "", "");
122: } catch (NullPointerException ex) {
123: /*
124: * The response will be default HTML page
125: * We need to allow imapWeb_pres to be functional
126: */
127: return super
128: .showCompletionPage(
129: MSG_OPERATION_FAILED,
130: "You cannot send massage while runing imapWeb_pres! That was a default HTML page",
131: "", "");
132: } catch (ImapWebException e) {
133: // error, show message
134: return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
135: "", "");
136:
137: } catch (KeywordValueException e) {
138: return showErrorPage(MSG_OPERATION_FAILED, e.getMessage(),
139: "", "");
140: }
141: }
142:
143: private String forward() throws HttpPresentationException {
144: // get input parameters
145: String fFolder = super .getStringParameter(PARAM_folder);
146: long fMsgid = super .getLongParameter(PARAM_msgid);
147: String fForwardType = super
148: .getStringParameter(PARAM_forwardType);
149:
150: boolean isForwardInline;
151: if (fForwardType.equalsIgnoreCase("INLINE")) {
152: isForwardInline = true;
153: } else {
154: isForwardInline = false;
155: }
156:
157: boolean quoteOrig = true;
158:
159: IWMessage iwMsg = IWMessageFactory
160: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
161:
162: try {
163: Message forwardMsg = iwMsg.forwardMessage(super
164: .getImapWebSessionData().getImapSession(), super
165: .getImapWebSessionData().getImapStore(), fFolder,
166: fMsgid, isForwardInline);
167:
168: composeHTML page = (composeHTML) m_comms.xmlcFactory
169: .create(composeHTML.class);
170:
171: // reset the session data
172: Object obj = getComms().sessionData
173: .get(FileUploadSessionData.SESSION_KEY);
174: if (obj != null) {
175: obj = null;
176: }
177:
178: if (isForwardInline) {
179: ((HTMLInputElement) page.getElementOrigFolder())
180: .setValue("-1");
181: ((HTMLInputElement) page.getElementOrigMsgid())
182: .setValue("-1");
183: } else {
184: ((HTMLInputElement) page.getElementOrigFolder())
185: .setValue(fFolder);
186: ((HTMLInputElement) page.getElementOrigMsgid())
187: .setValue(Long.toString(fMsgid));
188: }
189:
190: // set the 'to'
191: page.getElementTo().setValue(
192: InternetAddress.toString(forwardMsg
193: .getAllRecipients()));
194: // set the 'subject'
195: page.getElementSubject().setValue(forwardMsg.getSubject());
196:
197: HTMLTextAreaElement messageEle = page.getElementMessage();
198:
199: String quote = iwMsg.getMessageBody(forwardMsg);
200:
201: if (XMLCUtil.findFirstText(messageEle) != null) {
202: XMLCUtil.findFirstText(messageEle).setData(quote);
203: } else {
204: messageEle.appendChild(page.createTextNode(quote));
205: }
206:
207: return page.toDocument();
208:
209: } catch (NullPointerException ex) {
210: /*
211: * The response will be default HTML page
212: * We need to allow imapWeb_pres to be functional
213: */
214: return super
215: .showCompletionPage(
216: MSG_OPERATION_FAILED,
217: "You cannot forward massage while runing imapWeb_pres! That was a default HTML page",
218: "", "");
219: } catch (MessagingException e) {
220: return super .showErrorPage(MSG_OPERATION_FAILED, e
221: .getMessage(), "", "");
222:
223: } catch (ImapWebException e) {
224: return super .showErrorPage(MSG_OPERATION_FAILED, e
225: .getMessage(), "", "");
226:
227: } catch (KeywordValueException e) {
228: return super .showErrorPage(MSG_OPERATION_FAILED,
229: "Problem getting session data from session. "
230: + e.getMessage(), "", "");
231: }
232: }
233:
234: private String reply(boolean replyAll)
235: throws HttpPresentationException {
236: // get input parameters
237: String fFolder = super .getStringParameter(PARAM_folder);
238: long fMsgid = super .getLongParameter(PARAM_msgid);
239:
240: boolean quoteOrig = true;
241:
242: IWMessage iwMsg = IWMessageFactory
243: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
244:
245: try {
246: Message replyMsg = iwMsg.replyMessage(super
247: .getImapWebSessionData().getImapSession(), super
248: .getImapWebSessionData().getImapStore(), fFolder,
249: fMsgid, replyAll, quoteOrig);
250:
251: composeHTML page = (composeHTML) m_comms.xmlcFactory
252: .create(composeHTML.class);
253:
254: // reset the session data
255: Object obj = getComms().sessionData
256: .get(FileUploadSessionData.SESSION_KEY);
257: if (obj != null) {
258: obj = null;
259: }
260:
261: // set the 'to'
262: page.getElementTo().setValue(
263: InternetAddress.toString(replyMsg
264: .getAllRecipients()));
265: // set the 'subject'
266: page.getElementSubject().setValue(replyMsg.getSubject());
267:
268: // set the message box, if quoteOrig is true
269: if (quoteOrig) {
270: HTMLTextAreaElement messageEle = page
271: .getElementMessage();
272:
273: String quote = iwMsg.getMessageBody(replyMsg);
274:
275: if (XMLCUtil.findFirstText(messageEle) != null) {
276: XMLCUtil.findFirstText(messageEle).setData(quote);
277: } else {
278: messageEle.appendChild(page.createTextNode(quote));
279: }
280: }
281:
282: return page.toDocument();
283: } catch (NullPointerException ex) {
284: /*
285: * The response will be default HTML page
286: * We need to allow imapWeb_pres to be functional
287: */
288: return super
289: .showCompletionPage(
290: MSG_OPERATION_FAILED,
291: "You cannot reply massage while runing imapWeb_pres! That was a default HTML page",
292: "", "");
293:
294: } catch (MessagingException e) {
295: return super .showErrorPage(MSG_OPERATION_FAILED, e
296: .getMessage(), "", "");
297:
298: } catch (ImapWebException e) {
299: return super .showErrorPage(MSG_OPERATION_FAILED, e
300: .getMessage(), "", "");
301:
302: } catch (KeywordValueException e) {
303: return super .showErrorPage(MSG_OPERATION_FAILED,
304: "Problem getting session data from session. "
305: + e.getMessage(), "", "");
306: }
307: }
308:
309: private String delete() throws HttpPresentationException {
310: // get input parameters
311: String fFolder = super .getStringParameter(PARAM_folder);
312: long[] fMsgidArr = super .getLongParameterArray(PARAM_msgid);
313:
314: try {
315:
316: IWMessage iwMsg = IWMessageFactory
317: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
318:
319: iwMsg.deleteMessage(super .getImapWebSessionData()
320: .getImapStore(), fFolder, fMsgidArr);
321:
322: /** @todo : set target url */
323: return super .showCompletionPage(MSG_OPERATION_COMPLETED,
324: "The selected messages has been deleted.", "", "");
325: } catch (NullPointerException ex) {
326: /*
327: * The response will be default HTML page
328: * We need to allow imapWeb_pres to be functional
329: */
330: return super
331: .showCompletionPage(
332: MSG_OPERATION_FAILED,
333: "You cannot delete massage while runing imapWeb_pres! That was a default HTML page",
334: "", "");
335:
336: } catch (ImapWebException e) {
337: return super .showErrorPage(MSG_OPERATION_FAILED, e
338: .getMessage(), "", "");
339: }
340:
341: }
342:
343: private String move() throws HttpPresentationException {
344: // get input parameters
345: String fFolder = super .getStringParameter(PARAM_folder);
346: long[] fMsgidArr = super .getLongParameterArray(PARAM_msgid);
347: String fTargetFolder = super
348: .getStringParameter(PARAM_targetFolder);
349:
350: try {
351: IWMessage iwMsg = IWMessageFactory
352: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
353:
354: iwMsg.moveMessage(super .getImapWebSessionData()
355: .getImapStore(), fFolder, fMsgidArr, fTargetFolder);
356:
357: /** @todo : set target url */
358: return super .showCompletionPage(MSG_OPERATION_COMPLETED,
359: "The selected messages has been moved.", "", "");
360: } catch (NullPointerException ex) {
361: /*
362: * The response will be default HTML page
363: * We need to allow imapWeb_pres to be functional
364: */
365: return super
366: .showCompletionPage(
367: MSG_OPERATION_FAILED,
368: "You cannot move massage while runing imapWeb_pres! That was a default HTML page",
369: "", "");
370: } catch (ImapWebException e) {
371: return super .showErrorPage(MSG_OPERATION_FAILED, e
372: .getMessage(), "", "");
373: }
374: }
375:
376: private String downloadAttachment()
377: throws HttpPresentationException {
378: // get input parameters
379: String fFolder = super .getStringParameter(PARAM_folder);
380: long fMsgid = super .getLongParameter(PARAM_msgid);
381: String fPart = super .getStringParameter(PARAM_part);
382:
383: try {
384: IWMessage iwMsg = IWMessageFactory
385: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
386:
387: Part part = iwMsg.getMessagePart(super
388: .getImapWebSessionData().getImapSession(), super
389: .getImapWebSessionData().getImapStore(), fFolder,
390: fMsgid, fPart);
391:
392: // get binary stream to retrieve BLOB data
393: InputStream is = part.getInputStream();
394:
395: // Get the buffer size suggested by JDBC
396: byte[] buffer = new byte[1024];
397: int length = -1;
398:
399: this .getComms().response.setContentType(part
400: .getContentType());
401: this .getComms().response.setHeader("Content-disposition",
402: "filename=" + part.getFileName());
403:
404: // Pipe the input stream to output stream
405: while ((length = is.read(buffer)) != -1) {
406: this .getComms().response.getOutputStream().write(
407: buffer, 0, length);
408: }
409:
410: // Close input stream
411: is.close();
412:
413: // Flush the output
414: this .getComms().response.flush();
415:
416: return "?";
417:
418: } catch (NullPointerException ex) {
419: /*
420: * The response will be default HTML page
421: * We need to allow imapWeb_pres to be functional
422: */
423: return super
424: .showCompletionPage(
425: MSG_OPERATION_FAILED,
426: "You cannot download attachment while runing imapWeb_pres! That was a default HTML page",
427: "", "");
428: } catch (IOException e) {
429: return super .showErrorPage(MSG_OPERATION_FAILED, e
430: .getMessage(), "", "");
431: } catch (MessagingException e) {
432: return super .showErrorPage(MSG_OPERATION_FAILED, e
433: .getMessage(), "", "");
434:
435: } catch (ImapWebException e) {
436: return super .showErrorPage(MSG_OPERATION_FAILED, e
437: .getMessage(), "", "");
438: }
439: }
440:
441: private String createFolder() throws HttpPresentationException {
442: // get input parameter
443: String fName = super .getStringParameter(PARAM_name);
444:
445: try {
446: IWMessage iwMsg = IWMessageFactory
447: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
448:
449: iwMsg.createFolder(super .getImapWebSessionData()
450: .getImapStore(), fName);
451:
452: /** @todo : set target url */
453: return super .showCompletionPage(MSG_OPERATION_COMPLETED,
454: "Folder created.", "", "");
455: } catch (NullPointerException ex) {
456: /*
457: * The response will be default HTML page
458: * We need to allow imapWeb_pres to be functional
459: */
460: return super
461: .showCompletionPage(
462: MSG_OPERATION_FAILED,
463: "You cannot create folder while runing imapWeb_pres! That was a default HTML page",
464: "", "");
465: } catch (ImapWebException e) {
466: return super .showErrorPage(MSG_OPERATION_FAILED, e
467: .getMessage(), "", "");
468: }
469: }
470:
471: private String renameFolder() throws HttpPresentationException {
472: // get input parameter
473: String fFolder = super .getStringParameter(PARAM_folder);
474: String fName = super .getStringParameter(PARAM_name);
475:
476: try {
477: IWMessage iwMsg = IWMessageFactory
478: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
479:
480: iwMsg.renameFolder(super .getImapWebSessionData()
481: .getImapStore(), fFolder, fName);
482:
483: /** @todo : set target url */
484: return super .showCompletionPage(MSG_OPERATION_COMPLETED,
485: "Folder renamed.", "", "");
486: } catch (ImapWebException e) {
487: return super .showErrorPage(MSG_OPERATION_FAILED, e
488: .getMessage(), "", "");
489: }
490: }
491:
492: private String removeFolder() throws HttpPresentationException {
493: // get input parameter
494: String fFolder = super .getStringParameter(PARAM_folder);
495:
496: try {
497: IWMessage iwMsg = IWMessageFactory
498: .getIWMessage("scioworks.imap.business.beans.IWMessageImpl");
499:
500: iwMsg.removeFolder(super .getImapWebSessionData()
501: .getImapStore(), fFolder);
502:
503: /** @todo : set target url */
504: return super .showCompletionPage(MSG_OPERATION_COMPLETED,
505: "Folder removed.", "", "");
506: } catch (NullPointerException ex) {
507: /*
508: * The response will be default HTML page
509: * We need to allow imapWeb_pres to be functional
510: */
511: return super
512: .showCompletionPage(
513: MSG_OPERATION_FAILED,
514: "You cannot remove folder while runing imapWeb_pres! That was a default HTML page",
515: "", "");
516: } catch (ImapWebException e) {
517: return super .showErrorPage(MSG_OPERATION_FAILED, e
518: .getMessage(), "", "");
519: }
520: }
521:
522: public String handleDefault() throws HttpPresentationException {
523:
524: String event = getStringParameter(PARAM_event);
525: String returnHTML = null;
526:
527: if (event.equals(EVENT_send)) {
528: returnHTML = send();
529:
530: } else if (event.equals(EVENT_reply)) {
531: returnHTML = reply(false);
532:
533: } else if (event.equals(EVENT_replyAll)) {
534: returnHTML = reply(true);
535:
536: } else if (event.equals(EVENT_delete)) {
537: returnHTML = delete();
538:
539: } else if (event.equals(EVENT_forward)) {
540: returnHTML = forward();
541:
542: } else if (event.equals(EVENT_downloadAttachment)) {
543: returnHTML = downloadAttachment();
544:
545: } else if (event.equals(EVENT_move)) {
546: returnHTML = move();
547:
548: } else if (event.equals(EVENT_createFolder)) {
549: returnHTML = createFolder();
550:
551: } else if (event.equals(EVENT_renameFolder)) {
552: returnHTML = renameFolder();
553:
554: } else if (event.equals(EVENT_removeFolder)) {
555: returnHTML = removeFolder();
556:
557: } else {
558: return showErrorPage(ERROR_UNDEFINE_EVENT, "Event " + event
559: + " is undefined", "", "");
560: }
561:
562: return returnHTML;
563: }
564:
565: }
|