001: /*
002: * SendMessageTag
003: * @(#)SendMessageTag.java 1.3 04/01/01 Sun Microsystems
004: *
005: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
006: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
007: */
008:
009: package com.sun.portal.wireless.taglibs.mail;
010:
011: import javax.servlet.jsp.*;
012: import javax.servlet.jsp.tagext.*;
013:
014: import javax.mail.*;
015: import javax.mail.internet.*;
016: import javax.mail.search.*;
017: import java.util.Date;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import java.util.*;
021:
022: import com.sun.portal.wireless.taglibs.base.*;
023: import com.sun.portal.log.common.PortalLogger;
024:
025: /**
026: *
027: * @version 1.0
028: */
029: public class SendMessageTag extends CommandTag {
030: private static Logger logger = PortalLogger
031: .getLogger(SendMessageTag.class);
032:
033: private String msgName = null;
034: private String fwdIdx = null;
035: private String replyIdx = null;
036: static final String delim = "\n\n --------------------------------------------------------\n\n";
037:
038: public boolean execute() throws JspException {
039:
040: MailContext mc = null;
041: try {
042: mc = MailContext.getContext(pageContext);
043: MsgBean msg = (MsgBean) pageContext.getAttribute(msgName);
044:
045: if (fwdIdx != null || replyIdx != null) {
046:
047: String idx = null;
048:
049: if (fwdIdx != null) {
050: idx = fwdIdx;
051: } else {
052: idx = replyIdx;
053: }
054:
055: int msgIdx = 0;
056: msgIdx = Integer.parseInt(idx);
057: MsgBean[] msgs = mc.getMsgsCollection();
058: MsgBean tmpMsg = msgs[msgIdx];
059:
060: if (fwdIdx != null) {
061: // forwarding tmpMsg;
062:
063: String oldText = msg.getText();
064: String fwdText = tmpMsg.getText();
065: String plainfwdText = fwdText.substring(0, 4);
066: if (plainfwdText.equals("<Aml")) {
067: msg.setText(oldText);
068: } else {
069: msg.setText(oldText + delim + tmpMsg.getText());
070: }
071:
072: if (tmpMsg.isHasattachments()) {
073: msg.setAttachments(tmpMsg.getAttachments());
074: }
075: } else if (replyIdx != null) {
076: if (mc.includeMsg()) {
077: String orgText = msg.getText();
078: // TODO: should make "> " configurable
079: String replyText = tmpMsg.getReplyText("> ");
080: String plainreplyText = replyText.substring(0,
081: 4);
082: if (plainreplyText.equals("> <A")) {
083: msg.setText(orgText);
084: } else {
085: msg.setText(orgText + "\n" + replyText);
086: }
087:
088: if (tmpMsg.isHasattachments()) {
089: //For Bug
090: Vector htmlAttachments = tmpMsg
091: .getAttachments();
092: Vector replyAttachments = new Vector();
093: int vectorsize = htmlAttachments.size();
094: for (int i = 0; i < vectorsize; i++) {
095: AttachmentBean rattach = (AttachmentBean) htmlAttachments
096: .elementAt(i);
097: String contentType = rattach
098: .getContentType();
099: String type = contentType.substring(0,
100: 4);
101: if ((type.equals("TEXT"))) {
102: replyAttachments
103: .addElement(rattach);
104: }
105: // ContentType type = new ContentType(p.getContentType())
106: }
107: // msg.setAttachments(tmpMsg.getAttachments());
108: msg.setAttachments(replyAttachments);
109: }
110: }
111: }
112:
113: }
114:
115: if (mc.includeSig()) {
116: String orgText = msg.getText();
117: msg.setText(orgText + "\n\n" + mc.getSignature());
118: }
119:
120: // send msg in its current state
121: msg.send();
122:
123: if (mc.saveSentMail()) {
124: try {
125: Store mailStore = mc.getMailStore();
126: Folder sentMail = mailStore.getFolder("Sent");
127:
128: if (!sentMail.exists()) {
129: sentMail.create(Folder.HOLDS_MESSAGES);
130: }
131:
132: Date date = new Date();
133: Message sMsg[] = new Message[1];
134: sMsg[0] = msg.getObject();
135: sMsg[0].setHeader("Date", date.toString());
136: sentMail.appendMessages(sMsg);
137: } catch (Exception e) {
138: // don't care what the error was
139: }
140: }
141:
142: release();
143: return true;
144: } catch (SendFailedException sfe) {
145: logger.log(Level.FINER, "PSMA_CSPWTM0035", sfe);
146: release();
147: mc.setErrorCode("MAIL_001"); // MAIL_001 = Wrong email address
148: return false;
149: } catch (MessagingException me) {
150: logger.log(Level.FINER, "PSMA_CSPWTM0036", me);
151: release();
152: mc.setErrorCode("MAIL_002"); // MAIL_002 = generic message sent failed
153: return false;
154: } catch (Exception e) {
155: //log("Exception caught in SendMessageTag", e);
156: logger.log(Level.FINER, "PSMA_CSPWTM0037", e);
157: release();
158: mc.setErrorCode("");
159: return false;
160: }
161: }
162:
163: public void release() {
164: super .release();
165: msgName = null;
166: fwdIdx = null;
167: replyIdx = null;
168: }
169:
170: public void setMsgname(String msgName) {
171: this .msgName = evalAttribute(msgName);
172: }
173:
174: public void setFwdindex(String idx) {
175: fwdIdx = evalAttribute(idx);
176: }
177:
178: public void setReplyindex(String idx) {
179: replyIdx = evalAttribute(idx);
180: }
181: }
|