KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > email > EmailPortlet


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 // Java stuff
32
import java.util.Vector JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 import java.util.List JavaDoc;
36 import java.util.Enumeration JavaDoc;
37
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41
42 //JavaMail
43

44 import javax.mail.Folder JavaDoc;
45 import javax.mail.AuthenticationFailedException JavaDoc;
46 import javax.mail.NoSuchProviderException JavaDoc;
47 import javax.mail.MessagingException JavaDoc;
48 import javax.mail.Message JavaDoc;
49 import javax.mail.Multipart JavaDoc;
50 import javax.mail.Part JavaDoc;
51 import javax.mail.Header JavaDoc;
52
53 // upload download to db
54
import javax.servlet.http.HttpServletResponse JavaDoc;
55
56 // email stuff
57
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 /**
62  * Email Action
63  *
64  * @author <a HREF="mailto:jlim@gluecode.com">Jonas Lim </a>
65  * @version $Id: EmailPortlet.java,v 1.2 2004/03/22 22:26:58 taylor Exp $
66  */

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     /**
77      * Subclasses should override this method if they wish to build specific
78      * content when maximized. Default behavior is to do the same as normal
79      * content.
80      */

81     protected void buildMaximizedContext(VelocityPortlet portlet,
82             Context context, RunData rundata) throws Exception JavaDoc
83     {
84         buildNormalContext(portlet, context, rundata);
85
86     }
87
88     protected static final String JavaDoc CUSTOMIZE_TEMPLATE = "customizeTemplate";
89
90     /**
91      * Subclasses should override this method if they wish to provide their own
92      * customization behavior. Default is to use Portal base customizer action
93      */

94     protected void buildConfigureContext(VelocityPortlet portlet,
95             Context context, RunData rundata)
96     {
97         try
98         {
99             super.buildConfigureContext(portlet, context, rundata);
100         } catch (Exception JavaDoc ex)
101         {
102             log.error("Exception", ex);
103         }
104         String JavaDoc template = PortletConfigState.getParameter(portlet, rundata,
105                 CUSTOMIZE_TEMPLATE, null);
106         setTemplate(rundata, template);
107     }
108
109     /**
110      * Subclasses must override this method to provide default behavior for the
111      * portlet action
112      */

