001: /*
002: *
003: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
004: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
005: */
006:
007: package com.sun.portal.comm.url;
008:
009: import com.sun.ssoadapter.SSOAdapter;
010:
011: import java.io.BufferedReader;
012: import java.io.InputStreamReader;
013:
014: import java.net.HttpURLConnection;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017:
018: import javax.mail.Address;
019: import javax.mail.Folder;
020: import javax.mail.Message;
021: import javax.mail.MessagingException;
022: import javax.mail.UIDFolder;
023:
024: public class SunMailURLBuilder extends URLBuilder implements MailURL {
025:
026: protected String sid = null;
027:
028: public void init(SSOAdapter ssoAdapter) {
029: super .init(ssoAdapter);
030: if (!serverSSOEnabled) {
031: getSession();
032: }
033: }
034:
035: /**
036: * Checks whether the current sid for Messenger Express has
037: * timed out. This is done by checking if the page returned by sending a Get
038: * Mail WMAP command to messenger express contains string
039: * "parent.timeoutCB()". If yes, then the webmailSessionId has timed out.
040: *
041: *@return boolean
042: */
043: public boolean isValidSession() {
044:
045: if (sid == null) {
046: return false;
047: }
048: StringBuffer pageContent = new StringBuffer();
049:
050: // Build the WMAP protocol's Get Mail command
051: // Get 0 messages (count=0) to reduce the traffic
052: StringBuffer urlstr = new StringBuffer(baseURL);
053: urlstr
054: .append("/mbox.msc?security=false&mbox=INBOX&start=0&count=0&date=false");
055: urlstr.append("&sid=");
056: urlstr.append(encode(sid));
057:
058: HttpURLConnection urlconn = null;
059: try {
060: URL url = new URL(urlstr.toString());
061: urlconn = (HttpURLConnection) url.openConnection();
062: urlconn.setInstanceFollowRedirects(false);
063: } catch (java.io.IOException ioe) {
064: return false;
065: }
066: try {
067: BufferedReader in = new BufferedReader(
068: new InputStreamReader(urlconn.getInputStream()));
069: String inputLine = null;
070: // Getting the content of the page
071: while ((inputLine = in.readLine()) != null) {
072: pageContent.append(inputLine);
073: }
074: in.close();
075: } catch (java.io.IOException ioe) {
076: return false;
077: }
078: // Check if the page contents contain string "parent.timeoutCB()"
079: // If yes, then the webmailSessionId has timed out
080: if ((pageContent.indexOf("parent.timeoutCB()")) > 0) {
081: sid = null;
082: return false;
083: }
084: return true;
085: }
086:
087: /**
088: * Authenticate the user to Webmail / Messenger Express. If the return
089: * cookie contains 'sid' then the user is considered authenticated.
090: * Also sets the path to the authenticated location with the sid.
091: *
092: **/
093: public void getSession() {
094:
095: // Build WMAP protocol's Login command
096: StringBuffer urlstr = new StringBuffer(baseURL);
097: urlstr.append("/login.msc?");
098: urlstr.append("password=");
099: urlstr.append(encode(password));
100: urlstr.append("&user=");
101: urlstr.append(encode(user));
102:
103: if (useProxyAuth) {
104: if ((proxyAuthUid != null) && (proxyAuthUid.length() > 0)) {
105: urlstr.append("&proxyauth=");
106: urlstr.append(encode(proxyAuthUid));
107: }
108: }
109:
110: if ((domain != null) && (domain.length() > 0)) {
111: urlstr.append("%40"); // URL encoding value for '@'
112: urlstr.append(encode(domain));
113: }
114:
115: HttpURLConnection urlconn = null;
116: try {
117: URL url = new URL(urlstr.toString());
118: urlconn = (HttpURLConnection) url.openConnection();
119: urlconn.setInstanceFollowRedirects(false);
120: } catch (MalformedURLException mfe) {
121: return;
122: } catch (java.io.IOException ioe) {
123: return;
124: }
125: // Check if the headers of the response contain a Location field with
126: // parameter 'sid' set.
127: String location = urlconn.getHeaderField("Location");
128: if (location != null) {
129: int sessionIdBegin = 0;
130: if ((sessionIdBegin = location.indexOf("sid")) < 0) {
131: // cannot authenticate to the Messenger Express
132: return;
133: } else {
134: setPath(location + "&view=portal");
135: String inter = location.substring(sessionIdBegin + 4);
136: int sessionIdEnd = inter.indexOf("&");
137: sid = inter.substring(0, sessionIdEnd);
138: }
139: }
140: }
141:
142: /**
143: * Lets invoking classes know if per-message urls are available
144: * in this URLBuilder.
145: *
146: * @return boolean Are message URLs available
147: */
148: public boolean allowsMessageURL() {
149: return true;
150: }
151:
152: /**
153: * Return URL string for specific message to be opened in mail client.
154: * On error return the start URL
155: *
156: * @param javax.mail.Message The message to open
157: * @return String Message URL string
158: */
159: public String getMessageURL(Message message) {
160: if (message == null) {
161: return getStartURL();
162: }
163: StringBuffer msgURL = new StringBuffer(getBaseURL());
164: Folder folder = message.getFolder();
165: if (getServerSSOEnabled()) {
166: // In order to enable Messenger Express SSO, must access
167: // proper message using format server/?argv=mbox=BOX&argv=msg=n
168: msgURL.append("/?argv=mbox=");
169: msgURL.append(encode(message.getFolder().getFullName()));
170: msgURL.append("&argv=msg=");
171: try {
172: msgURL.append(((UIDFolder) folder).getUID(message));
173: } catch (MessagingException me) {
174: return getStartURL();
175: }
176: } else {
177: msgURL.append(getPath());
178: msgURL.append("&mbox=");
179: msgURL.append(encode(message.getFolder().getFullName()));
180: msgURL.append("&msg=");
181: try {
182: msgURL.append(((UIDFolder) folder).getUID(message));
183: } catch (MessagingException me) {
184: return getStartURL();
185: }
186: }
187: return msgURL.toString();
188: }
189:
190: /**
191: * Lets invoking classes know if composing URLs are available
192: * in this URLBuilder.
193: *
194: * @return boolean Are compisition URLs available
195: */
196: public boolean allowsComposeURL() {
197: return false;
198: }
199:
200: /**
201: * Return URL string to open the client's compose window.
202: *
203: * @param String Subject of the message
204: * @param javax.mail.Address[] Array of to: addresses
205: * @param javax.mail.Address[] Array of cc: addresses
206: * @param javax.mail.Address[] Array of bcc: addresses
207: * @return String Composition URL string
208: */
209: public String getComposeURL(String subject, Address[] to,
210: Address[] cc, Address[] bcc) {
211: return getStartURL();
212: }
213:
214: /**
215: * Return the current ME session ID
216: *
217: *@return String The ME session ID
218: */
219: public String getSid() {
220: return sid;
221: }
222:
223: /**
224: * Return the current session ID
225: *
226: *@param String The session ID
227: */
228: public void setSid(String sid) {
229: this.sid = sid;
230: }
231:
232: }
|