001: package com.sun.portal.subscriptions.profiler;
002:
003: import com.sun.portal.subscriptions.profiler.cli.ProfilerCmd;
004:
005: import com.iplanet.am.sdk.AMStoreConnection;
006: import com.iplanet.am.sdk.AMUser;
007: import com.iplanet.sso.SSOToken;
008: import com.iplanet.sso.SSOException;
009: import com.iplanet.am.sdk.AMException;
010: import com.iplanet.am.util.AdminUtils;
011:
012: import java.net.InetAddress;
013: import java.util.Properties;
014: import java.util.Date;
015: import java.net.*;
016: import javax.mail.*;
017: import javax.mail.internet.*;
018: import com.sun.portal.subscriptions.providers.Subscription;
019: import com.sun.portal.subscriptions.providers.SearchSubscription;
020:
021: import java.util.*;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024: import java.util.logging.Logger;
025: import java.text.MessageFormat;
026: import java.io.*;
027: import java.security.AccessController;
028: import com.sun.identity.security.EncryptAction;
029: import com.sun.portal.search.soif.*;
030: import com.sun.portal.log.common.PortalLogger;
031: import com.sun.portal.admin.common.util.AdminUtil;
032:
033: public class ProfilerWorker implements Runnable {
034: private AMStoreConnection dsameCon;
035: private UserQueue users;
036: private NotificationQueue nq;
037: private Properties profilerProps;
038: private Properties notifierProps;
039: private SSOToken ssoToken;
040: private String uid;
041: private String pwd;
042: private static Logger logger = PortalLogger
043: .getLogger(ProfilerWorker.class);
044:
045: public ProfilerWorker(AMStoreConnection c, SSOToken sso,
046: String uid, String pwd, UserQueue uq, NotificationQueue nq,
047: Properties p, Properties n) {
048: this .dsameCon = c;
049: this .ssoToken = sso;
050: this .uid = uid;
051: this .pwd = pwd;
052: this .users = uq;
053: this .nq = nq;
054: this .profilerProps = p;
055: this .notifierProps = n;
056: }
057:
058: public void run() {
059: long startTime = System.currentTimeMillis();
060: long interTime = startTime;
061:
062: // execute profiles for each user in the set
063: // looping over the users
064: String dnStr = null;
065: while ((dnStr = (String) users.get()) != null) {
066: profile(dnStr);
067: }
068: }
069:
070: /*
071: * called for each user this execute the profile, and prepare the result
072: * for delivery it uses internally the private method that does formatting
073: * of the result passed on for delivery
074: *
075: * algo is : - get the user dn - create a notification obj - profile the dn
076: * updating the notification obj - push the notification in the
077: * notification queue for delivery - if error, put back the user back in
078: * the user queue
079: */
080:
081: private void profile(String dnStr) {
082: // generate from remote access to desktop Profiler channel with user impersonation
083: Notification n = null;
084: AMUser user = null;
085: boolean hasSubs = false;
086: String uEmail = null;
087: boolean profilerEnabled = false;
088: String portalId = profilerProps.getProperty("portalId");
089: if (portalId == null
090: || portalId.equals(AdminUtil.UPGRADED_PORTAL)) {
091: // Upgraded portal's have a subscriptions service which has a "%PORTALID% empty
092: portalId = "";
093: }
094: try {
095: user = dsameCon.getUser(dnStr);
096: hasSubs = user.getAssignedServices().contains(
097: "SunPortal" + portalId + "SubscriptionsService");
098: try {
099: uEmail = (String) user.getStringAttribute("mail");
100: String enabled = user.getStringAttribute("sunPortal"
101: + portalId + "ProfilerEnabled");
102: profilerEnabled = (enabled == null) ? true : Boolean
103: .valueOf(enabled).booleanValue();
104: } catch (AMException ame) {
105: } catch (SSOException ssoe) {
106: }
107:
108: if (profilerEnabled == false || uEmail == null) {
109: if (getLogger().isLoggable(Level.FINE))
110: getLogger().log(Level.FINE, "PSSS_CSPP0002", dnStr);
111: } else if (hasSubs) {
112: // create an notification object, empty of content
113: n = new Notification(user, null, null,
114: Notification.MEDIUM_DEFAULT);
115: // render the profile via the JSP profiler Channel
116: URL u;
117: try {
118: u = new URL(profilerProps
119: .getProperty("profilerProvider")
120: + "&action=content");
121: //building post data
122: StringBuffer postData = new StringBuffer();
123: // adding proxyAuth
124: postData.append(URLEncoder.encode("proxyAuth",
125: "UTF-8"));
126: postData.append("=");
127: postData
128: .append(URLEncoder
129: .encode((String) AccessController
130: .doPrivileged(new EncryptAction(
131: new String(
132: AdminUtils
133: .getAdminPassword())))));
134: // adding maxHits
135: postData.append("&"
136: + URLEncoder.encode("maxHits", "UTF-8"));
137: postData.append("=");
138: postData.append(URLEncoder.encode(profilerProps
139: .getProperty("profilerMaxHits"), "UTF-8"));
140: // adding proxyDN
141: postData.append("&"
142: + URLEncoder.encode("proxyDN", "UTF-8"));
143: postData.append("=");
144: postData.append(URLEncoder.encode(dnStr, "UTF-8"));
145: // adding defaultSearchServer
146: postData.append("&"
147: + URLEncoder.encode("defSrchSvr", "UTF-8"));
148: postData.append("=");
149: postData
150: .append(URLEncoder
151: .encode(
152: profilerProps
153: .getProperty("profilerDefaultSearchServer"),
154: "UTF-8"));
155: Set categoriesSet = (Set) user
156: .getAttribute("sunPortal" + portalId
157: + "CategorySubscriptions");
158: Set discussionsSet = (Set) user
159: .getAttribute("sunPortal" + portalId
160: + "DiscussionSubscriptions");
161: Set savedSearchSet = (Set) user
162: .getAttribute("sunPortal" + portalId
163: + "SavedSearch");
164: String profilerLastRun = (String) user
165: .getStringAttribute("sunPortal" + portalId
166: + "ProfilerLastRun");
167: // adding profiler Last Run
168: postData.append("&"
169: + URLEncoder.encode("profilerLastRun",
170: "UTF-8"));
171: postData.append("=");
172: postData.append(URLEncoder.encode(profilerLastRun,
173: "UTF-8"));
174: // adding subscriptions
175: Iterator ci = categoriesSet.iterator();
176: while (ci.hasNext()) {
177: postData
178: .append("&cat="
179: + URLEncoder.encode((String) ci
180: .next()));
181: }
182: ci = discussionsSet.iterator();
183: while (ci.hasNext()) {
184: postData
185: .append("&disc="
186: + URLEncoder.encode((String) ci
187: .next()));
188: }
189: ci = savedSearchSet.iterator();
190: while (ci.hasNext()) {
191: postData
192: .append("&srch="
193: + URLEncoder.encode((String) ci
194: .next()));
195: }
196: URLConnection connection = u.openConnection();
197: //connection.setRequestMethod("POST");
198: connection.setAllowUserInteraction(false); // system may not ask the user
199: connection.setDoOutput(true); // we want to send things
200: connection.setDoInput(true);
201: connection.setUseCaches(false);
202: connection.setRequestProperty("Content-Type",
203: "application/x-www-form-urlencoded");
204:
205: // get the output stream to POST
206: OutputStream rawOutStream = connection
207: .getOutputStream();
208:
209: // write argument
210: DataOutputStream dos = new DataOutputStream(
211: rawOutStream);
212: dos.writeBytes(postData.toString());
213: dos.flush();
214: dos.close();
215:
216: // explicitly connect to the server.
217: String emptyResult = connection
218: .getHeaderField("empty");
219: String unauthorized = connection
220: .getHeaderField("unauthorized");
221: if (unauthorized != null
222: && unauthorized.equals("true")) {
223: getLogger().log(Level.FINE, "PSSS_CSPP0015",
224: user.getDN());
225: } else if (emptyResult != null
226: && emptyResult.equals("false")) {
227: InputStream stream = connection
228: .getInputStream();
229: BufferedInputStream in = new BufferedInputStream(
230: stream);
231: ByteArrayOutputStream out = new ByteArrayOutputStream();
232: int i;
233: while ((i = in.read()) != -1) {
234: out.write(i);
235: }
236: out.flush();
237: //populating the notification with the profile output
238: format(0, n, out.toString("UTF-8"), null);
239: out.close();
240: in.close();
241: stream.close();
242: // pushing the notification in notification Queue
243: nq.put(n);
244: } else {
245: getLogger().log(Level.FINE, "PSSS_CSPPC0011",
246: user.getDN());
247: }
248: } catch (Exception e) {
249: if (getLogger().isLoggable(Level.SEVERE))
250: getLogger().log(Level.SEVERE, "PSSS_CSPP0003",
251: e);
252: }
253: } else {
254: if (getLogger().isLoggable(Level.FINE))
255: getLogger().log(Level.FINE, "PSSS_CSPP0002", dnStr);
256: }
257: } catch (SSOException se) {
258: if (getLogger().isLoggable(Level.SEVERE))
259: getLogger().log(Level.SEVERE, "PSSS_CSPP0004", dnStr);
260: } catch (AMException ame) {
261: if (getLogger().isLoggable(Level.SEVERE))
262: getLogger().log(Level.SEVERE, "PSSS_CSPP0005", dnStr);
263: }
264: }
265:
266: /*
267: * type = type of formating (0-html / 1-text) n = Notification object inObj =
268: * input Object to be formated
269: */
270: private void format(int type, Notification n, String inObj,
271: String prop) {
272: if (inObj != null) {
273: try {
274: // instanciating the formated content
275: if (n.getContent() == null) {
276: n.setContent(new StringBuffer());
277: }
278: switch (type) {
279: case 0:
280: htmlFormat(inObj, (StringBuffer) n.getContent(),
281: prop);
282: break;
283: case 1:
284: default:
285: textFormat(inObj, (StringBuffer) n.getContent(),
286: prop);
287: break;
288: }
289: } catch (Exception e) {
290: if (getLogger().isLoggable(Level.SEVERE))
291: getLogger().log(Level.SEVERE, "PSSS_CSPP0014", e);
292: }
293: }
294: }
295:
296: /*
297: * html formating
298: */
299: private void htmlFormat(String in, StringBuffer out, String prop) {
300: try {
301: //String pattern = this.profilerProps.getProperty(prop,"{0}");
302: String pattern = "{0}";
303: String iA[] = { in };
304: out.append(MessageFormat.format(pattern, iA));
305: } catch (Exception e) {
306: if (getLogger().isLoggable(Level.SEVERE))
307: getLogger().log(Level.SEVERE, "PSSS_CSPP0006",
308: e.getMessage());
309: }
310: }
311:
312: /*
313: * text formating
314: */
315: private void textFormat(String in, StringBuffer out, String prop) {
316: try {
317: } catch (Exception e) {
318: if (getLogger().isLoggable(Level.SEVERE))
319: getLogger().log(Level.SEVERE, "PSSS_CSPP0007",
320: e.getMessage());
321: }
322: }
323:
324: public Logger getLogger() {
325: return logger;
326: }
327:
328: public void setLogger(Logger logger) {
329: this.logger = logger;
330: }
331: }
|