Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 03EF8200B63 for ; Mon, 1 Aug 2016 04:19:30 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 027E7160AAE; Mon, 1 Aug 2016 02:19:30 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C1DC5160AAF for ; Mon, 1 Aug 2016 04:19:28 +0200 (CEST) Received: (qmail 48085 invoked by uid 500); 1 Aug 2016 02:19:27 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 47872 invoked by uid 99); 1 Aug 2016 02:19:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Aug 2016 02:19:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9785AE2C1A; Mon, 1 Aug 2016 02:19:27 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: reta@apache.org To: commits@cxf.apache.org Date: Mon, 01 Aug 2016 02:19:36 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [10/16] cxf git commit: CXF-5855: Introduce support for Server Sent Events. Initial implementation based on Atmosphere archived-at: Mon, 01 Aug 2016 02:19:30 -0000 http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/README.txt ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/README.txt b/distribution/src/main/release/samples/jax_rs/sse_tomcat/README.txt new file mode 100644 index 0000000..2e4a2e3 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/README.txt @@ -0,0 +1,35 @@ +JAX-RS SSE Demo +================= + +This is a SSE version of JAX-RS Basic Demo. + +A SSE endpoint service is provided on URL http://localhost:8686/rest/api/stats/sse/{id} +where {id} is any integer value, f.e.: + + http://localhost:8686/rest/api/stats/sse/1 + +This sample includes a simple web UI using Highcharts JavaScript library to show off +randomly generated statistics about particular server, pushed to the client using +SSE JAX-RS endpoint. The UI is available at + + http://localhost:8686/static/index.html + +Under the hood, embedded Tomcat 8 container is being used. + +Building and running the demo using maven +--------------------------------------- + +From the base directory of this sample (i.e., where this README file is +located), the maven pom.xml file can be used to build and run the demo. + +Using either UNIX or Windows: + + mvn install + mvn -Pserver + +To remove the target dir, run mvn clean". + +Connecting to the SSE stream +--------------------------------------- + +Open a web browser at: http://localhost:8686/static/index.html http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/pom.xml b/distribution/src/main/release/samples/jax_rs/sse_tomcat/pom.xml new file mode 100644 index 0000000..06d905b --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/pom.xml @@ -0,0 +1,140 @@ + + 4.0.0 + JAX-RS SSE Demo (Tomcat 8) + JAX-RS SSE Demo (Tomcat 8) + jax_rs_sse_tomcat + + + org.apache.cxf.samples + cxf-samples + 3.2.0-SNAPSHOT + ../.. + + + + UTF-8 + 8.0.32 + + + + + org.springframework + spring-context + + + + org.springframework + spring-web + + + + org.apache.cxf + cxf-rt-frontend-jaxrs + 3.2.0-SNAPSHOT + + + + org.apache.cxf + cxf-rt-rs-sse + 3.2.0-SNAPSHOT + + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + + + + com.fasterxml.jackson.core + jackson-core + + + + org.apache.tomcat.embed + tomcat-embed-core + ${cxf.tomcat.version} + + + + org.apache.tomcat.embed + tomcat-embed-logging-juli + ${cxf.tomcat.version} + + + + org.apache.tomcat.embed + tomcat-embed-jasper + ${cxf.tomcat.version} + + + + org.apache.tomcat + tomcat-jasper + ${cxf.tomcat.version} + + + + org.apache.tomcat + tomcat-jasper-el + ${cxf.tomcat.version} + + + + org.apache.tomcat + tomcat-jsp-api + ${cxf.tomcat.version} + + + + org.apache.tomcat.embed + tomcat-embed-websocket + ${cxf.tomcat.version} + + + + org.atmosphere + atmosphere-runtime + 2.4.3 + + + + + + server + + test + + + org.codehaus.mojo + exec-maven-plugin + + + test + + java + + + demo.jaxrs.sse.StatsServer + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/Stats.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/Stats.java b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/Stats.java new file mode 100644 index 0000000..874047c --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/Stats.java @@ -0,0 +1,56 @@ +/** + * 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 demo.jaxrs.sse; + +import java.io.Serializable; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +@JsonInclude(value = Include.NON_NULL) +public class Stats implements Serializable { + private static final long serialVersionUID = -6705829915457870975L; + + private long timestamp; + private int load; + + public Stats() { + } + + public Stats(long timestamp, int load) { + this.timestamp = timestamp; + this.load = load; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public int getLoad() { + return load; + } + + public void setLoad(int load) { + this.load = load; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsRestServiceImpl.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsRestServiceImpl.java b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsRestServiceImpl.java new file mode 100644 index 0000000..b8f8608 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsRestServiceImpl.java @@ -0,0 +1,80 @@ +/** + * 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 demo.jaxrs.sse; + +import java.io.IOException; +import java.util.Date; +import java.util.Random; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.sse.OutboundSseEvent; +import javax.ws.rs.sse.SseContext; +import javax.ws.rs.sse.SseEventOutput; + +@Path("/stats") +public class StatsRestServiceImpl { + private static final Random RANDOM = new Random(); + + private static OutboundSseEvent createStatsEvent(final OutboundSseEvent.Builder builder, final int eventId) { + return builder + .id("" + eventId) + .data(Stats.class, new Stats(new Date().getTime(), RANDOM.nextInt(100))) + .mediaType(MediaType.APPLICATION_JSON_TYPE) + .build(); + } + + @GET + @Path("sse/{id}") + @Produces("text/event-stream") + public SseEventOutput stats(@Context SseContext sseContext, @PathParam("id") final String id) { + final SseEventOutput output = sseContext.newOutput(); + + new Thread() { + public void run() { + try { + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 1)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 2)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 3)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 4)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 5)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 6)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 7)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("stats"), 8)); + output.close(); + } catch (final InterruptedException | IOException e) { + e.printStackTrace(); + } + } + }.start(); + + return output; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsServer.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsServer.java b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsServer.java new file mode 100644 index 0000000..8a8831b --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/java/demo/jaxrs/sse/StatsServer.java @@ -0,0 +1,74 @@ +/** + * 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 demo.jaxrs.sse; + +import java.io.File; + +import org.apache.catalina.Context; +import org.apache.catalina.WebResourceRoot; +import org.apache.catalina.Wrapper; +import org.apache.catalina.core.StandardContext; +import org.apache.catalina.servlets.DefaultServlet; +import org.apache.catalina.startup.Tomcat; +import org.apache.catalina.webresources.DirResourceSet; +import org.apache.catalina.webresources.StandardRoot; +import org.apache.cxf.transport.servlet.CXFServlet; +import org.apache.cxf.transport.sse.SseHttpTransportFactory; +import org.springframework.web.context.ContextLoaderListener; + +public final class StatsServer { + private StatsServer() { + } + + public static void main(final String[] args) throws Exception { + // Register and map the dispatcher servlet + final File base = new File(System.getProperty("java.io.tmpdir")); + + final Tomcat server = new Tomcat(); + server.setPort(8686); + server.setBaseDir(base.getAbsolutePath()); + + final StandardContext context = (StandardContext)server.addWebapp("/", base.getAbsolutePath()); + context.setConfigFile(StatsServer.class.getResource("/META-INF/context.xml")); + context.addApplicationListener(ContextLoaderListener.class.getName()); + context.setAddWebinfClassesResources(true); + context.setResources(resourcesFrom(context, "target/classes")); + + final Wrapper cxfServlet = Tomcat.addServlet(context, "cxfServlet", new CXFServlet()); + cxfServlet.addInitParameter(CXFServlet.TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID); + context.addServletMapping("/rest/*", "cxfServlet"); + + final Context staticContext = server.addWebapp("/static", base.getAbsolutePath()); + Tomcat.addServlet(staticContext, "cxfStaticServlet", new DefaultServlet()); + staticContext.addServletMapping("/static/*", "cxfStaticServlet"); + staticContext.setResources(resourcesFrom(staticContext, "target/classes/web-ui")); + staticContext.setParentClassLoader(Thread.currentThread().getContextClassLoader()); + + server.start(); + server.getServer().await(); + } + + private static WebResourceRoot resourcesFrom(final Context context, final String path) { + final File additionResources = new File(path); + final WebResourceRoot resources = new StandardRoot(context); + resources.addPreResources(new DirResourceSet(resources, "/", additionResources.getAbsolutePath(), "/")); + return resources; + } +} + http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/META-INF/cxf/org.apache.cxf.Logger ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/META-INF/cxf/org.apache.cxf.Logger b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/META-INF/cxf/org.apache.cxf.Logger new file mode 100644 index 0000000..27dd788 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/META-INF/cxf/org.apache.cxf.Logger @@ -0,0 +1 @@ +org.apache.cxf.common.logging.Slf4jLogger \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/WEB-INF/applicationContext.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/WEB-INF/applicationContext.xml b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/WEB-INF/applicationContext.xml new file mode 100644 index 0000000..d4d4584 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/WEB-INF/applicationContext.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/log.properties ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/log.properties b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/log.properties new file mode 100644 index 0000000..63477d3 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/log.properties @@ -0,0 +1,3 @@ +handlers=java.util.logging.ConsoleHandler +org.jboss.weld.level=FINEST +java.util.logging.ConsoleHandler.level=FINEST \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/logback.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/logback.xml b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/logback.xml new file mode 100644 index 0000000..1052186 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + [%level] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} - [%X] %msg%n + + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/60b935d4/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/web-ui/index.html ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/web-ui/index.html b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/web-ui/index.html new file mode 100644 index 0000000..2aaed6e --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/sse_tomcat/src/main/resources/web-ui/index.html @@ -0,0 +1,49 @@ + + + + SSE: Apache CXF example + + + + + +
+
+
+
+ + + + \ No newline at end of file