KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > MailContext


1 /*
2  * Copyright 1999-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.cocoon.mail;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import javax.mail.Folder JavaDoc;
22 import javax.mail.MessagingException JavaDoc;
23 import javax.mail.Store JavaDoc;
24 //import javax.mail.Session;
25
import org.apache.avalon.framework.context.Context;
26 import org.apache.avalon.framework.context.ContextException;
27 import org.apache.avalon.framework.context.DefaultContext;
28 import org.apache.avalon.framework.logger.Logger;
29 import org.apache.avalon.framework.logger.LogEnabled;
30 import org.apache.cocoon.environment.Request;
31
32 /**
33  * Encapsulation of context info of this webmail application
34  *
35  * @author Bernhard Huber
36  * @since 29 December 2002
37  * @version CVS $Id: MailContext.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public class MailContext extends DefaultContext implements LogEnabled {
40     /**
41      * attribute name of MailContext object in an application session, eg http-session
42      */

43     public final static String JavaDoc SESSION_MAIL_CONTEXT = "mail-context";
44
45     /**
46      * Description of the Field
47      */

48     public final static String JavaDoc MAIL_SESSION_ENTRY = "mail-session";
49     /**
50      * Description of the Field
51      */

52     public final static String JavaDoc MAIL_STORE_ENTRY = "mail-store";
53
54     /**
55      * Description of the Field
56      */

57     public final static String JavaDoc MAIL_CURRENT_WORKING_FOLDER_ENTRY = "mail-current-working-folder";
58     /**
59      * Description of the Field
60      */

61     public final static String JavaDoc MAIL_CURRENT_WORKING_COMMAND_ENTRY = "mail-current-working-command";
62
63     private Request request;
64     private Logger logger;
65
66
67     /**
68      *Constructor for the MailContext object
69      *
70      *@param parent Description of the Parameter
71      */

72     MailContext(Context parent) {
73         super(parent);
74     }
75
76
77     /**
78      * Sets the request attribute of the MailContext object
79      *
80      *@param request The new request value
81      */

82     public void setRequest(Request request) {
83         this.request = request;
84     }
85
86
87     /**
88      * A specialization of the plain Context get method.
89      * <p>
90      * Implementing special key prefixes
91      * </p>
92      * <ul>
93      * <li>"param:key" get key from request parameters
94      * </li>
95      * <li>"param-integer:key" get key from request parameters, casting to Integer
96      * </li>
97      * <li>"param-folder:key" get key from request parameters, ie foldername, lookup
98      * foldername using key "folder:foldername"
99      * </li>
100      * <li>key get key via plain context get method
101      * </li>
102      * </ul>
103      *
104      *@param key Description of the Parameter
105      *@return Description of the Return Value
106      *@exception ContextException Description of the Exception
107      */

