Skip to content

Commit d1953a6

Browse files
committed
Create an example of how cometd is used in GateIn
1 parent f9310b6 commit d1953a6

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed

cometd-hello-world/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) 2003-2010 eXo Platform SAS.
5+
6+
This program is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Affero General Public License
8+
as published by the Free Software Foundation; either version 3
9+
of the License, or (at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program; if not, see <http://www.gnu.org/licenses />.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.exoplatform.portal</groupId>
24+
<artifactId>exo.portal.parent</artifactId>
25+
<version>3.2.0-PLF-SNAPSHOT</version>
26+
</parent>
27+
<groupId>t3.examples</groupId>
28+
<artifactId>cometd-hello-world</artifactId>
29+
<packaging>war</packaging>
30+
<name>GateIn Cometd demonstration</name>
31+
<dependencies>
32+
<dependency>
33+
<!--groupId>org.exoplatform.ws.frameworks.cometd</groupId -->
34+
<groupId>org.exoplatform.commons</groupId>
35+
<artifactId>exo.platform.commons.comet.service</artifactId>
36+
<version>1.1.0-Beta02-SNAPSHOT</version>
37+
<scope>provided</scope>
38+
</dependency>
39+
</dependencies>
40+
<build>
41+
<finalName>cometd-hello-world</finalName>
42+
</build>
43+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2003-2007 eXo Platform SAS.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Affero General Public License
6+
* as published by the Free Software Foundation; either version 3
7+
* of the License, or (at your option) any later version.
8+
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see<http://www.gnu.org/licenses/>.
16+
*/
17+
package t3.examples.cometd;
18+
19+
import org.exoplatform.container.ExoContainer;
20+
import org.exoplatform.container.ExoContainerContext;
21+
import org.exoplatform.ws.frameworks.cometd.ContinuationService;
22+
import org.exoplatform.ws.frameworks.json.impl.JsonException;
23+
import org.exoplatform.ws.frameworks.json.impl.JsonGeneratorImpl;
24+
25+
import java.io.IOException;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import javax.portlet.ActionRequest;
30+
import javax.portlet.ActionResponse;
31+
import javax.portlet.GenericPortlet;
32+
import javax.portlet.PortletContext;
33+
import javax.portlet.PortletException;
34+
import javax.portlet.PortletRequestDispatcher;
35+
import javax.portlet.RenderRequest;
36+
import javax.portlet.RenderResponse;
37+
38+
public class CometdHelloWorldPortlet extends GenericPortlet
39+
{
40+
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException,
41+
IOException
42+
{
43+
44+
String message = actionRequest.getParameter("message");
45+
46+
ContinuationService continuation = getContinuationService();
47+
if (continuation == null)
48+
return;
49+
50+
Map<String, String> msg = new HashMap<String, String>();
51+
msg.put("subcribe", "/eXo/portal/notification");
52+
msg.put("sender", "Cometd Hello World Portlet");
53+
msg.put("message", message);
54+
55+
String userName = actionRequest.getRemoteUser();
56+
System.out.println("DEBUG: Sending message \"" + message + "\"");
57+
JsonGeneratorImpl jsonGenerator = new JsonGeneratorImpl();
58+
try
59+
{
60+
continuation.sendMessage(userName, "/eXo/portal/notification", jsonGenerator.createJsonObjectFromMap(msg), msg.toString());
61+
}
62+
catch (JsonException e)
63+
{
64+
e.printStackTrace();
65+
}
66+
}
67+
68+
protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException,
69+
IOException
70+
{
71+
renderResponse.setContentType("text/html; charset=UTF-8");
72+
73+
ContinuationService continuationService = getContinuationService();
74+
75+
String userName = renderRequest.getRemoteUser();
76+
String userToken = continuationService.getUserToken(userName);
77+
renderRequest.setAttribute("userName", userName);
78+
renderRequest.setAttribute("userToken", userToken);
79+
80+
PortletContext context = getPortletContext();
81+
PortletRequestDispatcher rd = context.getRequestDispatcher("/index.jsp");
82+
rd.include(renderRequest, renderResponse);
83+
}
84+
85+
private ContinuationService getContinuationService()
86+
{
87+
ExoContainer container = ExoContainerContext.getCurrentContainer();
88+
ContinuationService continuation =
89+
(ContinuationService)container.getComponentInstanceOfType(ContinuationService.class);
90+
return continuation;
91+
92+
}
93+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="1.0">
5+
<portlet>
6+
<description xml:lang="en">GateIn Cometd demonstration</description>
7+
<portlet-name>cometd-hello-world</portlet-name>
8+
<display-name xml:lang="en">Cometd Hello World</display-name>
9+
<portlet-class>t3.examples.cometd.CometdHelloWorldPortlet</portlet-class>
10+
<expiration-cache>0</expiration-cache>
11+
<cache-scope>PRIVATE</cache-scope>
12+
<supports>
13+
<mime-type>text/html</mime-type>
14+
<portlet-mode>view</portlet-mode>
15+
</supports>
16+
<supports>
17+
<mime-type>image/jpeg</mime-type>
18+
<portlet-mode>view</portlet-mode>
19+
</supports>
20+
<supported-locale>en</supported-locale>
21+
<portlet-info>
22+
<title>Cometd demonstration portlet</title>
23+
<short-title>Cometd demonstration</short-title>
24+
<keywords>Cometd</keywords>
25+
</portlet-info>
26+
</portlet>
27+
</portlet-app>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="ISO-8859-1"?>
2+
3+
<!DOCTYPE web-app
4+
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
5+
"http://java.sun.com/dtd/web-app_2_3.dtd">
6+
7+
<web-app>
8+
<display-name>cometd-hello-world</display-name>
9+
<description>GateIn Cometd demonstration</description>
10+
11+
<!-- This for portlet registration in eXo Portal -->
12+
<listener>
13+
<listener-class>org.exoplatform.services.portletcontainer.impl.servlet.PortletApplicationListener</listener-class>
14+
</listener>
15+
16+
<servlet>
17+
<servlet-name>PortletWrapper</servlet-name>
18+
<servlet-class>org.exoplatform.services.portletcontainer.impl.servlet.ServletWrapper</servlet-class>
19+
</servlet>
20+
<servlet-mapping>
21+
<servlet-name>PortletWrapper</servlet-name>
22+
<url-pattern>/PortletWrapper</url-pattern>
23+
</servlet-mapping>
24+
<!-- End -->
25+
26+
</web-app>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
2+
<portlet:defineObjects/>
3+
4+
<div>
5+
6+
<p>
7+
Click <a href="#" onclick="init_<portlet:namespace/>();">here</a> to init cometd processing
8+
</p>
9+
10+
<p>
11+
Send message:
12+
<input type="text" id="msg_<portlet:namespace/>" value="Hello World"/> <a href="#" onclick="send_<portlet:namespace/>();">send</a>
13+
14+
</p>
15+
16+
</div>
17+
18+
<script type="text/javascript">
19+
20+
function send_<portlet:namespace/>() {
21+
var msg = document.getElementById("msg_<portlet:namespace/>").value;
22+
23+
var query = "message=" + msg;
24+
var url = "<portlet:actionURL />".replace(/&amp;/g, "&");
25+
var request = new eXo.portal.AjaxRequest('POST', url, query);
26+
request.process();
27+
28+
}
29+
30+
function init_<portlet:namespace/>()
31+
{
32+
if (!eXo.core.Cometd.isConnected()) {
33+
eXo.core.Cometd.url = '/cometd-ksdemo/cometd' ;
34+
eXo.core.Cometd.exoId = '<%=request.getAttribute("userName")%>';
35+
eXo.core.Cometd.exoToken = '<%=request.getAttribute("userToken")%>';
36+
eXo.core.Cometd.addOnConnectionReadyCallback(subcribeNotification_<portlet:namespace/>);
37+
eXo.core.Cometd.init();
38+
} else {
39+
subcribeNotification_<portlet:namespace/>();
40+
}
41+
}
42+
43+
function subcribeNotification_<portlet:namespace/>()
44+
{
45+
eXo.core.Cometd.subscribe('/eXo/portal/notification', function(eventObj) {
46+
var data = eXo.core.JSON.parse(eventObj.data);
47+
eXo.core.Notification.addMessage(data.message);
48+
});
49+
}
50+
51+
</script>

0 commit comments

Comments
 (0)