001: package com.sun.portal.comm.url;
002:
003: import com.sun.ssoadapter.SSOAdapter;
004:
005: import java.io.BufferedReader;
006: import java.io.InputStreamReader;
007:
008: import java.net.HttpURLConnection;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011:
012: public class SunABURLBuilder extends URLBuilder implements ABURL {
013:
014: protected String sid = null;
015:
016: public void init(SSOAdapter ssoAdapter) {
017: super .init(ssoAdapter);
018: if (!serverSSOEnabled) {
019: getSession();
020: }
021: }
022:
023: /**
024: * Checks whether the current sid for Messenger Express has
025: * timed out. This is done by checking if the page returned by sending a Get
026: * Mail WMAP command to messenger express contains string
027: * "parent.timeoutCB()". If yes, then the webmailSessionId has timed out.
028: *
029: *@return boolean
030: */
031: public boolean isValidSession() {
032:
033: if (sid == null) {
034: return false;
035: }
036: StringBuffer pageContent = new StringBuffer();
037:
038: // Build the WMAP protocol's Get Mail command
039: // Get 0 messages (count=0) to reduce the traffic
040: StringBuffer urlstr = new StringBuffer(baseURL);
041: urlstr
042: .append("/mbox.msc?security=false&mbox=INBOX&start=0&count=0&date=false");
043: urlstr.append("&sid=");
044: urlstr.append(encode(sid));
045:
046: HttpURLConnection urlconn = null;
047: try {
048: URL url = new URL(urlstr.toString());
049: urlconn = (HttpURLConnection) url.openConnection();
050: urlconn.setInstanceFollowRedirects(false);
051: BufferedReader in = new BufferedReader(
052: new InputStreamReader(urlconn.getInputStream()));
053: String inputLine = null;
054: // Getting the content of the page
055: while ((inputLine = in.readLine()) != null) {
056: pageContent.append(inputLine);
057: }
058: in.close();
059: } catch (java.io.IOException ioe) {
060: return false;
061: }
062: // Check if the page contents contain string "parent.timeoutCB()"
063: // If yes, then the webmailSessionId has timed out
064: if ((pageContent.indexOf("parent.timeoutCB()")) > 0) {
065: sid = null;
066: return false;
067: }
068: return true;
069: }
070:
071: /**
072: * Authenticate the user to Webmail / Messenger Express. If the return
073: * cookie contains 'sid' then the user is considered authenticated.
074: * Also sets the path to the authenticated location with the sid.
075: *
076: **/
077: public void getSession() {
078:
079: // Build WMAP protocol's Login command
080: StringBuffer urlstr = new StringBuffer(baseURL);
081: urlstr.append("/login.msc?");
082: urlstr.append("password=");
083: urlstr.append(encode(password));
084: urlstr.append("&user=");
085: urlstr.append(encode(user));
086:
087: if (useProxyAuth) {
088: if ((proxyAuthUid != null) && (proxyAuthUid.length() > 0)) {
089: urlstr.append("&proxyauth=");
090: urlstr.append(encode(proxyAuthUid));
091: }
092: }
093:
094: if ((domain != null) && (domain.length() > 0)) {
095: urlstr.append("%40"); // URL encoding value for '@'
096: urlstr.append(encode(domain));
097: }
098:
099: HttpURLConnection urlconn = null;
100: try {
101: URL url = new URL(urlstr.toString());
102: urlconn = (HttpURLConnection) url.openConnection();
103: urlconn.setInstanceFollowRedirects(false);
104: } catch (MalformedURLException mfe) {
105: return;
106: } catch (java.io.IOException ioe) {
107: return;
108: }
109: // Check if the headers of the response contain a Location field with
110: // parameter 'sid' set.
111: String location = urlconn.getHeaderField("Location");
112: if (location != null) {
113: int sessionIdBegin = 0;
114: if ((sessionIdBegin = location.indexOf("sid")) < 0) {
115: // cannot authenticate to the Messenger Express
116: return;
117: } else {
118: setPath(location + "&view=portal");
119: String inter = location.substring(sessionIdBegin + 4);
120: int sessionIdEnd = inter.indexOf("&");
121: sid = inter.substring(0, sessionIdEnd);
122: }
123: }
124: }
125:
126: /**
127: * Lets invoking classes know if contact urls are available
128: * in this URLBuilder.
129: *
130: * @return boolean Are contact URLs available
131: */
132: public boolean allowsContactURL() {
133: return false;
134: }
135:
136: /**
137: * Return URL string for specific contact to be opened in ab client.
138: *
139: * @param Object contact
140: * @return String View URL string
141: */
142: public String getContactURL(Object contact) {
143: return getStartURL();
144: }
145:
146: /**
147: * Return the current ME session ID
148: *
149: *@return String The ME session ID
150: */
151: public String getSid() {
152: return sid;
153: }
154:
155: /**
156: * Return the current session ID
157: *
158: *@param String The session ID
159: */
160: public void setSid(String sid) {
161: this.sid = sid;
162: }
163:
164: }
|