108     public Object JavaDoc get(Object JavaDoc key) throws ContextException {
109         String JavaDoc keyString = (String JavaDoc) key;
110
111         final String JavaDoc PARAM_PREFIX_ENTRY = "param:";
112         final String JavaDoc PARAM_INTEGER_PREFIX_ENTRY = "param-integer:";
113         final String JavaDoc PARAM_FOLDER_PREFIX_ENTRY = "param-folder:";
114
115         if (keyString.startsWith(PARAM_PREFIX_ENTRY)) {
116             String JavaDoc paramName = keyString.substring(PARAM_PREFIX_ENTRY.length());
117             String JavaDoc paramValue = getParameter(paramName);
118             if (paramValue == null) {
119                 String JavaDoc message = "No parameter " + String.valueOf(keyString) + " available.";
120                 throw new ContextException(message);
121             }
122             return paramValue;
123         } else if (keyString.startsWith(PARAM_INTEGER_PREFIX_ENTRY)) {
124             String JavaDoc paramName = keyString.substring(PARAM_INTEGER_PREFIX_ENTRY.length());
125             try {
126                 Integer JavaDoc paramValue = getParameterAsInteger(paramName);
127                 return paramValue;
128             } catch (NumberFormatException JavaDoc nfe) {
129                 String JavaDoc message = "Cannot create Integer for parameter " + String.valueOf(keyString);
130                 throw new ContextException(message, nfe);
131             }
132         } else if (keyString.startsWith(PARAM_FOLDER_PREFIX_ENTRY)) {
133             String JavaDoc paramName = keyString.substring(PARAM_FOLDER_PREFIX_ENTRY.length());
134             String JavaDoc folderName = getParameter(paramName);
135             if (folderName == null) {
136                 // no folderName is available in the parameters bag
137
// try to get the current working folder
138
try {
139                     folderName = (String JavaDoc) super.get(MAIL_CURRENT_WORKING_FOLDER_ENTRY);
140                 } catch (ContextException ce) {
141                     // no current working folder entry available
142
String JavaDoc message = "No " + MAIL_CURRENT_WORKING_FOLDER_ENTRY + " entry available ";
143                     getLogger().error(message);
144                     throw new ContextException(message, ce);
145                 }
146             }
147
148             // get folder object, folderName is okay
149
Folder JavaDoc folder = null;
150             try {
151                 folder = (Folder JavaDoc) getFolder(folderName);
152             } catch (ContextException ce) {
153                 // folder is not stored yet
154

155                 Store JavaDoc store = (Store JavaDoc) get(MAIL_STORE_ENTRY);
156                 // get folder, eventually connect the store
157
try {
158                     if (!store.isConnected()) {
159                         store.connect();
160                     }
161                     final String JavaDoc DEFAULT_FOLDER_NAME = "~";
162
163                     // empty folder name is specified by empty string, or "~"
164
if (folderName.equals(DEFAULT_FOLDER_NAME) || folderName.length() == 0) {
165                         folder = store.getDefaultFolder();
166                     } else {
167                         folder = store.getFolder(folderName);
168                     }
169
170                     // save the Folder, for later access
171
putFolder(folder);
172                 } catch (MessagingException JavaDoc me) {
173                     String JavaDoc message = "Cannot get folder " + String.valueOf(folderName);
174                     throw new ContextException(message, ce);
175                 }
176             }
177             return folder;
178         } else {
179             return super.get(key);
180         }
181     }
182
183
184     /**
185      * Gets the theFolder attribute of the MailContext object
186      *
187      *@param entry Description of the Parameter
188      *@return The theFolder value
189      *@exception ContextException Description of the Exception
190      */

191     public Folder JavaDoc getTheFolder(String JavaDoc entry) throws ContextException {
192         Folder JavaDoc f;
193         try {
194             f = (Folder JavaDoc) get("param-folder:" + entry);
195         } catch (Exception JavaDoc e) {
196             String JavaDoc message = "Cannot get Folder object for " + String.valueOf(entry);
197             throw new ContextException(message, e);
198         }
199         return f;
200     }
201
202
203     /**
204      * Gets the folder attribute of the MailContext object
205      *
206      *@param folderName Description of the Parameter
207      *@return The folder value
208      *@exception ContextException Description of the Exception
209      */

210     public Object JavaDoc getFolder(String JavaDoc folderName) throws ContextException {
211         // canonicalize folder name
212
folderName = canoncializeFoldername(folderName);
213
214         final String JavaDoc key = "folder:" + folderName;
215         getLogger().debug("Getting folder " + String.valueOf(key));
216
217         Object JavaDoc o = super.get(key);
218         getLogger().debug("Successfully getting folder " + String.valueOf(key) + ": " + String.valueOf(o));
219         return o;
220     }
221
222
223     /**
224      * Remove and close Store of this MailContext, implicitly remove all folders, too.
225      */

226     public void removeStore() {
227         try {
228             getLogger().info("Remove store " + String.valueOf(this));
229             removeAllFolders();
230
231             Map JavaDoc map = getContextData();
232             Store JavaDoc store = (Store JavaDoc) map.remove(MAIL_STORE_ENTRY);
233             if (store != null) {
234                 MailCommandManager.closeStore(store);
235             }
236         } catch (Exception JavaDoc e) {
237             String JavaDoc message = "Cannot remove store";
238             getLogger().error(message, e);
239         }
240     }
241
242
243     /**
244      * remove all folders in this MailContext object
245      */