113     protected void buildNormalContext(VelocityPortlet portlet, Context context,
114             RunData rundata) throws Exception JavaDoc
115     {
116
117         
118         Hashtable JavaDoc userInfo = this.getEmailUserInfo(rundata, context);
119         Email email = null;
120
121         log.info("BuildNormalContext in EmailPortlet");
122         String JavaDoc host = getPortletParameter(rundata, context, "hostname");
123         context.put("host", host);
124
125         String JavaDoc user = (String JavaDoc) userInfo.get("username");
126         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
127         String JavaDoc emailAdd = (String JavaDoc) userInfo.get("email");
128
129         context.put("emailAdd", emailAdd);
130         String JavaDoc message_prompt = (String JavaDoc)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             //email.authenticateUser(user, pass);
144
context.put("hasLoggedIn", "yes");
145         } catch (AuthenticationFailedException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc showNumNewmessages = (String JavaDoc)getPortletSession(rundata, context,
170                 "showNumNewmessages");
171
172         if (showNumNewmessages == null || showNumNewmessages.equals(""))
173         { // get the number of new messages
174
// get the username and password in case the user did signout and
175
// login again using another username and password..
176

177             String JavaDoc protocol = getPortletParameter(rundata, context, "protocol");
178             context.put("protocol", protocol);
179
180             int numNewmessages = email.num_Newmessages();
181
182             String JavaDoc showInbox = (String JavaDoc)getPortletSession(rundata, context,
183                     "showInbox");
184
185             log.info("showInbox " + showInbox);
186             if (showInbox == null || showInbox.equals(""))// for initial load
187
// only
188
{
189                 if (numNewmessages != 0)
190                 {
191                     context.put("numNewmessages", String
192                             .valueOf(numNewmessages)); // displays
193
// (numNewmessages)the
194
// number of new
195
// messages
196
}
197             }
198             context.put("num_Newmessages", String.valueOf(numNewmessages)); // displays
199
// no.
200
// of
201
// new
202
// messages
203
// beside
204
// Inbox(num_Newmessages)
205
setPortletSession(rundata, context, "showNumNewmessages", "yes");
206             setPortletSession(rundata, context, "numNewmessages", String
207                     .valueOf(numNewmessages));
208
209         } else if (showNumNewmessages.equals("yes"))
210         {
211             String JavaDoc num_Newmessages = (String JavaDoc)getPortletSession(rundata, context,
212                     "numNewmessages");
213             context.put("num_Newmessages", num_Newmessages);
214         }
215
216         String JavaDoc compose = (String JavaDoc)getPortletSession(rundata, context, "compose");
217         context.put("compose", compose);
218
219         String JavaDoc showInbox = (String JavaDoc)getPortletSession(rundata, context, "showInbox");
220         context.put("showInbox", showInbox);
221
222         String JavaDoc showContent = (String JavaDoc)getPortletSession(rundata, context,
223                 "showContent");
224         context.put("showContent", showContent);
225
226         String JavaDoc createfolder = (String JavaDoc)getPortletSession(rundata, context,
227                 "createfolder");
228         context.put("createfolder", createfolder);
229
230         String JavaDoc showFolders = (String JavaDoc)getPortletSession(rundata, context,
231                 "showFolders");
232         context.put("showFolders", showFolders);
233
234         //get folder name
235
String JavaDoc protocol = (String JavaDoc)getPortletParameter(rundata, context, "protocol");
236         if (protocol.equals("imap"))
237         {
238             String JavaDoc folder_name = (String JavaDoc)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             //messages on each folder
250
String JavaDoc showmessagefolder = (String JavaDoc)getPortletSession(rundata, context,
251                     "showmessagefolder");
252             context.put("showmessagefolder", showmessagefolder);
253         }
254
255         String JavaDoc inboxMessages = (String JavaDoc)getPortletSession(rundata, context,
256                 "inboxMessages");
257
258         checkMessages(rundata, context,email);
259
260         String JavaDoc msgeContent = (String JavaDoc)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             //need to get the value of inContent to be able to retrieve the
270
// fields in reply form...
271
Vector JavaDoc inContent = (Vector JavaDoc) 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 JavaDoc reply = (String JavaDoc)getPortletSession(rundata, context, "reply");
282         context.put("reply", reply);
283         String JavaDoc forward = (String JavaDoc)getPortletSession(rundata, context, "forward");
284         context.put("forward", forward);
285         String JavaDoc rsubject = (String JavaDoc)getPortletSession(rundata, context, "rsubject");
286         context.put("rsubject", rsubject);
287
288         String JavaDoc msg = (String JavaDoc)getPortletSession(rundata, context, "msg");
289         context.put("msg", msg);
290
291         //multiple pages
292

293         String JavaDoc msgctr = (String JavaDoc)getPortletSession(rundata, context, "msgcount");
294         String JavaDoc msgcount = "";
295         if (msgctr != null && !msgctr.equals(""))
296         {
297             Integer JavaDoc imsgctr = new Integer JavaDoc(msgctr);
298             context.put("msgcount", imsgctr);
299         } else
300         {
301             context.put("msgcount", "");
302         }
303
304         String JavaDoc pages = (String JavaDoc)getPortletSession(rundata, context,
305                 "total_no_of_pages");
306         String JavaDoc total_no_of_pages = "";
307         if (pages != null && !pages.equals(""))
308         {
309             Integer JavaDoc ipages = new Integer JavaDoc(pages);
310             context.put("total_no_of_pages", ipages);
311         } else
312         {
313             context.put("total_no_of_pages", "");
314         }
315
316         String JavaDoc cpage = (String JavaDoc)getPortletSession(rundata, context, "cur_page");
317         String JavaDoc cur_page = "";
318         if (cpage != null && !cpage.equals(""))
319         {
320             Integer JavaDoc icur_page = new Integer JavaDoc(cpage);
321             context.put("cur_page", icur_page);
322         } else
323         {
324             context.put("cur_page", "");
325         }
326
327         String JavaDoc in_index = (String JavaDoc)getPortletSession(rundata, context, "start_index");
328         String JavaDoc start_index = "";
329         if (in_index != null && !in_index.equals(""))
330         {
331             Integer JavaDoc iin_index = new Integer JavaDoc(in_index);
332             context.put("start_index", iin_index);
333         } else
334         {
335             context.put("start_index", "");
336         }
337
338         String JavaDoc cRange = (String JavaDoc)getPortletSession(rundata, context, "range_per_page");
339         String JavaDoc range_per_page = "";
340         if (cRange != null && !cRange.equals(""))
341         {
342             Integer JavaDoc irange_per_page = new Integer JavaDoc(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 JavaDoc vFolders = (Vector JavaDoc) email.allFolders();
354             if (vFolders != null)
355             {
356
357                 context.put("vFolders", vFolders);
358             }
359
360             // vector of messages in one folder
361
Vector JavaDoc message_folder = (Vector JavaDoc) 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     }//end of buildNormalcontext
374

375     public void doAuth(RunData data, Context context)
376     {
377         log.info("doAuth in emailportlet");
378         Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
379
380         String JavaDoc user = (String JavaDoc) userInfo.get("username");
381         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
382
383         try
384         {
385             Email email = new Email(user, pass, getPortletParameters(data,
386                     context));
387             //log.info("after email");
388
setPortletSession(data, context, "showFolders", "yes");
389             setPortletSession(data, context, "hasLoggedIn", "yes");
390             //always recheck the no. of new messages
391
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 JavaDoc ae)
397         {
398             String JavaDoc message_prompt = "Authentication Failed... Bad LogIn.";
399             setPortletSession(data, context, "hasLoggedIn", "no");
400             setPortletSession(data, context, "message_prompt", message_prompt);
401         } catch (NoSuchProviderException JavaDoc np)
402         {
403             String JavaDoc 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 JavaDoc e)
407         {
408             String JavaDoc message_prompt = e.getMessage();
409             setPortletSession(data, context, "message_prompt", message_prompt);
410             log.info("error : " + e.getMessage());
411         }
412
413         //checkMessages(data, user, pass);
414
}
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 JavaDoc
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     //check if it's a new message
447
public int checkNewmessage(Message JavaDoc message[], int current_index)
448             throws Exception JavaDoc
449     {
450         log.info("### check new message");
451
452         //get message Id to know if it's a new message
453
String JavaDoc msgeId = getMessageId(message[current_index]);
454
455         Criteria cr = new Criteria();
456         cr.add(EmailInboxPeer.MESSAGE_ID, msgeId);
457
458         List JavaDoc vMsge = EmailInboxPeer.doSelect(cr); //tdk2.2 version
459
if (vMsge.isEmpty())
460         { // message id not found in db
461
return 1;
462         } else
463         { // message found in db but flag is set to 0
464

465             EmailInbox email = (EmailInbox) EmailInboxPeer.doSelect(cr).get(0);
466             //tdk2.2 version
467
if (email.getReadflag() == 0)
468             {
469                 return 1; // return 1 if new
470
} else
471                 return 0;
472         }
473     }
474
475     public void doSend(RunData data, Context context)
476     {
477         try
478         {
479             Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
480
481             log.info("do send in email portlet");
482             String JavaDoc user = (String JavaDoc) userInfo.get("username");
483             String JavaDoc pass = (String JavaDoc) userInfo.get("password");
484
485             String JavaDoc addressTo = data.getParameters().getString("addressTo");
486             String JavaDoc addressFrom = data.getParameters().getString("addressFrom");
487             setPortletSession(data, context, "addressFrom", addressFrom);
488
489             String JavaDoc subject = data.getParameters().getString("subject");
490             String JavaDoc 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 JavaDoc message_prompt = "Message Sent";
499             setPortletSession(data, context, "message_prompt", message_prompt);
500             setPortletSession(data, context, "msgeIndex", null);
501
502         } catch (Exception JavaDoc 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 JavaDoc
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 JavaDoc h = (Hashtable JavaDoc) getPortletSession(data, context, "hcontent");
521
522         //get the subject
523
String JavaDoc subject = (String JavaDoc) h.get("Subject").toString();
524
525         //get the original message
526
String JavaDoc msg = (String JavaDoc) 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         //check if subject has a prefix "Re:"
532
//if subject doesn't have a prefix "Re:", default as Re:Subject
533
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 JavaDoc recipient = null;
554
555         try
556         {
557             log.info("doSendReply");
558
559             Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
560             String JavaDoc user = (String JavaDoc) userInfo.get("username");
561             String JavaDoc pass = (String JavaDoc) userInfo.get("password");
562             String JavaDoc emailAdd = (String JavaDoc) userInfo.get("email");
563
564             String JavaDoc index = (String JavaDoc)getPortletSession(data, context, "index");
565
566             int current_index = Integer.parseInt(index);
567             log.info("index" + current_index);
568
569             String JavaDoc msg = (String JavaDoc) data.getParameters().getString("msg");
570             log.info("reply message &&&&&&&&&&&&&&&& " + msg);
571             setPortletSession(data, context, "msg", msg);
572             //set the value of from
573

574             String JavaDoc from = data.getParameters().getString("addressFrom");
575
576             //set the value of addresTo
577
//this will allow the user to change into new recipient
578
String JavaDoc addressTo = data.getParameters().getString("addressTo");
579
580             //set the value of subject
581
String JavaDoc subject = data.getParameters().getString("subject");
582
583             //set message
584
msg = data.getParameters().getString("msg");
585
586             String JavaDoc newmsge = checkFormat(msg);
587             setPortletSession(data, context, "msg", newmsge);
588             //check if the subject is not empty, else default as "none"
589
if (subject.equals(""))
590             {
591                 subject = "none";
592             }
593
594             //if subject doesn't have a prefix "Re:"
595
// set the subject with prefix "Re:"
596
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 JavaDoc newSubj = (String JavaDoc)getPortletSession(data, context, "rsubject");
605             msg = (String JavaDoc)getPortletSession(data, context, "msg");
606
607             String JavaDoc 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             //get the exact message
615
Message JavaDoc message = email.getMessage(current_index);
616             email.reply(from, addressTo, msgecontent, newSubj, fileItem,
617                     message);
618             email.close();
619
620             String JavaDoc message_prompt = "Reply Sent";
621             setPortletSession(data, context, "message_prompt", message_prompt);
622             setPortletSession(data, context, "msgeIndex", null);
623         } catch (Exception JavaDoc 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 JavaDoc h = (Hashtable JavaDoc) getPortletSession(data, context, "hcontent");
641
642         //get subject
643
String JavaDoc subject = (String JavaDoc) h.get("Subject").toString();
644         log.info("subj in doforward" + subject);
645
646         //check if the subject has a prefix "Fwd:"
647
//if subject doesn't have a prefix "Fwd:", default as "Fwd:subject"
648
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         //get the message
665
String JavaDoc msg = (String JavaDoc) 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 JavaDoc userInfo = this.getEmailUserInfo(data, context);
677         String JavaDoc user = (String JavaDoc) userInfo.get("username");
678         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
679         String JavaDoc from = (String JavaDoc) userInfo.get("email");
680         //set addressTo
681
String JavaDoc to = data.getParameters().getString("addressTo");
682
683         String JavaDoc index = (String JavaDoc)getPortletSession(data, context, "index");
684         int current_index = Integer.parseInt(index);
685         setPortletSession(data, context, "msgeIndex", null);
686         try
687         {
688             // Set the from field
689

690             // set subject
691
String JavaDoc subject = data.getParameters().getString("subject");
692             //check if subject is not empty, else default as "none"
693
if (subject.equals(""))
694             {
695                 subject = "none";
696             }
697
698             //check if subject has a prefix "Fwd:"
699
//if subject doesn't hava a prefix "Fwd:",default as "Fwd:subject"
700
if (subject.substring(0, 4).equals("Fwd:"))
701             {
702                 //forward.setSubject(subject);
703
setPortletSession(data, context, "rsubject", subject);
704             } else
705             {
706                 setPortletSession(data, context, "rsubject", "Fwd:" + subject);
707             }
708             String JavaDoc fsubject = (String JavaDoc)getPortletSession(data, context, "rsubject");
709
710             //Set message content
711
String JavaDoc content = data.getParameters().getString("msg");
712             String JavaDoc msge = checkFormat(content);
713
714             Email email = new Email(user, pass, getPortletParameters(data,
715                     context));
716
717             //get exact message
718
Message JavaDoc message = email.getMessage(current_index);
719             email.forward(to, from, fsubject, msge, message);
720             email.close();
721
722             String JavaDoc message_prompt = "Message Sent";
723             setPortletSession(data, context, "message_prompt", message_prompt);
724             setPortletSession(data, context, "msgeIndex", null);
725         } catch (Exception JavaDoc e)
726         {
727             setPortletSession(data, context, "message_prompt",
728                     "Message Sending Failed." + e.getMessage());
729             log.error("Error in doForwardSend()", e);
730         }
731     }
732
733     public void doDelete(RunData data, Context context)
734     {
735         log.info("delete");
736
737         Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
738         String JavaDoc user = (String JavaDoc) userInfo.get("username");
739         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
740
741         String JavaDoc foldername = (String JavaDoc)getPortletSession(data, context, "folder_name");
742
743         int current_index = 0;
744
745         try
746         {
747             //delete
748
String JavaDoc index = (String JavaDoc)getPortletSession(data, context, "index");
749             Email email = new Email(user, pass, getPortletParameters(data,
750                     context));
751
752             String JavaDoc protocol = getPortletParameter(data, context, "protocol");
753
754             //delete in showcontent
755
if (index != null && !index.equals(""))
756             {
757                 current_index = Integer.parseInt(index);
758                 if (protocol.equals("imap"))
759                 {
760                     email.contentDelete(current_index, foldername, protocol);
761                 } else
762                 {
763                     email.contentDelete(current_index);
764
765                 }
766
767                 setPortletSession(data, context, "message_prompt", "Deleted");
768             }
769
770             //multiple delete
771
else
772             {
773                 String JavaDoc[] checkboxes = null;
774                 checkboxes = data.getParameters().getStrings("check");
775                 if (checkboxes != null)
776                 {
777                     if (protocol.equals("imap"))
778                     {
779                         email.checkboxDelete(foldername, checkboxes, protocol);
780                         //email.close();
781
} else
782                     {
783                         email.checkboxDelete(checkboxes);
784
785                     }
786
787                     setPortletSession(data, context, "message_prompt",
788                             "Deleted");
789                 } else
790                 {
791                     setPortletSession(data, context, "message_prompt",
792                             "Select message to be deleted.");
793                 }
794
795             }
796             email.close();
797             log.info("deleted");
798             setPortletSession(data, context, "showContent", "no");
799             setPortletSession(data, context, "showInbox", "yes");
800             setPortletSession(data, context, "msgeIndex", null);
801         } catch (Exception JavaDoc e)
802         {
803             setPortletSession(data, context, "message_prompt",
804                     "Delete Failed. " + e.getMessage());
805             log.error("Error in doDelete()", e);
806
807         }
808     }
809
810     public void doShowcontent(RunData data, Context context,Email email) throws Exception JavaDoc
811     {
812
813         log.info("show content");
814         String JavaDoc AttachmentName = null;
815         Vector JavaDoc vAttachments = new Vector JavaDoc();
816         Message JavaDoc message = null;
817         Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
818         String JavaDoc user = (String JavaDoc) userInfo.get("username");
819         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
820
821         String JavaDoc folderMode = (String JavaDoc)getPortletSession(data, context, "folderMode");
822
823         Vector JavaDoc vContent = new Vector JavaDoc();
824
825         String JavaDoc protocol = getPortletParameter(data, context, "protocol");
826
827         String JavaDoc index = (String JavaDoc)getPortletSession(data, context, "index");
828
829         String JavaDoc folder_name = (String JavaDoc)getPortletSession(data, context, "folder_name");
830
831         int current_index = Integer.parseInt(index);
832
833         if (folderMode.equals("inboxFolder") || (protocol.equals("pop3")))
834         {
835             message = email.getMessage(current_index);
836         } else
837         {
838             message = email.getMessage(current_index, folder_name);
839             Folder JavaDoc current_folder = email.getFolder(folder_name);
840
841         }
842
843         String JavaDoc subject = email.getSubject(message);
844         String JavaDoc from = email.getFrom(message);
845
846         // POP3 does not provide a "received date", so the getReceivedDate
847
// method will
848
// return null. It may be possible to examine other message headers
849
// (e.g., the "Received" headers) to estimate the received date,
850
// but these techniques are error-prone at best.
851
String JavaDoc date = null;
852
853         Vector JavaDoc vAddr = email.getTo(message);
854         String JavaDoc messageContent = email.getMessageContent(message);
855
856         if (!message.isMimeType("text/plain"))
857         {
858             boolean hasAtt = email.checkAttachment(message);
859             if (hasAtt == true)
860             {
861                 AttachmentName = email.getAttachmentname(message);
862                 log.info("attachmentName" + AttachmentName);
863                 vAttachments = email.getAttachments(message);
864                 log.info("vAttachments " + vAttachments.size());
865             }
866         }
867
868         if (protocol.equals("pop3"))
869         {
870             context.put("protocol", "pop3");
871
872         } else if (protocol.equals("imap"))
873         {
874             date = email.getReceivedDate(message);
875             context.put("protocol", "imap");
876         }
877
878         //check if there's a subject... else default as "none"
879
if ((subject == null) || (subject.equals("")))
880         {
881             subject = "none";
882             log.info("subj" + subject);
883         }
884
885         Hashtable JavaDoc hcontent = new Hashtable JavaDoc();
886         // Store Message Id into context : RJPY
887
hcontent.put("MessageId", getMessageId(message));
888
889         hcontent.put("From", from);
890         hcontent.put("Subject", subject);
891         if (protocol.equals("imap"))
892         {
893             hcontent.put("ReceivedDate", date);
894         }
895
896         hcontent.put("message", messageContent);
897
898         if (AttachmentName != null)
899                 hcontent.put("AttachmentName", AttachmentName);
900
901         vContent.add(hcontent);
902         //set message as Old
903
setMessageflag(message);
904
905         setPortletSession(data, context, "hcontent", hcontent);
906         setPortletSession(data, context, "inContent", vContent);
907
908         context.put("inContent", vContent);
909         context.put("vAddr", vAddr);
910         context.put("vAttachments", vAttachments);
911
912
913     }
914
915     public void doShow(RunData data, Context context) throws Exception JavaDoc
916     {
917         log.info("show");
918         setPortletSession(data, context, "showInbox", "no");
919         setPortletSession(data, context, "showContent", "yes");
920         setPortletSession(data, context, "msgeContent", "yes");
921         setPortletSession(data, context, "createfolder", "no");
922         setPortletSession(data, context, "showmessagefolder", "no");
923
924         //get the index of the message chosen by the user
925
String JavaDoc index = (String JavaDoc) data.getParameters().getString("index");
926         //get folderMode; to know what folder to open
927
String JavaDoc folderMode = (String JavaDoc) data.getParameters().getString(
928                 "folderMode");
929
930         setPortletSession(data, context, "index", index);
931         setPortletSession(data, context, "folderMode", folderMode);
932
933     }
934
935     public void doInbox(RunData data, Context context) throws Exception JavaDoc
936     {
937
938         int cur_page = 1;
939         int range_per_page = maxPerPage - 1;
940         int start_index = 0;
941         int total_no_of_pages = 0;
942
943         //reset the value of index
944
setPortletSession(data, context, "index", null);
945         setPortletSession(data, context, "message_prompt", null);
946
947         //for multiple pages
948
setPortletSession(data, context, "cur_page", String.valueOf(cur_page));
949         setPortletSession(data, context, "range_per_page", String
950                 .valueOf(range_per_page));
951         setPortletSession(data, context, "start_index", String
952                 .valueOf(start_index));
953         setPortletSession(data, context, "total_no_of_pages", String
954                 .valueOf(total_no_of_pages));
955         setPortletSession(data, context, "showFolders", "no");
956         setPortletSession(data, context, "showInbox", "yes");
957         setPortletSession(data, context, "compose", "no");
958         setPortletSession(data, context, "showContent", "no");
959         setPortletSession(data, context, "reply", "no");
960         setPortletSession(data, context, "forward", "no");
961         setPortletSession(data, context, "createfolder", "no");
962         setPortletSession(data, context, "showmessagefolder", "no");
963         setPortletSession(data, context, "folder_name", "INBOX");
964
965         Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
966         String JavaDoc user = (String JavaDoc) userInfo.get("username");
967         String JavaDoc pass = (String JavaDoc) userInfo.get("password");
968
969         //always recheck the no. of new messages
970
setPortletSession(data, context, "showNumNewmessages", null);
971
972         setPortletSession(data, context, "inboxMessages", "yes");
973         setPortletSession(data, context, "msgeIndex", null);
974
975     }
976
977     public void checkMessages(RunData data, Context context, Email email) throws Exception JavaDoc
978     {
979
980         String JavaDoc start_index1 = (String JavaDoc)getPortletSession(data, context, "start_index");
981
982         String JavaDoc range_per_page1 = (String JavaDoc)getPortletSession(data, context,
983                 "range_per_page");
984         String JavaDoc cur_page1 = (String JavaDoc)getPortletSession(data, context, "cur_page");
985         String JavaDoc total_no_of_pages1 = (String JavaDoc)getPortletSession(data, context,
986                 "total_no_of_pages");
987         int start_index = Integer.parseInt(start_index1);
988         int range_per_page = Integer.parseInt(range_per_page1);
989         int cur_page = Integer.parseInt(cur_page1);
990         int total_no_of_pages = Integer.parseInt(total_no_of_pages1);
991         
992         try
993         {
994             Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
995             String JavaDoc user = (String JavaDoc) userInfo.get("username");
996             String JavaDoc pass = (String JavaDoc) userInfo.get("password");
997             Vector JavaDoc vMessages = null;
998             int msgectr = email.getNo_of_messages();
999
1000            setPortletSession(data, context, "msgcount", String
1001                    .valueOf(msgectr));
1002
1003            //check if more than 1 page
1004
if (msgectr > maxPerPage)
1005            {
1006
1007                //get the total no. of pages
1008
if ((msgectr % 10) == 0)
1009                {
1010                    total_no_of_pages = (msgectr / 10);
1011                    setPortletSession(data, context, "total_no_of_pages",
1012                            String.valueOf(total_no_of_pages));
1013                    log.info("total pages" + total_no_of_pages);
1014                    setPortletSession(data, context, "cur_page", String
1015                            .valueOf(cur_page));
1016                    log.info("cur_page" + cur_page);
1017                } else
1018                {
1019                    total_no_of_pages = ((msgectr / 10) + 1);
1020                    setPortletSession(data, context, "total_no_of_pages",
1021                            String.valueOf(total_no_of_pages));
1022                    log.info("total pages" + total_no_of_pages);
1023                    setPortletSession(data, context, "cur_page", String
1024                            .valueOf(cur_page));
1025                    log.info("cur_page" + cur_page);
1026                }
1027            }
1028            setPortletSession(data, context, "start_index", String
1029                    .valueOf(start_index));
1030
1031            setPortletSession(data, context, "range_per_page", String
1032                    .valueOf(range_per_page));
1033
1034
1035            range_per_page1 = (String JavaDoc)getPortletSession(data, context,
1036                    "range_per_page");
1037
1038            String JavaDoc protocol = getPortletParameter(data, context, "protocol");
1039            if (protocol.equals("pop3"))
1040            {
1041                context.put("protocol", "pop3");
1042            } else if (protocol.equals("imap"))
1043            {
1044                context.put("protocol", "imap");
1045            }
1046
1047            Vector JavaDoc vAscmessages = email.openInbox(protocol);
1048            // all messages in ascending order
1049

1050            int msgeIndexnum = 0; // initial message index per page
1051
String JavaDoc msgeIndexstring = (String JavaDoc)getPortletSession(data, context,
1052                    "msgeIndex");
1053            msgeIndexnum = vAscmessages.size();
1054            if (msgeIndexstring == null || msgeIndexstring.equals(""))
1055            {
1056                setPortletSession(data, context, "msgeIndex", String
1057                        .valueOf(msgeIndexnum));
1058
1059            } else
1060            {
1061
1062                //check if msgeIndex is > than the actual message size
1063
//if it is, reset msgeIndex
1064
try
1065                {
1066                    int msgIndex = Integer.parseInt(msgeIndexstring);
1067                    if (msgIndex > msgeIndexnum)
1068                    {
1069                        setPortletSession(data, context, "msgeIndex", null);
1070                    }
1071
1072                } catch (NumberFormatException JavaDoc ne)
1073                {
1074                    setPortletSession(data, context, "msgeIndex", String
1075                            .valueOf(msgeIndexnum));
1076                }
1077
1078            }
1079
1080            boolean withAttachment = true;
1081            for (int i = 0; i < vAscmessages.size(); i++)
1082            {
1083                Hashtable JavaDoc ht = (Hashtable JavaDoc) vAscmessages.get(i);
1084                String JavaDoc s = (String JavaDoc) ht.get("hasAttachment");
1085                Message JavaDoc message = (Message JavaDoc) ht.get("message");
1086
1087                if (s.equals(""))
1088                {
1089                    withAttachment = false;
1090                } else
1091                    withAttachment = true;
1092
1093                DBInsert(data, message, withAttachment);
1094
1095            }
1096            
1097            descendingOrder(data, context, vAscmessages, start_index,
1098                    range_per_page, msgectr);
1099
1100        } catch (NoSuchProviderException JavaDoc np)
1101        {
1102            log.error("Please check email paramters... Protocol(imap/pop3) is case-sensitive.");
1103            log.error(np);
1104        } catch (MessagingException JavaDoc ms)
1105        {
1106            log.error(ms);
1107        } catch (IOException JavaDoc io)
1108        {
1109            log.error(io);
1110        }
1111    }
1112
1113    /*-----------------------------------------------------------------------------------------*
1114     * Retrieves message id from message : RJPY
1115     *-----------------------------------------------------------------------------------------*/

1116    public String JavaDoc getMessageId(Message JavaDoc message)
1117    {
1118        String JavaDoc messageid = "";
1119        try
1120        {
1121            // Retrieve message id from headers
1122
Enumeration JavaDoc e = message.getAllHeaders();
1123            while (messageid.equals(""))
1124            {
1125                Header JavaDoc header = (Header JavaDoc) e.nextElement();
1126                if (header.getName().equals("Message-ID"))
1127                {
1128                    messageid = header.getValue();
1129                }
1130            }
1131        } catch (Exception JavaDoc e)
1132        {
1133
1134            log.error("Error in getMessageId()", e);
1135        }
1136        return messageid;
1137        // end Retrieve
1138
}
1139
1140    /*-----------------------------------------------------------------------------------------*
1141     * Inserts message and attachment detail to database : RJPY
1142     *-----------------------------------------------------------------------------------------*/

1143
1144    public void DBInsert(RunData data, Message JavaDoc message, boolean withAttachment)
1145    {
1146        try
1147        {
1148            log.info("[RJPY] Trying to write into DB...");
1149
1150            String JavaDoc messageid = getMessageId(message);
1151            Criteria crit = new Criteria();
1152            crit.add(EmailInboxPeer.MESSAGE_ID, messageid);
1153            //Vector vEmail = EmailInboxPeer.doSelect(crit);
1154
List JavaDoc vEmail = EmailInboxPeer.doSelect(crit); //tdk2.2 version
1155
if (vEmail.size() == 0)
1156            {
1157
1158                log.info("[RJPY] This email is not yet in the DB...");
1159                String JavaDoc filename = "";
1160                InputStream JavaDoc is = null;
1161                //PreparedStatement ps = con.prepareStatement(sql2);
1162

1163                int size = 0;
1164
1165                // Get Attachment
1166
if (withAttachment)
1167                {
1168
1169                    Object JavaDoc obj = message.getContent();
1170                    Multipart JavaDoc mpart = (Multipart JavaDoc) obj;
1171
1172                    for (int j = 0, n = mpart.getCount(); j < n; j++)
1173                    {
1174                        Part JavaDoc part = mpart.getBodyPart(j);
1175                        String JavaDoc disposition = part.getDisposition();
1176                        if ((disposition != null)
1177                                && ((disposition
1178                                        .equalsIgnoreCase(Part.ATTACHMENT)) || (disposition
1179                                        .equals(Part.INLINE))))
1180                        {
1181                            if (part.getFileName() != null)
1182                            {
1183                                log.info("*** Attachment name: "
1184                                        + part.getFileName());
1185                                if (part.getContent() instanceof String JavaDoc)
1186                                {
1187                                    byte[] b = ((String JavaDoc) part.getContent())
1188                                            .getBytes();
1189                                    is = new ByteArrayInputStream JavaDoc(b);
1190                                } else
1191                                {
1192                                    is = part.getInputStream();
1193                                }
1194                                filename = part.getFileName();
1195                                size = part.getSize();
1196                            }
1197                        }
1198                    } //for
1199
} // if with attachment
1200

1201                if (is != null)
1202                {
1203                    // ps.executeUpdate();
1204
log.info("sure");
1205                    EmailInbox emailInbox = new EmailInbox();
1206                    emailInbox.setMessageId(messageid);
1207                    emailInbox.setReadflag(0);
1208                    emailInbox.setFilename(filename);
1209
1210                    byte[] b = new byte[size];
1211                    int bytes = is.read(b);
1212                    while (bytes != -1)
1213                    {
1214                        bytes = is.read(b);
1215                    }
1216                    emailInbox.setAttachment(b);
1217                    EmailInboxPeer.doInsert(emailInbox);
1218                } else
1219                {
1220                    //EmailInboxPeer.executeStatement(sql);
1221
EmailInbox emailInbox = new EmailInbox();
1222                    emailInbox.setMessageId(messageid);
1223                    emailInbox.setReadflag(0);
1224                    EmailInboxPeer.doInsert(emailInbox);
1225                }
1226                //ps.close();
1227
} //if email doesnt exist
1228

1229        } catch (Exception JavaDoc ex)
1230        {
1231
1232            log.error("Error in DBInsert()", ex);
1233
1234        }
1235
1236    }
1237
1238    // recent messages dislayed on top
1239
public void descendingOrder(RunData data, Context context,
1240            Vector JavaDoc inMessages, int start_index, int range_per_page, int msgectr)
1241    {
1242        Vector JavaDoc vDescending = new Vector JavaDoc();
1243        int msgSize = inMessages.size();
1244        String JavaDoc msgeIndex1 = (String JavaDoc)getPortletSession(data, context, "msgeIndex");
1245        int msgIndex = 0;
1246        try
1247        {
1248            //check if msgeIndex is > than the actual message size
1249
//if it is, reset msgeIndex
1250
msgIndex = Integer.parseInt(msgeIndex1);
1251            if (msgIndex > msgSize)
1252            {
1253                setPortletSession(data, context, "msgeIndex", null);
1254            }
1255
1256        } catch (NumberFormatException JavaDoc ne)
1257        {
1258            msgIndex = msgSize;
1259            setPortletSession(data, context, "msgeIndex", String
1260                    .valueOf(msgSize));
1261        }
1262
1263        //display 10 messages per page (descending order)
1264
for (int j = start_index; ((j <= range_per_page) && (j < msgectr)); j++)
1265        {
1266            vDescending.add(inMessages.get(msgIndex - 1));
1267            msgIndex--;
1268        }
1269        context.put("inMessages", vDescending);
1270
1271    }
1272
1273    //check if message is to be move to a new line
1274
public String JavaDoc convertMessage(String JavaDoc msg) throws Exception JavaDoc
1275    {
1276        log.info("convert message");
1277        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1278
1279        for (int i = 0; i < msg.length() - 1; i++)
1280        {
1281            char c = msg.charAt(i);
1282            if (c == '\n')
1283            {
1284                if (((msg.charAt(i + 1)) != '<') && (msg.charAt(i + 1) != ' '))
1285                {
1286                    log.info("new line");
1287                    sb.append("<br>");
1288                } else
1289                {
1290                    sb.append(c);
1291                }
1292            } else
1293            {
1294                sb.append(c);
1295            }
1296        }
1297        String JavaDoc returnString = sb.toString();
1298        return returnString;
1299    }
1300
1301    // exclude displaying html tags in replying or forwarding message
1302
// there are still instances that html tags may be included/displayed in
1303
// reply/forward message.
1304
// only those tags below were checked... other than those, tags may be
1305
// displayed in some cases.
1306

1307    public String JavaDoc checkFormat(String JavaDoc msg) throws Exception JavaDoc
1308    {
1309        log.info("check format ");
1310        int startIndex = 0;
1311        int end = 0;
1312
1313        String JavaDoc testSubstring = null;
1314
1315        for (int i = 0; i < msg.length() - 6; i++)
1316        {
1317            String JavaDoc sub = msg.substring(i, i + 2);
1318            if (sub.equals("<A"))
1319            {
1320                startIndex = i;
1321                end = msg.indexOf(">", startIndex);
1322                StringBuffer JavaDoc sb = new StringBuffer JavaDoc(msg);
1323                sb.replace(startIndex, end + 1, " ");
1324                msg = sb.toString();
1325            }
1326        }
1327
1328        for (int i = 0; i < msg.length() - 7; i++)
1329        {
1330            String JavaDoc sub = msg.substring(i, i + 7);
1331            if (sub.equals("style=\""))
1332            {
1333                startIndex = i;
1334                end = msg.indexOf(">", startIndex);
1335                StringBuffer JavaDoc sb = new StringBuffer JavaDoc(msg);
1336                sb.replace(startIndex, end + 1, " ");
1337                msg = sb.toString();
1338            }
1339        }
1340
1341        for (int j = 0; j < msg.length() - 6; j++)
1342        {
1343            String JavaDoc sub = msg.substring(j, j + 6);
1344            if (sub.equals("href=\""))
1345            {
1346                startIndex = j;
1347                end = msg.indexOf(">", startIndex);
1348                StringBuffer JavaDoc sb = new StringBuffer JavaDoc(msg);
1349                sb.replace(startIndex, end + 1, " ");
1350                msg = sb.toString();
1351            }
1352        }
1353        return msg;
1354    }
1355
1356    //set flag as OLD
1357
public void setMessageflag(Message JavaDoc message) throws Exception JavaDoc
1358    {
1359        log.info("set message flag");
1360
1361        String JavaDoc msgeId = getMessageId(message);
1362
1363        Criteria cr = new Criteria();
1364        cr.add(EmailInboxPeer.MESSAGE_ID, msgeId);
1365
1366        //Vector vMsge = EmailInboxPeer.doSelect(cr);
1367
List JavaDoc vMsge = EmailInboxPeer.doSelect(cr); //tdk2.2 version
1368
EmailInbox eMsge = (EmailInbox) vMsge.get(0);
1369        eMsge.setReadflag(1);
1370        EmailInboxPeer.doUpdate(eMsge);
1371    }
1372
1373    public void doNext(RunData data, Context context) throws Exception JavaDoc
1374    {
1375
1376        //view next page
1377
log.info("doNext");
1378        String JavaDoc start_index1 = (String JavaDoc)getPortletSession(data, context, "start_index");
1379
1380        String JavaDoc range_per_page1 = (String JavaDoc)getPortletSession(data, context,
1381                "range_per_page");
1382        String JavaDoc cur_page1 = (String JavaDoc)getPortletSession(data, context, "cur_page");
1383        String JavaDoc msgeIndex1 = (String JavaDoc)getPortletSession(data, context, "msgeIndex");
1384
1385        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1386        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1387        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1388
1389        int start_index = Integer.parseInt(start_index1);
1390        int range_per_page = Integer.parseInt(range_per_page1);
1391        int cur_page = Integer.parseInt(cur_page1);
1392        int msgeIndex = Integer.parseInt(msgeIndex1);
1393
1394        start_index = (range_per_page + 1);
1395        range_per_page = (range_per_page + maxPerPage);
1396        msgeIndex = msgeIndex - 10;
1397        log.info("msgeIndex in doNext " + msgeIndex);
1398        setPortletSession(data, context, "start_index", String
1399                .valueOf(start_index));
1400
1401        setPortletSession(data, context, "range_per_page", String
1402                .valueOf(range_per_page));
1403        setPortletSession(data, context, "msgeIndex", String
1404                .valueOf(msgeIndex));
1405        cur_page = cur_page + 1;
1406        setPortletSession(data, context, "cur_page", String.valueOf(cur_page));
1407
1408        setPortletSession(data, context, "inboxMessages", "yes");
1409
1410        log.info("checkmessages ---------");
1411        // checkMessages(data,context);
1412

1413        // store.close();
1414
}
1415
1416    public void doPrevious(RunData data, Context context) throws Exception JavaDoc
1417    {
1418
1419        //view previous page
1420

1421        log.info("doPrevious");
1422        String JavaDoc start_index1 = (String JavaDoc)getPortletSession(data, context, "start_index");
1423        String JavaDoc range_per_page1 = (String JavaDoc)getPortletSession(data, context,
1424                "range_per_page");
1425        String JavaDoc cur_page1 = (String JavaDoc)getPortletSession(data, context, "cur_page");
1426        String JavaDoc msgeIndex1 = (String JavaDoc)getPortletSession(data, context, "msgeIndex");
1427
1428        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1429        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1430        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1431
1432        int start_index = Integer.parseInt(start_index1);
1433        int range_per_page = Integer.parseInt(range_per_page1);
1434        int cur_page = Integer.parseInt(cur_page1);
1435        int msgeIndex = Integer.parseInt(msgeIndex1);
1436
1437        start_index = start_index - maxPerPage;
1438        range_per_page = range_per_page - maxPerPage;
1439        msgeIndex = msgeIndex + 10;
1440        log.info("msgeIndex in previous " + msgeIndex);
1441        setPortletSession(data, context, "start_index", String
1442                .valueOf(start_index));
1443        setPortletSession(data, context, "range_per_page", String
1444                .valueOf(range_per_page));
1445        cur_page = cur_page - 1;
1446        setPortletSession(data, context, "cur_page", String.valueOf(cur_page));
1447
1448        setPortletSession(data, context, "msgeIndex", String
1449                .valueOf(msgeIndex));
1450        setPortletSession(data, context, "inboxMessages", "yes");
1451
1452    }
1453
1454    /*-----------------------------------------------------------------------------------------*
1455     * Downloads mail attachment from database using the message id as the key : RJPY
1456     *-----------------------------------------------------------------------------------------*/

1457
1458    public void doDownload(RunData data)
1459    {
1460
1461        log.info("[RJPY] Downloading Attachment");
1462
1463        String JavaDoc messageid = data.getParameters().getString("messageid");
1464        String JavaDoc filename = data.getParameters().getString("filename");
1465
1466        try
1467        {
1468            log.info("inside try in download");
1469            HttpServletResponse JavaDoc m_response = data.getResponse();
1470
1471            Criteria crit = new Criteria();
1472            crit.add(EmailInboxPeer.MESSAGE_ID, messageid);
1473            List JavaDoc vMsge = EmailInboxPeer.doSelect(crit);
1474
1475            if (vMsge.isEmpty())
1476            { // message id not found in db
1477
return;
1478            } else
1479            { // message found in db but flag is set to 0
1480
EmailInbox email = (EmailInbox) EmailInboxPeer.doSelect(crit)
1481                        .get(0);
1482                byte b[] = email.getAttachment();
1483                m_response.setContentType("application/x-msdownload");
1484                m_response.setContentLength(b.length);
1485                m_response.setHeader("Content-Disposition",
1486                        "attachment; filename="
1487                                .concat(String.valueOf(filename)));
1488                m_response.getOutputStream().write(b, 0, b.length);
1489            }
1490
1491        } catch (Exception JavaDoc e)
1492        {
1493            log.error("Error in doDownload()", e);
1494        }
1495    }
1496
1497    public void DBdelete(RunData data, int current_index, Context context)
1498            throws Exception JavaDoc
1499    {
1500        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1501        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1502        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1503
1504        Email email = new Email(user, pass, getPortletParameters(data, context));
1505        Message JavaDoc message = email.getMessage(current_index);
1506        String JavaDoc messageId = email.getMessageId(message);
1507        email.close();
1508
1509        Criteria crit = new Criteria();
1510        crit.add(EmailInboxPeer.MESSAGE_ID, messageId);
1511        EmailInboxPeer.doDelete(crit);
1512    }
1513
1514    public void DBmultipleDelete(RunData data, String JavaDoc[] checkboxes,
1515            Context context) throws Exception JavaDoc
1516    {
1517        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1518        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1519        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1520
1521        Email email = new Email(user, pass, getPortletParameters(data, context));
1522        for (int i = 0; i < checkboxes.length; i++)
1523        {
1524            int current_index = Integer.parseInt(checkboxes[i]);
1525            Message JavaDoc message = email.getMessage(current_index);
1526            String JavaDoc messageId = email.getMessageId(message);
1527            email.close();
1528            Criteria crit = new Criteria();
1529            crit.add(EmailInboxPeer.MESSAGE_ID, messageId);
1530            EmailInboxPeer.doDelete(crit);
1531        }
1532        email.close();
1533    }
1534
1535    public void doCreatenewfolder(RunData data, Context context)
1536            throws AuthenticationFailedException JavaDoc, NoSuchProviderException JavaDoc,
1537            Exception JavaDoc
1538    {
1539        setPortletSession(data, context, "showFolders", "no");
1540        setPortletSession(data, context, "showInbox", "no");
1541        setPortletSession(data, context, "compose", "no");
1542        setPortletSession(data, context, "showContent", "no");
1543        setPortletSession(data, context, "reply", "no");
1544        setPortletSession(data, context, "forward", "no");
1545        setPortletSession(data, context, "createfolder", "yes");
1546        setPortletSession(data, context, "showmessagefolder", "no");
1547        setPortletSession(data, context, "message_prompt", null);
1548
1549    }
1550
1551    public void doGetfoldername(RunData data, Context context)
1552    {
1553
1554        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1555        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1556        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1557        try
1558        {
1559            Email email = new Email(user, pass, getPortletParameters(data,
1560                    context));
1561            String JavaDoc folder_name = data.getParameters().getString("folder_name");
1562            email.doCreatefolder(folder_name);
1563            email.close();
1564        } catch (Exception JavaDoc e)
1565        {
1566            log.error("Error in doGetfoldername()", e);
1567        }
1568    }
1569
1570    public void doGetfolderdest(RunData data, Context context)
1571    {
1572        String JavaDoc toFolder = data.getParameters().getString("foldername");
1573        String JavaDoc fromFolder = (String JavaDoc)getPortletSession(data, context, "folder_name");
1574        String JavaDoc[] checkboxes = null;
1575        checkboxes = data.getParameters().getStrings("check");
1576
1577        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1578        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1579        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1580
1581        try
1582        {
1583            Email email = new Email(user, pass, getPortletParameters(data,
1584                    context));
1585            if (checkboxes != null)
1586            {
1587                email.moveMessage(fromFolder, toFolder, checkboxes);
1588            }
1589            email.close();
1590            doInbox(data, context);
1591
1592        } catch (Exception JavaDoc e)
1593        {
1594            log.error("Error in doGetfolderdest()", e);
1595        }
1596    }
1597
1598    public void doMovesinglemsge(RunData data, Context context)
1599    {
1600        String JavaDoc index = (String JavaDoc)getPortletSession(data, context, "index");
1601
1602        String JavaDoc fromFolder = (String JavaDoc)getPortletSession(data, context, "folder_name");
1603
1604        int current_index = Integer.parseInt(index);
1605
1606        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1607        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1608        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1609
1610        try
1611        {
1612            Email email = new Email(user, pass, getPortletParameters(data,
1613                    context));
1614            String JavaDoc toFolder = data.getParameters().getString("foldername");
1615            email.moveMessage(fromFolder, toFolder, current_index);
1616            email.close();
1617            doInbox(data, context);
1618        } catch (Exception JavaDoc e)
1619        {
1620            log.error("Error in doMovesinglemsge()", e);
1621        }
1622
1623    }
1624
1625    // show list of messages in a folder
1626
public void doOpenmyfolder(RunData data, Context context)
1627    {
1628        setPortletSession(data, context, "showInbox", "no");
1629        setPortletSession(data, context, "showmessagefolder", "yes");
1630        setPortletSession(data, context, "createfolder", "no");
1631
1632        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1633        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1634        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1635
1636        String JavaDoc folder_name = data.getParameters().getString("folder_name");
1637
1638        setPortletSession(data, context, "folder_name", folder_name);
1639
1640        try
1641        {
1642            Email email = new Email(user, pass, getPortletParameters(data,
1643                    context));
1644            String JavaDoc protocol = getPortletParameter(data, context, "protocol");
1645            Vector JavaDoc message_folder = (Vector JavaDoc) email.openMyfolder(folder_name,
1646                    protocol);
1647            setPortletSession(data, context, "message_folder", message_folder);
1648            email.close();
1649        } catch (Exception JavaDoc e)
1650        {
1651            log.error("Error in doOpenmyfolder()", e);
1652        }
1653    }
1654
1655    public void doFolderdelete(RunData data, Context context)
1656    {
1657        String JavaDoc folder_name = data.getParameters().getString("folder_name");
1658
1659        Hashtable JavaDoc userInfo = this.getEmailUserInfo(data, context);
1660        String JavaDoc user = (String JavaDoc) userInfo.get("username");
1661        String JavaDoc pass = (String JavaDoc) userInfo.get("password");
1662
1663        try
1664        {
1665            Email email = new Email(user, pass, getPortletParameters(data,
1666                    context));
1667            email.folderDelete(folder_name);
1668            email.close();
1669
1670        } catch (Exception JavaDoc e)
1671        {
1672            log.error("Error in doFolderdelete()", e);
1673        }
1674    }
1675
1676    /**
1677     * get user authentication info for email. Check first in porlet instance.
1678     * If not found retrieve current user name and password
1679     *
1680     */

1681    public Hashtable JavaDoc getEmailUserInfo(RunData rundata, Context context)
1682    {
1683        Hashtable JavaDoc userHash = new Hashtable JavaDoc();
1684        String JavaDoc user = "";
1685        String JavaDoc password = "";
1686        String JavaDoc email = "";
1687        String JavaDoc jetspeedUser = "";
1688        String JavaDoc jetspeedPassword = "";
1689        String JavaDoc jetspeedEmail = "";
1690        try
1691        {
1692            //look first if there is a username passed by the login page
1693
user = rundata.getParameters().getString("emailUsername");
1694            password = rundata.getParameters().getString("emailPassword");
1695            email = rundata.getParameters().getString("addressFrom");
1696
1697            if (email == null || email.equals(""))
1698            {
1699                email = jetspeedEmail = (JetspeedSecurity.getUser(rundata
1700                        .getUser().getUserName()).getEmail());
1701            }
1702
1703            if (user == null || user.equals(""))
1704            {
1705
1706                user = getPortletParameter(rundata, context, "username");
1707                password = getPortletParameter(rundata, context, "password");
1708                
1709                //check if there is an email address in the portlet parameter
1710
String JavaDoc emailParam = getPortletParameter(rundata, context, "email_address");
1711                if(emailParam != null && !emailParam.equals(""))
1712                {
1713                    email = emailParam;
1714                }
1715
1716                //check if there is a default value in the portlet parameter
1717
if (user.equals(""))
1718                {
1719
1720                    jetspeedUser = (JetspeedSecurity.getUser(rundata.getUser()
1721                            .getUserName()).getUserName());
1722                    jetspeedPassword = (JetspeedSecurity.getUser(rundata
1723                            .getUser().getUserName()).getPassword());
1724                    if (email.equals(""))
1725                    {
1726                        jetspeedEmail = (JetspeedSecurity.getUser(rundata
1727                                .getUser().getUserName()).getEmail());
1728                    }
1729                    //use turbine user info
1730
user = jetspeedUser;
1731                    password = jetspeedPassword;
1732                    email = jetspeedEmail;
1733
1734                }
1735            }
1736
1737            userHash.put("username", user);
1738            userHash.put("password", password);
1739            userHash.put("email", email);
1740        } catch (Exception JavaDoc e)
1741        {
1742            log.error(e);
1743        }
1744        return userHash;
1745    }
1746
1747    /**
1748     * get portlet parameter from portlet instance. if not found, get from
1749     * registry
1750     */

1751    public String JavaDoc getPortletParameter(RunData data, Context context,
1752            String JavaDoc paramName)
1753    {
1754        String JavaDoc ret = null;
1755        try
1756        {
1757
1758            ret = PortletConfigState.getParameter(this.getPortlet(context),
1759                    data, paramName, null);
1760        } catch (Exception JavaDoc e)
1761        {
1762            ret = null;
1763        }
1764        return ret;
1765    }
1766
1767
1768    /** sets the portlet session */
1769    public void setPortletSession(RunData data,Context context,String JavaDoc paramName, Object JavaDoc value)
1770    {
1771        
1772        
1773      PortletSessionState.setAttribute(this.getPortlet(context),data,paramName,value);
1774
1775    }
1776    
1777    /** gets the portlet session */
1778    public Object JavaDoc getPortletSession(RunData data,Context context,String JavaDoc paramName)
1779    {
1780        
1781        
1782      return (Object JavaDoc)PortletSessionState.getAttribute(this.getPortlet(context),data,paramName);
1783
1784    }
1785    
1786    public Hashtable JavaDoc getPortletParameters(RunData data, Context context)
1787    {
1788
1789        Hashtable JavaDoc param = new Hashtable JavaDoc();
1790        try
1791        {
1792            // PortletInstance instance = this.getPortletInstance(context);
1793
// Registry reg =
1794
// org.apache.jetspeed.services.Registry.get("Portlet");
1795

1796            param.put("hostname",
1797                    getPortletParameter(data, context, "hostname"));
1798            param.put("protocol",
1799                    getPortletParameter(data, context, "protocol"));
1800            param.put("smtp_user", getPortletParameter(data, context,
1801                    "smtp_user"));
1802            param.put("smtp_port", getPortletParameter(data, context,
1803                    "smtp_port"));
1804            param.put("smtp_conn_timeout", getPortletParameter(data, context,
1805                    "smtp_conn_timeout"));
1806            param.put("smtp_timeout", getPortletParameter(data, context,
1807                    "smtp_timeout"));
1808            param.put("smtp_from", getPortletParameter(data, context,
1809                    "smtp_from"));
1810            param.put("smtp_localhost", getPortletParameter(data, context,
1811                    "smtp_localhost"));
1812            param.put("smtp_ehlo", getPortletParameter(data, context,
1813                    "smtp_ehlo"));
1814            param.put("smtp_auth", getPortletParameter(data, context,
1815                    "smtp_auth"));
1816            param.put("smtp_dsn_notify", getPortletParameter(data, context,
1817                    "smtp_dsn_notify"));
1818            param.put("smtp_dsn_ret", getPortletParameter(data, context,
1819                    "smtp_dsn_ret"));
1820            param.put("smtp_allow8bitmime", getPortletParameter(data, context,
1821                    "smtp_allow8bitmime"));
1822            param.put("smtp_send_partial", getPortletParameter(data, context,
1823                    "smtp_send_partial"));
1824            param.put("smtp_sasl_realm", getPortletParameter(data, context,
1825                    "smtp_sasl_realm"));
1826            param.put("smtp_quit_wait", getPortletParameter(data, context,
1827                    "smtp_quit_wait"));
1828            param.put("imap_port", getPortletParameter(data, context,
1829                    "imap_port"));
1830            param.put("imap_partial_fetch", getPortletParameter(data, context,
1831                    "imap_partial_fetch"));
1832            param.put("imap_fetch_size", getPortletParameter(data, context,
1833                    "imap_fetch_size"));
1834            param.put("imap_timeout", getPortletParameter(data, context,
1835                    "imap_timeout"));
1836            param.put("imap_host", getPortletParameter(data, context,
1837                    "imap_host"));
1838
1839        } catch (Exception JavaDoc e)
1840        {
1841            log.error("Error in getPortletParameters()", e);
1842        }
1843
1844        return param;
1845    }
1846
1847}
Popular Tags