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; + } +}