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 8AB61200B76 for ; Tue, 26 Jul 2016 02:41:09 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 898E6160A8F; Tue, 26 Jul 2016 00:41:09 +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 87F18160A96 for ; Tue, 26 Jul 2016 02:41:08 +0200 (CEST) Received: (qmail 69827 invoked by uid 500); 26 Jul 2016 00:41:07 -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 68056 invoked by uid 99); 26 Jul 2016 00:41:06 -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; Tue, 26 Jul 2016 00:41:06 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BE4F3E0100; Tue, 26 Jul 2016 00:41:06 +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: Tue, 26 Jul 2016 00:41:37 -0000 Message-Id: <2e9d3015797449abbd76e18c4459a869@git.apache.org> In-Reply-To: <573ae0b605264b058198e87587ce9bb9@git.apache.org> References: <573ae0b605264b058198e87587ce9bb9@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [32/42] cxf git commit: CXF-5855: Introduce support for Server Sent Events. Initial implementation based on Atmosphere archived-at: Tue, 26 Jul 2016 00:41:09 -0000 http://git-wip-us.apache.org/repos/asf/cxf/blob/e24fb5bc/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/BookStore.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/BookStore.java b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/BookStore.java new file mode 100644 index 0000000..4e0575f --- /dev/null +++ b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/BookStore.java @@ -0,0 +1,69 @@ +/** + * 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.cxf.systest.jaxrs.sse; + +import java.io.IOException; + +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("/api/bookstore") +public class BookStore { + private static OutboundSseEvent createStatsEvent(final OutboundSseEvent.Builder builder, final int eventId) { + return builder + .id(Integer.toString(eventId)) + .data(Book.class, new Book("New Book #" + eventId, eventId)) + .mediaType(MediaType.APPLICATION_JSON_TYPE) + .build(); + } + + @GET + @Path("sse") + @Produces(MediaType.SERVER_SENT_EVENTS) + 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("book"), 1)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("book"), 2)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("book"), 3)); + Thread.sleep(1000); + output.write(createStatsEvent(sseContext.newEvent().name("book"), 4)); + Thread.sleep(1000); + output.close(); + } catch (final InterruptedException | IOException e) { + e.printStackTrace(); + } + } + }.start(); + + return output; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/e24fb5bc/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/AbstractJettyServer.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/AbstractJettyServer.java b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/AbstractJettyServer.java new file mode 100644 index 0000000..c0642e2 --- /dev/null +++ b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/AbstractJettyServer.java @@ -0,0 +1,98 @@ +/** + * 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.cxf.systest.jaxrs.sse.jetty; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; + +import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet; +import org.apache.cxf.systest.jaxrs.sse.BookStore; +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.cxf.transport.sse.SseHttpTransportFactory; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.webapp.WebAppContext; + +public abstract class AbstractJettyServer extends AbstractBusTestServerBase { + + private org.eclipse.jetty.server.Server server; + private final String resourcePath; + private final String contextPath; + private final int port; + + protected AbstractJettyServer(final String contextPath, int portNumber) { + this(null, contextPath, portNumber); + } + + protected AbstractJettyServer(final String resourcePath, final String contextPath, int portNumber) { + this.resourcePath = resourcePath; + this.contextPath = contextPath; + this.port = portNumber; + } + + protected void run() { + server = new Server(port); + + try { + if (resourcePath == null) { + // Register and map the dispatcher servlet + final ServletHolder holder = new ServletHolder(new CXFNonSpringJaxrsServlet()); + holder.setInitParameter(CXFNonSpringJaxrsServlet.TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID); + holder.setInitParameter("jaxrs.serviceClasses", BookStore.class.getName()); + holder.setInitParameter("jaxrs.providers", JacksonJsonProvider.class.getName()); + final ServletContextHandler context = new ServletContextHandler(); + context.setContextPath(contextPath); + context.addServlet(holder, "/rest/*"); + server.setHandler(context); + } else { + final WebAppContext context = new WebAppContext(); + context.setContextPath(contextPath); + context.setWar(getClass().getResource(resourcePath).toURI().getPath()); + + HandlerCollection handlers = new HandlerCollection(); + handlers.setHandlers(new Handler[] {context, new DefaultHandler()}); + server.setHandler(handlers); + } + + configureServer(server); + server.start(); + } catch (final Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } + } + + protected void configureServer(org.eclipse.jetty.server.Server theserver) throws Exception { + + } + + public void tearDown() throws Exception { + super.tearDown(); + + if (server != null) { + server.stop(); + server.destroy(); + server = null; + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/e24fb5bc/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java new file mode 100644 index 0000000..975876a --- /dev/null +++ b/systests/rs-sse/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java @@ -0,0 +1,49 @@ +/** + * 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.cxf.systest.jaxrs.sse.jetty; + +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.systest.jaxrs.sse.AbstractSseTest; +import org.junit.BeforeClass; +import org.junit.Ignore; + +public class JettyEmbeddedTest extends AbstractSseTest { + @Ignore + public static class EmbeddedJettyServer extends AbstractJettyServer { + public static final int PORT = allocatePortAsInt(EmbeddedJettyServer.class); + + public EmbeddedJettyServer() { + super("/", PORT); + } + } + + @BeforeClass + public static void startServers() throws Exception { + AbstractResourceInfo.clearAllMaps(); + //keep out of process due to stack traces testing failures + assertTrue("server did not launch correctly", launchServer(EmbeddedJettyServer.class, true)); + createStaticBus(); + } + + @Override + protected int getPort() { + return EmbeddedJettyServer.PORT; + } +}