diff options
4 files changed, 62 insertions, 111 deletions
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java new file mode 100644 index 0000000000..a597d86f3a --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java @@ -0,0 +1,53 @@ +package org.apache.tuscany.sca.binding.comet.runtime; + +import java.lang.reflect.InvocationTargetException; + +import javax.servlet.ServletContext; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.atmosphere.annotation.Broadcast; +import org.atmosphere.cpr.Broadcaster; +import org.atmosphere.cpr.DefaultBroadcaster; +import org.atmosphere.jersey.Broadcastable; +import org.atmosphere.jersey.SuspendResponse; + +import com.sun.jersey.spi.container.servlet.PerSession; + +@Path("/") +@Produces("text/html;charset=ISO-8859-1") +@PerSession +public class CometBindingHandler { + + private Broadcaster broadcaster; + public static final String ENDPOINT_KEY = "org.apache.tuscany.sca.binding.comet.endpoint"; + public static final String OPERATION_KEY = "org.apache.tuscany.sca.binding.comet.operation"; + + @Context + private ServletContext sc; + + @GET + public SuspendResponse<String> connect() { + broadcaster = new DefaultBroadcaster(); + return new SuspendResponse.SuspendResponseBuilder<String>() + .broadcaster(broadcaster) + .outputComments(true) + // .addListener(new EventsLogger()) + .build(); + } + + @POST + @Broadcast + public Broadcastable callAndRespond() throws InvocationTargetException { + RuntimeEndpoint wire = (RuntimeEndpoint)sc.getAttribute(ENDPOINT_KEY); + Operation operation = (Operation)sc.getAttribute(OPERATION_KEY); + Object response = wire.invoke(operation, new Object[] {}); + return new Broadcastable(response.toString(), "", broadcaster); + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java index 0077504296..effbb8d49d 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -26,13 +26,13 @@ import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.provider.ServiceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; -import org.atmosphere.cpr.MeteorServlet; +import org.atmosphere.cpr.AtmosphereServlet; public class CometServiceBindingProvider implements ServiceBindingProvider { - private static final String SERVLET_KEY = "org.atmosphere.servlet"; - private static final String SERVLET_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.CometServlet"; - + private static final String PACKAGE_KEY = "com.sun.jersey.config.property.packages"; + private static final String PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime"; + private RuntimeEndpoint endpoint; private ServletHost servletHost; @@ -45,12 +45,12 @@ public class CometServiceBindingProvider implements ServiceBindingProvider { ComponentService service = endpoint.getService(); Interface serviceInterface = service.getInterfaceContract().getInterface(); for (Operation op : serviceInterface.getOperations()) { - MeteorServlet servlet = new MeteorServlet(); - servlet.addInitParameter(SERVLET_KEY, SERVLET_VALUE); - String path = endpoint.getBinding().getURI() + "/" + op.getName(); + String path = endpoint.getBinding().getURI() + "/" + op.getName() + "/*"; + AtmosphereServlet servlet = new AtmosphereServlet(); + servlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE); servletHost.addServletMapping(path, servlet); - servlet.getServletContext().setAttribute(CometServlet.ENDPOINT_KEY, endpoint); - servlet.getServletContext().setAttribute(CometServlet.OPERATION_KEY, op); + servlet.getServletContext().setAttribute(CometBindingHandler.ENDPOINT_KEY, endpoint); + servlet.getServletContext().setAttribute(CometBindingHandler.OPERATION_KEY, op); } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java deleted file mode 100644 index d199a77a68..0000000000 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServlet.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 org.apache.tuscany.sca.binding.comet.runtime; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -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.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.runtime.RuntimeEndpoint; -import org.atmosphere.cpr.BroadcastFilter; -import org.atmosphere.cpr.Broadcaster.SCOPE; -import org.atmosphere.cpr.Meteor; -import org.atmosphere.util.XSSHtmlFilter; - -public class CometServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - private static final String METEOR_KEY = "org.apache.tuscany.comet.meteor"; - public static final String ENDPOINT_KEY = "org.apache.tuscany.sca.binding.comet.endpoint"; - public static final String OPERATION_KEY = "org.apache.tuscany.sca.binding.comet.operation"; - - protected transient RuntimeEndpoint wire; - protected transient Operation operation; - private List<BroadcastFilter> filters; - - @Override - public void init(ServletConfig config) throws ServletException { - super.init(config); - filters = new LinkedList<BroadcastFilter>(); - filters.add(new XSSHtmlFilter()); - filters.add(new ScriptFilter()); - } - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - Meteor meteor = Meteor.build(req, SCOPE.REQUEST, filters, null); - // meteor.addListener(new EventsLogger()); - req.getSession().setAttribute(METEOR_KEY, meteor); - resp.setContentType("text/html"); - meteor.suspend(-1); - } - - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - this.wire = (RuntimeEndpoint)getServletContext().getAttribute(ENDPOINT_KEY); - this.operation = (Operation)getServletContext().getAttribute(OPERATION_KEY); - Meteor meteor = (Meteor)req.getSession().getAttribute(METEOR_KEY); - meteor.broadcast(invokeService(new Object[] {})); - } - - protected Object invokeService(Object[] args) { - try { - return wire.invoke(operation, args); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } -} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java deleted file mode 100644 index 8964db68cc..0000000000 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ScriptFilter.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.apache.tuscany.sca.binding.comet.runtime; - -import org.atmosphere.cpr.BroadcastFilter; - -public class ScriptFilter implements BroadcastFilter { - - private static final String BEGIN_SCRIPT_TAG = "<script type='text/javascript'>\n"; - private static final String END_SCRIPT_TAG = "</script>\n"; - // TODO: add dynamic function configuration - private static final String DEFAULT_FUNCTION = "window.parent.update"; - - public BroadcastAction filter(Object o) { - if (o instanceof String) { - String message = (String)o; - return new BroadcastAction(BEGIN_SCRIPT_TAG + DEFAULT_FUNCTION + "('" + message + "');\n" + END_SCRIPT_TAG); - } else { - return new BroadcastAction(o); - } - } -} |