KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > MailFolderTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import com.micronova.util.*;
39 import com.micronova.util.servlet.*;
40 import java.util.*;
41 import java.util.regex.*;
42 import javax.servlet.*;
43 import javax.servlet.jsp.*;
44 import javax.servlet.http.*;
45
46 import javax.mail.*;
47 import javax.mail.internet.*;
48
49 /** mail folder */
50
51 public class MailFolderTag extends ParamTag
52 {
53     public static final String JavaDoc MESSAGES = "messages";
54     public static final String JavaDoc OPERATION = "operation";
55     public static final String JavaDoc CONTROL = "control";
56
57     public static final String JavaDoc READ = "read";
58     public static final String JavaDoc DELETE = "delete";
59
60     protected String JavaDoc _url;
61     protected Folder _folder;
62
63     protected void init()
64     {
65         super.init();
66
67         _url = null;
68         _folder = null;
69     }
70
71     public Folder getFolder()
72     {
73         Folder folder = _folder;
74
75         if (folder != null)
76         {
77             return folder;
78         }
79
80         String JavaDoc url = _url;
81
82         if (url == null)
83         {
84             MailFolderTag folderSource = (MailFolderTag)getAncestorTag("com.micronova.jsp.tag.MailFolderTag");
85
86             if (folderSource != null)
87             {
88                 folder = folderSource.getFolder();
89             }
90         }
91         else
92         {
93             try
94             {
95                 Session session = Session.getInstance(new Properties());
96                 
97                 folder = session.getFolder(new URLName(url));
98             
99                 _folder = folder;
100             }
101             catch (Exception JavaDoc e)
102             {
103             }
104         }
105
106         return folder;
107     }
108
109     public void closeFolder()
110     {
111         Folder folder = _folder;
112
113         if (folder != null)
114         {
115             try
116             {
117                 folder.close(true);
118             }
119             catch (Exception JavaDoc e)
120             {
121                 e.printStackTrace();
122             }
123
124             _folder = null;
125         }
126     }
127
128     protected void cleanup()
129     {
130         closeFolder();
131
132         super.cleanup();
133     }
134
135     public void setUrl(Object JavaDoc expression) throws Exception JavaDoc
136     {
137         _url = (String JavaDoc)evaluateAttribute("url", expression, String JavaDoc.class);
138
139         closeFolder();
140     }
141
142     public void setOperation(Object JavaDoc expression) throws Exception JavaDoc
143     {
144         _map.put(OPERATION, (String JavaDoc)evaluateAttribute(OPERATION, expression, String JavaDoc.class));
145     }
146
147     public void setMessages(Object JavaDoc expression) throws Exception JavaDoc
148     {
149         _map.put(MESSAGES, evaluateAttribute(MESSAGES, expression, Object JavaDoc.class));
150     }
151
152     public void setControl(Object JavaDoc expression) throws Exception JavaDoc
153     {
154         _map.put(CONTROL, evaluateAttribute(CONTROL, expression, Object JavaDoc.class));
155     }
156
157     public static int[] mark(String JavaDoc specs, int min, int max)
158     {
159         int size = max + 1;
160
161         boolean mark[] = new boolean[size];
162
163         int markCount = 0;
164
165         String JavaDoc[] specArray = specs.split(",");
166
167         for (int i = 0; i < specArray.length; i ++)
168         {
169             String JavaDoc spec = specArray[i];
170
171             int firstIndex = min;
172             int lastIndex = max;
173
174             if (!("*".equals(spec)))
175             {
176                 int separator = spec.indexOf('-');
177                 
178                 if (separator >= 0)
179                 {
180                     String JavaDoc first = spec.substring(0, separator);
181                     String JavaDoc last = spec.substring(separator + 1);
182                     
183                     if (!("*".equals(first)))
184                     {
185                         firstIndex = Integer.parseInt(first);
186                     }
187                     
188                     if (!("*".equals(last)))
189                     {
190                         lastIndex = Integer.parseInt(last);
191                     }
192                 }
193                 else
194                 {
195                     firstIndex = Integer.parseInt(spec);
196                     lastIndex = firstIndex;
197                 }
198             }
199
200             if (firstIndex < min)
201             {
202                 firstIndex = min;
203             }
204
205             if (lastIndex > max)
206             {
207                 lastIndex = max;
208             }
209             
210             for (int j = firstIndex; j <= lastIndex; j ++)
211             {
212                 boolean current = mark[j];
213                 
214                 if (!current)
215                 {
216                     mark[j] = true;
217                     markCount ++;
218                 }
219             }
220         }
221
222         int[] marked = new int[markCount];
223
224         int j = 0;
225
226         for (int i = min; i < size; i ++)
227         {
228             if (mark[i])
229             {
230                 marked[j ++] = i;
231             }
232         }
233
234         return marked;
235     }
236
237     public Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
238     {
239         Folder folder = getFolder();
240
241         if (!folder.isOpen())
242         {
243             folder.open(Folder.READ_WRITE);
244         }
245
246         NestedMap map = _map;
247
248         Map controlMap = new NestedMap(map.get(CONTROL));
249         Object JavaDoc messagesSpec = map.get(MESSAGES);
250         String JavaDoc operation = map.getString(OPERATION, READ);
251
252         int[] messages = null;
253
254         if (messagesSpec instanceof NestedMap)
255         {
256             messagesSpec = ((NestedMap)messagesSpec).getSubList(false);
257         }
258
259         if (messagesSpec instanceof int[])
260         {
261             messages = (int[])messagesSpec;
262         }
263         else if (messagesSpec instanceof String JavaDoc)
264         {
265             messages = mark(messagesSpec.toString(), 1, folder.getMessageCount());
266         }
267         else if (messagesSpec != null)
268         {
269             List messageList = TypeUtil.isList(messagesSpec);
270
271             if (messageList != null)
272             {
273                 int messageListSize = messageList.size();
274
275                 messages = new int[messageListSize];
276
277                 for (int i = 0; i < messageListSize; i ++)
278                 {
279                     Object JavaDoc element = messageList.get(i);
280
281                     if (element instanceof Message)
282                     {
283                         messages[i] = ((Message)element).getMessageNumber();
284                     }
285                     else
286                     {
287                         messages[i] = TypeUtil.isInteger(messageList.get(i)).intValue();
288                     }
289                 }
290             }
291         }
292
293         if (READ.equals(operation))
294         {
295             _map.copyFromSource(MailFolder.readMessages(folder, messages, controlMap));
296         }
297         else if (DELETE.equals(operation))
298         {
299             MailFolder.deleteMessages(folder, messages);
300         }
301
302         return tagValue;
303     }
304 }
305
Popular Tags