Source Code Cross Referenced for MessagingUtil.java in  » Portal » liferay-portal-4.4.2 » com » liferay » portlet » messaging » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » liferay portal 4.4.2 » com.liferay.portlet.messaging.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003:         *
004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
005:         * of this software and associated documentation files (the "Software"), to deal
006:         * in the Software without restriction, including without limitation the rights
007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008:         * copies of the Software, and to permit persons to whom the Software is
009:         * furnished to do so, subject to the following conditions:
010:         *
011:         * The above copyright notice and this permission notice shall be included in
012:         * all copies or substantial portions of the Software.
013:         *
014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020:         * SOFTWARE.
021:         */package com.liferay.portlet.messaging.util;
022:
023:        import com.liferay.portal.PortalException;
024:        import com.liferay.portal.SystemException;
025:        import com.liferay.portal.kernel.util.GetterUtil;
026:        import com.liferay.portal.kernel.util.StringPool;
027:        import com.liferay.portal.kernel.util.StringUtil;
028:        import com.liferay.portal.model.User;
029:        import com.liferay.portal.util.PropsUtil;
030:        import com.liferay.portal.util.PropsValues;
031:        import com.liferay.portal.util.WebKeys;
032:        import com.liferay.portlet.chat.model.RosterUpdateListener;
033:        import com.liferay.portlet.messaging.model.JabberSession;
034:        import com.liferay.portlet.messaging.model.MessageListener;
035:        import com.liferay.portlet.messaging.util.comparator.NameComparator;
036:
037:        import java.util.ArrayList;
038:        import java.util.Collections;
039:        import java.util.Iterator;
040:        import java.util.List;
041:
042:        import javax.servlet.http.HttpSession;
043:
044:        import org.jivesoftware.smack.AccountManager;
045:        import org.jivesoftware.smack.Chat;
046:        import org.jivesoftware.smack.PacketCollector;
047:        import org.jivesoftware.smack.Roster;
048:        import org.jivesoftware.smack.RosterEntry;
049:        import org.jivesoftware.smack.XMPPConnection;
050:        import org.jivesoftware.smack.XMPPException;
051:        import org.jivesoftware.smack.filter.PacketFilter;
052:        import org.jivesoftware.smack.filter.PacketTypeFilter;
053:        import org.jivesoftware.smack.packet.Message;
054:        import org.jivesoftware.smack.packet.Presence;
055:        import org.jivesoftware.smack.packet.RosterPacket;
056:
057:        import org.json.JSONArray;
058:        import org.json.JSONObject;
059:
060:        /**
061:         * <a href="MessagingUtil.java.html"><b><i>View Source</i></b></a>
062:         *
063:         * @author Ming-Gih Lam
064:         *
065:         */
066:        public class MessagingUtil {
067:
068:            public static String SERVER_ADDRESS = GetterUtil.getString(
069:                    PropsUtil.get(PropsUtil.JABBER_XMPP_SERVER_ADDRESS),
070:                    "localhost");
071:
072:            public static int SERVER_PORT = GetterUtil.getInteger(PropsUtil
073:                    .get(PropsUtil.JABBER_XMPP_SERVER_PORT), 5222);
074:
075:            public static String USER_PASSWORD = GetterUtil.getString(PropsUtil
076:                    .get(PropsUtil.JABBER_XMPP_USER_PASSWORD), "liferayllc");
077:
078:            public static JSONObject addRosterEntry(HttpSession ses, User user)
079:                    throws PortalException, SystemException, XMPPException {
080:
081:                JSONObject jo = new JSONObject();
082:
083:                Roster roster = getRoster(ses);
084:
085:                String smackId = getXmppId(user);
086:                String name = user.getFullName();
087:
088:                try {
089:                    XMPPConnection con = new XMPPConnection(SERVER_ADDRESS,
090:                            SERVER_PORT);
091:
092:                    AccountManager accountManager = con.getAccountManager();
093:
094:                    try {
095:                        accountManager.createAccount(String.valueOf(user
096:                                .getUserId()), USER_PASSWORD);
097:                    } catch (XMPPException xmppe) {
098:                    }
099:
100:                    con.close();
101:                } catch (XMPPException xmppe) {
102:                }
103:
104:                roster.createEntry(smackId, name, null);
105:
106:                jo.put("name", name);
107:                jo.put("user", user.getUserId());
108:                jo.put("status", "success");
109:
110:                return jo;
111:            }
112:
113:            public static void closeXMPPConnection(HttpSession ses) {
114:                if (isJabberEnabled()) {
115:                    JabberSession jabberSes = (JabberSession) ses
116:                            .getAttribute(WebKeys.JABBER_XMPP_SESSION);
117:
118:                    if (jabberSes != null) {
119:                        Roster roster = jabberSes.getRoster();
120:
121:                        roster.removeRosterListener(jabberSes
122:                                .getRosterListener());
123:
124:                        XMPPConnection con = jabberSes.getConnection();
125:
126:                        con
127:                                .removePacketListener(jabberSes
128:                                        .getMessageListener());
129:
130:                        con.close();
131:
132:                        ses.removeAttribute(WebKeys.JABBER_XMPP_SESSION);
133:                    }
134:                }
135:            }
136:
137:            public static void createXMPPConnection(HttpSession ses, long userId)
138:                    throws XMPPException {
139:
140:                createXMPPConnection(ses, String.valueOf(userId));
141:            }
142:
143:            public static void createXMPPConnection(HttpSession ses,
144:                    String userId) throws XMPPException {
145:
146:                if (isJabberEnabled()) {
147:                    XMPPConnection con = null;
148:
149:                    try {
150:                        con = new XMPPConnection(SERVER_ADDRESS, SERVER_PORT);
151:                    } catch (XMPPException e) {
152:                        return;
153:                    }
154:
155:                    try {
156:                        con.login(userId, USER_PASSWORD, ses.getId());
157:                    } catch (XMPPException xmppe) {
158:                        AccountManager accountManager = con.getAccountManager();
159:
160:                        accountManager.createAccount(userId, USER_PASSWORD);
161:
162:                        con.close();
163:
164:                        con = new XMPPConnection(SERVER_ADDRESS, SERVER_PORT);
165:
166:                        con.login(userId, USER_PASSWORD, ses.getId());
167:                    }
168:
169:                    PacketFilter filter = new PacketTypeFilter(Message.class);
170:
171:                    PacketCollector collector = con
172:                            .createPacketCollector(filter);
173:
174:                    MessageListener msgListener = new MessageListener(ses);
175:
176:                    con.addPacketListener(msgListener, filter);
177:
178:                    Roster roster = con.getRoster();
179:
180:                    //roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);
181:
182:                    RosterUpdateListener rosterListener = new RosterUpdateListener(
183:                            ses);
184:
185:                    roster.addRosterListener(rosterListener);
186:
187:                    JabberSession jabberSes = new JabberSession();
188:
189:                    jabberSes.setConnection(con);
190:                    jabberSes.setCollector(collector);
191:                    jabberSes.setMessageListener(msgListener);
192:                    jabberSes.setRoster(roster);
193:                    jabberSes.setRosterListener(rosterListener);
194:
195:                    ses.setAttribute(WebKeys.JABBER_XMPP_SESSION, jabberSes);
196:                }
197:            }
198:
199:            public static void deleteRosterEntries(HttpSession ses,
200:                    String[] userId) throws XMPPException {
201:
202:                Roster roster = getRoster(ses);
203:
204:                for (int i = 0; i < userId.length; i++) {
205:                    RosterEntry entry = roster.getEntry(getXmppId(userId[i]));
206:
207:                    roster.removeEntry(entry);
208:                }
209:            }
210:
211:            public static JSONObject getChatMessages(HttpSession ses) {
212:                JSONArray ja = new JSONArray();
213:                JSONObject jo = new JSONObject();
214:                PacketCollector collector = getCollector(ses);
215:                Message message = getNextMessage(collector);
216:                Roster roster = getRoster(ses);
217:
218:                while (message != null) {
219:                    JSONObject jMsg = new JSONObject();
220:                    String fromId = (String) message.getProperty("fromId");
221:
222:                    jMsg.put("body", message.getBody());
223:                    jMsg.put("category", message.getProperty("category"));
224:                    jMsg.put("toId", message.getProperty("toId"));
225:                    jMsg.put("toName", message.getProperty("toName"));
226:                    jMsg.put("fromId", fromId);
227:                    jMsg.put("fromName", message.getProperty("fromName"));
228:                    jMsg.put("status", getPresence(roster
229:                            .getPresence(getXmppId(fromId))));
230:
231:                    ja.put(jMsg);
232:
233:                    message = getNextMessage(collector);
234:                }
235:
236:                jo.put("chat", ja);
237:                jo.put("status", "success");
238:
239:                return jo;
240:            }
241:
242:            public static PacketCollector getCollector(HttpSession ses) {
243:                JabberSession jabberSes = (JabberSession) ses
244:                        .getAttribute(WebKeys.JABBER_XMPP_SESSION);
245:
246:                return jabberSes.getCollector();
247:            }
248:
249:            public static XMPPConnection getConnection(HttpSession ses) {
250:                JabberSession jabberSes = (JabberSession) ses
251:                        .getAttribute(WebKeys.JABBER_XMPP_SESSION);
252:
253:                return jabberSes.getConnection();
254:            }
255:
256:            public static Message getNextMessage(PacketCollector collector) {
257:                Message message = (Message) collector.pollResult();
258:
259:                return message;
260:            }
261:
262:            public static String getPresence(Presence presence) {
263:                String status = "unavailable";
264:
265:                if (presence != null) {
266:                    status = presence.getType().toString();
267:                }
268:
269:                return status;
270:            }
271:
272:            public static Roster getRoster(HttpSession ses) {
273:                JabberSession jabberSes = (JabberSession) ses
274:                        .getAttribute(WebKeys.JABBER_XMPP_SESSION);
275:
276:                return jabberSes.getRoster();
277:            }
278:
279:            public static JSONObject getRosterEntries(HttpSession ses) {
280:                JSONObject jo = new JSONObject();
281:                Roster roster = getRoster(ses);
282:                List rosterList = new ArrayList();
283:
284:                Iterator rosterEntries = roster.getEntries();
285:                JSONArray ja = new JSONArray();
286:
287:                while (rosterEntries.hasNext()) {
288:                    RosterEntry entry = (RosterEntry) rosterEntries.next();
289:
290:                    if (entry.getType() != RosterPacket.ItemType.FROM) {
291:                        rosterList.add(entry);
292:                    }
293:                }
294:
295:                if (rosterList.size() > 0) {
296:                    Collections.sort(rosterList, new NameComparator());
297:
298:                    for (int i = 0; i < rosterList.size(); i++) {
299:
300:                        JSONObject jEntry = new JSONObject();
301:                        RosterEntry entry = (RosterEntry) rosterList.get(i);
302:
303:                        jEntry.put("user", getUserId(entry));
304:                        jEntry.put("name", entry.getName());
305:                        jEntry.put("status", getPresence(roster
306:                                .getPresence(entry.getUser())));
307:                        ja.put(jEntry);
308:                    }
309:                }
310:
311:                jo.put("roster", ja);
312:
313:                return jo;
314:            }
315:
316:            public static String getUserId(RosterEntry entry) {
317:                String userId = entry.getUser();
318:
319:                String serverName = GetterUtil.getString(PropsUtil
320:                        .get(PropsUtil.JABBER_XMPP_SERVER_NAME), "localhost");
321:
322:                userId = StringUtil.replace(userId, StringPool.AT + serverName,
323:                        StringPool.BLANK);
324:
325:                return userId;
326:            }
327:
328:            public static String getXmppId(String userId) {
329:                String serverName = GetterUtil.getString(PropsUtil
330:                        .get(PropsUtil.JABBER_XMPP_SERVER_NAME), "localhost");
331:
332:                String xmppId = userId + StringPool.AT + serverName;
333:
334:                return xmppId;
335:            }
336:
337:            public static String getXmppId(User user) {
338:                String xmppId = getXmppId(String.valueOf(user.getUserId()));
339:
340:                return xmppId;
341:            }
342:
343:            public static boolean isJabberEnabled() {
344:                return PropsValues.JABBER_XMPP_SERVER_ENABLED;
345:            }
346:
347:            public static void sendMessage(HttpSession ses, String fromId,
348:                    String fromName, String toId, String toName, String bodyText)
349:                    throws XMPPException {
350:
351:                XMPPConnection con = getConnection(ses);
352:
353:                Chat chat = con.createChat(getXmppId(toId));
354:
355:                Message message = chat.createMessage();
356:
357:                message.setBody(bodyText);
358:                message.setProperty("category", "private");
359:                message.setProperty("fromId", fromId);
360:                message.setProperty("fromName", fromName);
361:                message.setProperty("toId", toId);
362:                message.setProperty("toName", toName);
363:
364:                chat.sendMessage(message);
365:            }
366:
367:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.