summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java')
-rw-r--r--sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java116
1 files changed, 116 insertions, 0 deletions
diff --git a/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java
new file mode 100644
index 0000000000..c93dc144dc
--- /dev/null
+++ b/sca-java-2.x/contrib/samples/stock-comet/src/main/java/sample/CometServlet.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package sample;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.atmosphere.cpr.AtmosphereResourceEvent;
+import org.atmosphere.cpr.AtmosphereResourceEventListener;
+import org.atmosphere.cpr.BroadcastFilter;
+import org.atmosphere.cpr.Broadcaster;
+import org.atmosphere.cpr.Meteor;
+import org.atmosphere.util.XSSHtmlFilter;
+import org.oasisopen.sca.ComponentContext;
+
+@SuppressWarnings("serial")
+public class CometServlet extends HttpServlet {
+
+ private static final String COMPONENT_CONTEXT_KEY = "org.oasisopen.sca.ComponentContext";
+ private static final String COMET_SCOPE_KEY = "org.apache.tuscany.comet.scope";
+ private static final String METEOR_KEY = "org.apache.tuscany.comet.meteor";
+
+ // TODO: check if static variables are a good choice here
+ private static List<BroadcastFilter> filters;
+ private static Broadcaster.SCOPE scope;
+
+
+ @Override
+ public void init(ServletConfig config) throws ServletException {
+ super.init(config);
+ filters = new LinkedList<BroadcastFilter>();
+ filters.add(new XSSHtmlFilter());
+ filters.add(new ScriptFilter());
+ String cometScope = getInitParameter(COMET_SCOPE_KEY).trim().toUpperCase();
+ scope = Broadcaster.SCOPE.valueOf(cometScope);
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ Meteor meteor = Meteor.build(request, scope, filters, null);
+ meteor.addListener(new CometEventListener());
+ request.getSession().setAttribute(METEOR_KEY, meteor);
+ response.setContentType("text/html");
+ meteor.suspend(-1); // http streaming
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
+ IOException {
+ Meteor meteor = (Meteor)request.getSession().getAttribute(METEOR_KEY);
+
+ // TODO: add dynamic call and cast
+ // String component = request.getParameter("component");
+ // String method = request.getParameter("method");
+ // String params = request.getParameter("params");
+ // String componentClass = request.getParameter("component.class");
+
+ // TODO: check if cc has all the information when servlet is not in
+ // web.composite
+ ComponentContext cc = (ComponentContext)getServletContext().getAttribute(COMPONENT_CONTEXT_KEY);
+ StockService service = cc.getService(StockService.class, "service");
+
+ // TODO: add JSON serialization
+ meteor.broadcast(service.getSymbol() + "#" + service.getValue());
+ }
+
+ // ----------------------------------------------
+
+ public class CometEventListener implements AtmosphereResourceEventListener {
+
+ @Override
+ public void onSuspend(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
+ System.out.println("onSuspend: " + event);
+ }
+
+ @Override
+ public void onResume(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
+ System.out.println("onResume: " + event);
+ }
+
+ @Override
+ public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
+ System.out.println("onDisconnect: " + event);
+ }
+
+ @Override
+ public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
+ System.out.println("onBroadcast: " + event);
+ }
+
+ }
+
+}