246     public void removeAllFolders() {
247         try {
248             getLogger().info("Remove folders " + String.valueOf(this));
249
250             Map JavaDoc map = getContextData();
251             Set JavaDoc entrySet = map.entrySet();
252             Iterator JavaDoc i = entrySet.iterator();
253             while (i.hasNext()) {
254                 Map.Entry JavaDoc me = (Map.Entry JavaDoc) i.next();
255                 String JavaDoc key = (String JavaDoc) me.getKey();
256                 if (key.startsWith("folder:")) {
257                     Folder JavaDoc f = (Folder JavaDoc) me.getValue();
258                     MailCommandManager.closeFolder(f);
259                     i.remove();
260                 }
261             }
262         } catch (Exception JavaDoc e) {
263             String JavaDoc message = "Cannot remove all folders";
264             getLogger().error(message, e);
265         }
266     }
267
268
269     /**
270      * put a folder in this MailContext object map
271      *
272      *@param folder Description of the Parameter
273      *@exception ContextException Description of the Exception
274      */

275     public void putFolder(Folder JavaDoc folder) throws ContextException {
276         String JavaDoc folderName = folder.getFullName();
277         // canonicalize folder name
278
folderName = canoncializeFoldername(folderName);
279
280         final String JavaDoc key = "folder:" + folderName;
281
282         getLogger().debug("Putting folder key: " + String.valueOf(key) +
283                 " folder " + String.valueOf(folder));
284
285         // close folder if folder is overwritten
286
try {
287             Object JavaDoc objRef = super.get(key);
288             if (objRef != null) {
289                 // close this folder as it is goint to get overwritten
290
try {
291                     Folder JavaDoc f = (Folder JavaDoc) objRef;
292                     MailCommandManager.closeFolder(f);
293                 } catch (MessagingException JavaDoc me) {
294                     String JavaDoc message = "Cannot close folder";
295                     getLogger().warn(message, me);
296                 }
297             }
298         } catch (ContextException e) {
299             // ignore as we set it
300
}
301
302         // Shall we garbage collect folders?
303

304         super.put(key, folder);
305     }
306
307
308     /**
309      * Description of the Method
310      *
311      *@param folders Description of the Parameter
312      *@exception ContextException Description of the Exception
313      */

314     public void putFolder(Folder JavaDoc[] folders) throws ContextException {
315         for (int i = 0; i < folders.length; i++) {
316             putFolder(folders[i]);
317         }
318     }
319
320
321     /**
322      * Description of the Method
323      *
324      *@param logger Description of the Parameter
325      */

326     public void enableLogging(Logger logger) {
327         this.logger = logger;
328     }
329
330
331
332     /**
333      * Gets the parameter attribute of the MailContext object
334      *
335      *@param key Description of the Parameter
336      *@return The parameter value
337      */

338     protected String JavaDoc getParameter(String JavaDoc key) {
339         String JavaDoc value = request.getParameter(key);
340         return value;
341     }
342
343
344     /**
345      * Gets the parameterAsInteger attribute of the MailContext object
346      *
347      *@param key Description of the Parameter
348      *@return The parameterAsInteger value
349      */

350     protected Integer JavaDoc getParameterAsInteger(String JavaDoc key) {
351         String JavaDoc value = request.getParameter(key);
352         Integer JavaDoc i = new Integer JavaDoc(value);
353         return i;
354     }
355
356
357     /**
358      * Gets the logger attribute of the MailContext object
359      *
360      *@return The logger value
361      */

362     protected Logger getLogger() {
363         return this.logger;
364     }
365
366
367     /**
368      * Description of the Method
369      *
370      *@param fn Description of the Parameter
371      *@return Description of the Return Value
372      */

373     protected String JavaDoc canoncializeFoldername(String JavaDoc fn) {
374         //
375
return fn;
376     }
377
378 }
379
380
Popular Tags