001: package com.sun.portal.providers.mailcheck;
002:
003: import java.net.URL;
004:
005: import java.util.List;
006: import java.util.ArrayList;
007: import java.util.Hashtable;
008: import java.util.Properties;
009: import java.util.ResourceBundle;
010: import java.util.StringTokenizer;
011: import java.util.logging.Logger;
012: import java.util.logging.Level;
013:
014: import javax.mail.Store;
015: import javax.mail.Folder;
016: import javax.mail.MessagingException;
017: import javax.mail.AuthenticationFailedException;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import java.security.AccessController;
023:
024: import com.sun.identity.security.DecryptAction;
025:
026: import com.sun.portal.desktop.util.I18n;
027: import com.sun.portal.desktop.util.Target;
028: import com.sun.portal.desktop.util.Integers;
029: import com.sun.portal.desktop.util.Password;
030:
031: import com.sun.portal.desktop.context.ContextException;
032:
033: import com.sun.portal.providers.context.ProviderContext;
034: import com.sun.portal.providers.context.ProviderContextException;
035: import com.sun.portal.providers.ProviderException;
036: import com.sun.portal.providers.ProfileProviderAdapter;
037: import com.sun.portal.log.common.PortalLogger;
038:
039: public class MailCheckProvider extends ProfileProviderAdapter {
040: private static Logger logger = PortalLogger
041: .getLogger(MailCheckProvider.class);
042: private ResourceBundle bundle = null;
043: private static final String IMAP_SERVER_NAME = "IMAPServerName";
044: private static final String IMAP_USER_ID = "IMAPUserId";
045: private static final String IMAP_PASSWORD = "IMAPPassword";
046:
047: public void init(String n, HttpServletRequest req)
048: throws ProviderException {
049: super .init(n, req);
050: }
051:
052: private String UnreadMessages = "";
053:
054: public MailCheckProvider() {
055: }
056:
057: public StringBuffer getContent(HttpServletRequest req,
058: HttpServletResponse res) throws ProviderException {
059:
060: StringBuffer content = new StringBuffer();
061: Hashtable tagTable = new Hashtable();
062:
063: String server = "";
064: String user = "";
065: String pass = "";
066:
067: List userApps = null;
068: List targets = null;
069:
070: boolean configParameters = getBooleanProperty("defaultConfigParameters");
071:
072: if (configParameters) {
073: // Netmail is enabled, read parameters from there
074: server = getStringAttribute("sunPortalNetmailIMAPServerName");
075: user = getStringAttribute("sunPortalNetmailIMAPUserid");
076: String encrypted = getStringAttribute("sunPortalNetmailIMAPPassword");
077: if (encrypted == null || encrypted.length() == 0) {
078: pass = encrypted;
079: } else {
080: pass = (String) AccessController
081: .doPrivileged(new DecryptAction(encrypted));
082: }
083: } else {
084:
085: server = getStringProperty(IMAP_SERVER_NAME);
086: user = getStringProperty(IMAP_USER_ID);
087: String encoded = getStringProperty(IMAP_PASSWORD);
088: if (encoded == null || encoded.length() == 0) {
089: pass = encoded;
090: } else {
091: pass = Password.decode(encoded);
092: }
093:
094: }
095:
096: logger.log(Level.FINEST, "PSDT_CSPPM0001", new Object[] {
097: server, user });
098:
099: tagTable = getMailStatus(server, user, pass);
100:
101: //Mail Server Status
102: Integer status = Integers.get(4);
103:
104: if (tagTable.isEmpty()) {
105:
106: tagTable = new Hashtable();
107: tagTable.put("Status", status);
108: } else {
109: status = (Integer) (tagTable.get("Status"));
110: }
111:
112: String color = null;
113: String msg = null;
114: String serverNotSet = null;
115: String userNotSet = null;
116: String passwordNotSet = null;
117: boolean emptyCheck = false;
118:
119: // Get Resource Bundle if it is null
120: if (bundle == null) {
121: bundle = getResourceBundle();
122: }
123:
124: switch (status.intValue()) {
125: case 0:
126: color = bundle.getString("okColor");
127: msg = bundle.getString("alive");
128: break;
129:
130: case 1:
131: color = bundle.getString("errorColor");
132: msg = bundle.getString("notResponding");
133: break;
134:
135: case 2:
136: color = bundle.getString("warningColor");
137: msg = bundle.getString("invalidUsernamePassword");
138: break;
139:
140: case 3:
141: color = bundle.getString("warningColor");
142: tagTable.put("mailStatColor", color);
143:
144: StringBuffer mailCheckErrorMessages = new StringBuffer();
145: mailCheckErrorMessages.append("<TABLE WIDTH=\"100%\">\n");
146: serverNotSet = (String) tagTable.get("serverNotSet");
147: if (serverNotSet != null) {
148: mailCheckErrorMessages
149: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"[tag:mailStatColor]\" SIZE=\"-1\">\n");
150: mailCheckErrorMessages.append(serverNotSet);
151: mailCheckErrorMessages.append("</FONT></TD></TR>\n");
152:
153: }
154: userNotSet = (String) tagTable.get("userNotSet");
155: if (userNotSet != null) {
156: mailCheckErrorMessages
157: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"[tag:mailStatColor]\" SIZE=\"-1\">\n");
158: mailCheckErrorMessages.append(userNotSet);
159: mailCheckErrorMessages.append("</FONT></TD></TR>\n");
160: }
161: passwordNotSet = (String) tagTable.get("passwordNotSet");
162: if (passwordNotSet != null) {
163: mailCheckErrorMessages
164: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"[tag:mailStatColor]\" SIZE=\"-1\">\n");
165: mailCheckErrorMessages.append(passwordNotSet);
166: mailCheckErrorMessages.append("</FONT></TD></TR>\n");
167: }
168: mailCheckErrorMessages.append("</TABLE>");
169: tagTable.put("mailCheckErrorMessages",
170: mailCheckErrorMessages.toString());
171: emptyCheck = true;
172: break;
173:
174: default:
175: color = bundle.getString("errorColor");
176: msg = bundle.getString("MailCheckProvider");
177: break;
178:
179: }
180:
181: if (!emptyCheck) {
182: tagTable.put("mailStatColor", color);
183:
184: StringBuffer mailCheckContents = new StringBuffer();
185: mailCheckContents.append("<TABLE WIDTH=\"100%\">\n");
186: mailCheckContents
187: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"[tag:mailStatColor]\" SIZE=\"-1\">\n");
188: mailCheckContents.append(msg);
189: mailCheckContents.append("</FONT></TD></TR>\n");
190:
191: if (status.intValue() == 0) {
192: // Status is Ok, print total unread messages.
193: mailCheckContents
194: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"\" SIZE=\"-1\">\n");
195: mailCheckContents.append(UnreadMessages);
196: mailCheckContents.append("</FONT></TD></TR>\n");
197:
198: if (configParameters) {
199: // Need to read parameters from Netmail
200: // Create Netmail links
201: try {
202: userApps = new ArrayList(getProviderContext()
203: .getCollectionProperty(getName(),
204: "userApps", true).values());
205: targets = new ArrayList(getProviderContext()
206: .getCollectionProperty(getName(),
207: "targets", true).values());
208: } catch (ProviderContextException pce) {
209: throw new ProviderException(
210: "MailCheckProvider.getContent(): Error getting app properties",
211: pce);
212: }
213: StringBuffer link_content = new StringBuffer();
214: if (userApps.size() >= 1) {
215: link_content
216: .append("<TABLE CELLPADDING=1 CELLSPACING=0 WIDTH=\"100%\"><TR><TD><FONT FACE=\"[tag:fontFace1]\" SIZE=\"+0\">\n");
217: }
218:
219: for (int i = 0; i < targets.size(); i++) {
220: String targ = (String) targets.get(i);
221: Target target = new Target(targ);
222:
223: //
224: // see if they've included optional js launch code
225: //
226: StringTokenizer tok = new StringTokenizer(
227: target.getValue(), "^");
228: String url = (String) tok.nextElement();
229: StringBuffer onClick = new StringBuffer();
230: if (tok.hasMoreElements()) {
231: onClick.append((String) tok.nextElement());
232: }
233:
234: if (!userApps.contains(target.getName())) {
235: continue;
236: }
237:
238: //
239: // build the window target name from the app name
240: // remove special characters from name and replace them
241: // with underbars
242: //
243: StringBuffer windowName = new StringBuffer();
244:
245: StringTokenizer tokens = new StringTokenizer(
246: target.getName(), " (),=+#[]@<>$%./!");
247: while (tokens.hasMoreTokens()) {
248: windowName.append((String) tokens
249: .nextToken());
250: if (tokens.hasMoreElements()) {
251: windowName.append("_");
252: }
253: }
254:
255: StringBuffer winTarget = new StringBuffer()
256: .append(" TARGET=\"").append(
257: windowName.toString()).append(
258: "\"");
259:
260: if (onClick == null) {
261: onClick.append("openAppURL('").append(url)
262: .append("', '").append(windowName)
263: .append("'); return false;");
264: }
265:
266: StringBuffer theHref = new StringBuffer()
267: .append("<A HREF=\"").append(url)
268: .append("\"").append(
269: winTarget.toString()).append(
270: " onClick=\"").append(onClick)
271: .append("\">").append(target.getName())
272: .append("</A><BR>");
273:
274: String appBG = bundle
275: .getString("appBackground");
276: link_content
277: .append("<TR><TD BGCOLOR=\"")
278: .append(appBG)
279: .append("\">")
280: .append(
281: "<FONT FACE=\"[tag:fontFace1]\" ")
282: .append("SIZE=\"-1\">").append(
283: theHref.toString()).append(
284: "</FONT></TD></TR>");
285: }
286:
287: if (userApps.size() >= 1) {
288: link_content.append("</TABLE>");
289: }
290:
291: tagTable.put("apps", link_content.toString());
292: tagTable.put("fontFace1",
293: getStringProperty("fontFace1"));
294: mailCheckContents
295: .append("<TR><TD><FONT FACE=\"[tag:fontFace1]\" COLOR=\"[tag:mailStatColor]\" SIZE=\"-1\">\n");
296: mailCheckContents.append(link_content.toString());
297: mailCheckContents.append("</FONT></TD></TR>\n");
298: }
299: }
300: mailCheckContents.append("</TABLE>");
301: tagTable.put("mailCheckContents", mailCheckContents
302: .toString());
303: }
304:
305: content = getTemplate("display.template", tagTable);
306:
307: return content;
308: }
309:
310: /**
311: * getMailStatus
312: *
313: * Returns the status of the mail server,
314: * and the mail counts for the specified user
315: *
316: * @param String server
317: * @param String user
318: * @param String password
319: * @return Hashtable contain results
320: */
321: private Hashtable getMailStatus(String mailsrvr, String user,
322: String password) throws ProviderException {
323: Hashtable passMailStatus;
324: passMailStatus = new Hashtable();
325: //
326: // put default vals in in case exception is thrown
327: //
328: passMailStatus.put("mailStatColor", "");
329: passMailStatus.put("mailCount", "");
330:
331: String mbox = "INBOX";
332: // Get Resource Bundle if it is null
333: if (bundle == null) {
334: bundle = getResourceBundle();
335: }
336: String mailCountMessage = bundle.getString("unreadMessages");
337:
338: javax.mail.Session session = null;
339: Store store = null;
340: boolean checkEmpty = false;
341:
342: if ((mailsrvr == null) || (mailsrvr.length() == 0)) {
343: passMailStatus.put("serverNotSet", bundle
344: .getString("serverNotSet"));
345: checkEmpty = true;
346: }
347: if ((user == null) || (user.length() == 0)) {
348: passMailStatus.put("userNotSet", bundle
349: .getString("userNotSet"));
350: checkEmpty = true;
351: }
352: if ((password == null) || (password.length() == 0)) {
353: passMailStatus.put("passwordNotSet", bundle
354: .getString("passwordNotSet"));
355: checkEmpty = true;
356: }
357:
358: if (checkEmpty) {
359: Integer stat = Integers.get(3);
360: passMailStatus.put("Status", stat);
361: return passMailStatus;
362: } else {
363: try {
364: Properties props = System.getProperties();
365: props.put("mail.host", mailsrvr);
366: props.put("mail.user", user);
367:
368: // ****************************************************************
369: // Get the default Session object.
370: // ****************************************************************
371:
372: session = javax.mail.Session.getInstance(props, null);
373:
374: // ****************************************************************
375: // Get a Store object that implements the IMAP protocol.
376: // ****************************************************************
377:
378: store = session.getStore("imap");
379:
380: // ****************************************************************
381: // Connect to 'host' as 'user'.
382: // ****************************************************************
383:
384: store.connect(mailsrvr, user, password);
385:
386: // ****************************************************************
387: // Open the specified Folder.
388: // ****************************************************************
389:
390: Folder folder = store.getFolder(mbox);
391:
392: // ****************************************************************
393: // Retrieve message counts - do not need to open folder to do this.
394: // ****************************************************************
395:
396: int totalUnreadMessages = folder
397: .getUnreadMessageCount();
398: String unreadPattern = bundle
399: .getString("unreadMessages");
400: UnreadMessages = I18n.format(unreadPattern, Integers
401: .get(totalUnreadMessages), getProviderContext()
402: .getLocale());
403:
404: Integer stat = Integers.get(0);
405: passMailStatus.put("Status", stat);
406:
407: store.close();
408:
409: return passMailStatus;
410:
411: } catch (AuthenticationFailedException a) {
412: Integer stat = Integers.get(2);
413: passMailStatus.put("Status", stat);
414: logger.log(Level.FINE, "PSDT_CSPPM0002", a);
415:
416: } catch (MessagingException me) {
417: Integer stat = Integers.get(1);
418: passMailStatus.put("Status", stat);
419:
420: logger.log(Level.FINE, "PSDT_CSPPM0002", me);
421:
422: } finally {
423: try {
424: if (store != null) {
425: store.close();
426: }
427: } catch (MessagingException me2) {
428: logger.log(Level.FINE, "PSDT_CSPPM0002", me2);
429: throw new ProviderException(
430: "error closing mail store", me2);
431: }
432:
433: return passMailStatus;
434: }
435: }
436: }
437:
438: public StringBuffer getEdit(HttpServletRequest req,
439: HttpServletResponse res) throws ProviderException {
440: // string buffer to hold the edit page markup
441: StringBuffer content = new StringBuffer();
442: // table will all the tags
443: Hashtable tagTable = new Hashtable();
444:
445: //Read the properties from the user display profile
446: tagTable.put("fontFace1", getStringProperty("fontFace1"));
447: tagTable.put(IMAP_SERVER_NAME,
448: getStringProperty(IMAP_SERVER_NAME));
449: tagTable.put(IMAP_USER_ID, getStringProperty(IMAP_USER_ID));
450: tagTable.put(IMAP_PASSWORD, getStringProperty(IMAP_PASSWORD));
451:
452: // load the template and swap the tags
453: content = getTemplate("edit.template", tagTable);
454:
455: return content;
456: }
457:
458: public URL processEdit(HttpServletRequest req,
459: HttpServletResponse res) throws ProviderException {
460:
461: getProviderContext().contentChanged(getName());
462:
463: String server = req.getParameter(IMAP_SERVER_NAME);
464: //if the parameter is not found, set it to empty string
465: if (server == null) {
466: server = "";
467: }
468: String user = req.getParameter(IMAP_USER_ID);
469: //if the parameter is not found, set it to empty string
470: if (user == null) {
471: user = "";
472: }
473: String pass = req.getParameter(IMAP_PASSWORD);
474: //if the parameter is not found, set it to empty string
475: if (pass == null) {
476: pass = "";
477: }
478:
479: // Set the value, only if the property is changed
480: if (!server.equals(getStringProperty(IMAP_SERVER_NAME))) {
481: setStringProperty(IMAP_SERVER_NAME, server);
482: }
483: if (!user.equals(getStringProperty(IMAP_USER_ID))) {
484: setStringProperty(IMAP_USER_ID, user);
485: }
486:
487: if (!pass.equals(getStringProperty(IMAP_PASSWORD))) {
488: if (pass.length() == 0) {
489: setStringProperty(IMAP_PASSWORD, pass);
490: } else {
491: setStringProperty(IMAP_PASSWORD, Password.encode(pass));
492: }
493: }
494:
495: return null;
496: }
497:
498: }
|