001: package com.sun.portal.sample.j2ee.chat.provider;
002:
003: import java.io.PrintWriter;
004: import java.io.StringWriter;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.Set;
008:
009: import com.iplanet.am.sdk.AMException;
010: import com.iplanet.am.sdk.AMStoreConnection;
011: import com.iplanet.am.sdk.AMUser;
012: import com.iplanet.sso.SSOToken;
013: import com.iplanet.sso.SSOException;
014: import com.iplanet.sso.SSOTokenManager;
015:
016: import com.sun.portal.providers.context.ProviderContext;
017: import com.sun.portal.providers.jsp.JSPProvider;
018: import com.sun.portal.providers.ProviderException;
019:
020: import javax.servlet.http.HttpSession;
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023:
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026:
027: import javax.jms.JMSException;
028: import javax.jms.TextMessage;
029: import javax.jms.Topic;
030: import javax.jms.TopicConnection;
031: import javax.jms.TopicConnectionFactory;
032: import javax.jms.TopicPublisher;
033: import javax.jms.TopicSession;
034: import javax.jms.TopicSubscriber;
035:
036: public class ChatProvider extends JSPProvider {
037:
038: private static final String LOOKUP_STRING_FACTORY = "java:comp/env/jms/sampleTCF";
039: private static final String LOOKUP_STRING_TOPIC = "java:comp/env/jms/sampleTopic";
040: private static TopicConnectionFactory TCFactory = null;
041: private static Topic topic = null;
042:
043: private boolean init() {
044: boolean result = true;
045: InitialContext ic = null;
046:
047: try {
048: ic = new InitialContext();
049: TCFactory = (TopicConnectionFactory) ic
050: .lookup(LOOKUP_STRING_FACTORY);
051: topic = (Topic) ic.lookup(LOOKUP_STRING_TOPIC);
052: } catch (Exception e) {
053: result = false;
054: }
055:
056: return result;
057: }
058:
059: private String getCommonName(HttpServletRequest req) {
060: String result = null;
061:
062: try {
063: SSOTokenManager tokenManager = SSOTokenManager
064: .getInstance();
065:
066: if (tokenManager != null) {
067: SSOToken token = tokenManager.createSSOToken(req);
068: AMStoreConnection conn = new AMStoreConnection(token);
069: AMUser user = conn.getUser(token.getPrincipal()
070: .getName());
071: Set s = user.getAttribute("cn");
072: Iterator i = s.iterator();
073: if (i.hasNext()) {
074: result = (String) i.next();
075: }
076: }
077: } catch (SSOException ssoe) {
078: } catch (AMException ame) {
079: }
080:
081: return result;
082: }
083:
084: public StringBuffer getContent(HttpServletRequest req,
085: HttpServletResponse res) throws ProviderException {
086:
087: String principal = getCommonName(req);
088: TopicSession tSession = null;
089: TopicConnection tConnection = null;
090: TopicPublisher tPublisher = null;
091: TopicSubscriber tSubscriber = null;
092: TextMessage msgReceived = null;
093: StringWriter sw = new StringWriter();
094: PrintWriter pw = new PrintWriter(sw);
095: ProviderContext ctx = getProviderContext();
096:
097: if ((TCFactory == null) || (topic == null)) {
098: if (!init()) {
099: ctx
100: .setSessionProperty(
101: "errMsg",
102: "Failed to initialize TopicConnectionFactory or Topic object. Check JNDI registration.");
103: return super .getContent(req, res);
104: }
105: }
106:
107: try {
108: tConnection = TCFactory.createTopicConnection();
109: tSession = tConnection.createTopicSession(true,
110: TopicSession.CLIENT_ACKNOWLEDGE);
111: tPublisher = tSession.createPublisher(topic);
112: if (principal != null) {
113: tSubscriber = tSession.createDurableSubscriber(topic,
114: principal);
115: } else {
116: tSubscriber = tSession.createSubscriber(topic);
117: }
118:
119: // get any message that needs to be published
120: String mesg = (String) req.getParameter("chat_mesg");
121: if (mesg != null) {
122:
123: TextMessage msgPublised = tSession.createTextMessage();
124: if (principal != null) {
125: msgPublised.setText(principal + ": [" + new Date()
126: + "] : " + mesg);
127: tPublisher
128: .setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
129: } else {
130: msgPublised.setText("[" + new Date() + "] : "
131: + mesg);
132: tPublisher
133: .setDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
134: }
135: // publishing the message to the topic destination
136: tPublisher.publish(msgPublised);
137: tSession.commit();
138: }
139:
140: tConnection.start();
141: while (true) {
142: msgReceived = (TextMessage) tSubscriber.receive(2000);
143: if (msgReceived != null) {
144: pw.println(msgReceived.getText());
145: } else {
146: break;
147: }
148: }
149: pw.flush();
150: sw.flush();
151: ctx.setSessionProperty("msgReceived", sw.toString());
152: } catch (JMSException e) {
153: ctx.setSessionProperty("errMsg",
154: "Failed to publish or recieve msg, error="
155: + e.getLinkedException().toString());
156: } finally {
157: if (tConnection != null) {
158: try {
159: if (tPublisher != null) {
160: tPublisher.close();
161: }
162:
163: if (tSubscriber != null) {
164: tSubscriber.close();
165: }
166:
167: if (tSession != null) {
168: tSession.close();
169: }
170:
171: tConnection.close();
172: } catch (JMSException e) {
173: ctx
174: .setSessionProperty("errMsg",
175: "Failed to cleanup, error="
176: + e.getLinkedException()
177: .toString());
178: }
179: }
180: try {
181: pw.close();
182: sw.close();
183: } catch (java.io.IOException ioe) {
184: }
185: }
186: return super.getContent(req, res);
187: }
188: }
|