Repository: cxf Updated Branches: refs/heads/master 043247acc -> f46d64c8d
CXF-7085: Introduce support for Server Sent Events (Client). Refactored SSE client sample to use Undertow and the way Atm response is closed. Added Undertow test suite. Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/f46d64c8 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/f46d64c8 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/f46d64c8 Branch: refs/heads/master Commit: f46d64c8d413ae13cd3baa58ebf691a17e655ba5 Parents: 043247a Author: reta <[email protected]> Authored: Sun Jul 30 17:05:42 2017 -0400 Committer: reta <[email protected]> Committed: Sun Jul 30 17:05:42 2017 -0400 ---------------------------------------------------------------------- .../samples/jax_rs/sse_client/README.txt | 2 +- .../release/samples/jax_rs/sse_client/pom.xml | 17 +-- .../main/java/demo/jaxrs/sse/StatsClient.java | 2 +- .../main/java/demo/jaxrs/sse/StatsServer.java | 69 ++++++----- .../atmosphere/SseAtmosphereEventSinkImpl.java | 29 +++-- systests/rs-sse/pom.xml | 1 + .../cxf/systest/jaxrs/sse/AbstractSseTest.java | 115 ++++++------------- .../jaxrs/sse/jetty/JettyEmbeddedTest.java | 5 - .../systest/jaxrs/sse/jetty/JettyWarTest.java | 5 - systests/rs-sse/rs-sse-undertow/pom.xml | 50 ++++++++ .../sse/undertow/AbstractUndertowServer.java | 96 ++++++++++++++++ .../sse/undertow/UndertowEmbeddedTest.java | 49 ++++++++ 12 files changed, 286 insertions(+), 154 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/distribution/src/main/release/samples/jax_rs/sse_client/README.txt ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_client/README.txt b/distribution/src/main/release/samples/jax_rs/sse_client/README.txt index 6dbd071..ec69be1 100644 --- a/distribution/src/main/release/samples/jax_rs/sse_client/README.txt +++ b/distribution/src/main/release/samples/jax_rs/sse_client/README.txt @@ -7,7 +7,7 @@ A SSE endpoint service is provided on URL http://localhost:8686/rest/stats/sse, http://localhost:8686/rest/stats/sse -Under the hood, embedded Tomcat 8.5 container is being used. The client is a simple +Under the hood, Undertow application container is being used. The client is a simple SSE EventSource consumer which prints each event received in the console. Building and running the demo using maven http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/distribution/src/main/release/samples/jax_rs/sse_client/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_client/pom.xml b/distribution/src/main/release/samples/jax_rs/sse_client/pom.xml index 410f6fe..3151d8f 100644 --- a/distribution/src/main/release/samples/jax_rs/sse_client/pom.xml +++ b/distribution/src/main/release/samples/jax_rs/sse_client/pom.xml @@ -14,7 +14,6 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <cxf.tomcat.version>8.5.16</cxf.tomcat.version> </properties> <dependencies> @@ -47,21 +46,13 @@ </dependency> <dependency> - <groupId>org.apache.tomcat.embed</groupId> - <artifactId>tomcat-embed-core</artifactId> - <version>${cxf.tomcat.version}</version> + <groupId>io.undertow</groupId> + <artifactId>undertow-core</artifactId> </dependency> <dependency> - <groupId>org.apache.tomcat.embed</groupId> - <artifactId>tomcat-embed-jasper</artifactId> - <version>${cxf.tomcat.version}</version> - </dependency> - - <dependency> - <groupId>org.apache.tomcat.embed</groupId> - <artifactId>tomcat-embed-websocket</artifactId> - <version>${cxf.tomcat.version}</version> + <groupId>io.undertow</groupId> + <artifactId>undertow-servlet</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsClient.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsClient.java b/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsClient.java index a028dde..b0c76d8 100644 --- a/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsClient.java +++ b/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsClient.java @@ -36,7 +36,7 @@ public final class StatsClient { .register(JacksonJsonProvider.class) .target("http://localhost:8686/rest/stats/sse"); - try (final SseEventSource eventSource = SseEventSource.target(target).build()) { + try (SseEventSource eventSource = SseEventSource.target(target).build()) { eventSource.register(StatsClient::print, System.out::println); eventSource.open(); // Give the SSE stream 5 seconds to collect all events http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsServer.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsServer.java b/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsServer.java index 140821f..77d847e 100644 --- a/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsServer.java +++ b/distribution/src/main/release/samples/jax_rs/sse_client/src/main/java/demo/jaxrs/sse/StatsServer.java @@ -18,54 +18,53 @@ */ package demo.jaxrs.sse; -import java.io.File; - import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; -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.startup.Tomcat; -import org.apache.catalina.webresources.DirResourceSet; -import org.apache.catalina.webresources.StandardRoot; import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet; import org.apache.cxf.transport.sse.SseHttpTransportFactory; +import io.undertow.Handlers; +import io.undertow.Undertow; +import io.undertow.server.handlers.PathHandler; +import io.undertow.servlet.api.DeploymentInfo; +import io.undertow.servlet.api.DeploymentManager; + +import static io.undertow.servlet.Servlets.defaultContainer; +import static io.undertow.servlet.Servlets.deployment; +import static io.undertow.servlet.Servlets.servlet; + 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 DeploymentInfo servletBuilder = deployment() + .setClassLoader(StatsServer.class.getClassLoader()) + .setContextPath("/") + .setDeploymentName("sse-demo") + .addServlets( + servlet("MessageServlet", CXFNonSpringJaxrsServlet.class) + .addInitParam(CXFNonSpringJaxrsServlet.TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID) + .addInitParam("jaxrs.providers", JacksonJsonProvider.class.getName()) + .addInitParam("jaxrs.serviceClasses", StatsRestServiceImpl.class.getName()) + .setAsyncSupported(true) + .setLoadOnStartup(1) + .addMapping("/rest/*") + ); - final StandardContext context = (StandardContext)server.addWebapp("/", base.getAbsolutePath()); - context.setConfigFile(StatsServer.class.getResource("/META-INF/context.xml")); - context.setAddWebinfClassesResources(true); - context.setResources(resourcesFrom(context, "target/classes")); - - final Wrapper cxfServlet = Tomcat.addServlet(context, "cxfServlet", new CXFNonSpringJaxrsServlet()); - cxfServlet.addInitParameter(CXFNonSpringJaxrsServlet.TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID); - cxfServlet.addInitParameter("jaxrs.providers", JacksonJsonProvider.class.getName()); - cxfServlet.addInitParameter("jaxrs.serviceClasses", StatsRestServiceImpl.class.getName()); - cxfServlet.setAsyncSupported(true); - cxfServlet.setLoadOnStartup(1); - context.addServletMappingDecoded("/rest/*", "cxfServlet"); + final DeploymentManager manager = defaultContainer().addDeployment(servletBuilder); + manager.deploy(); + final PathHandler path = Handlers + .path(Handlers.redirect("/")) + .addPrefixPath("/", manager.start()); + + final Undertow server = Undertow.builder() + .addHttpListener(8686, "localhost") + .setHandler(path) + .build(); + 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/f46d64c8/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/atmosphere/SseAtmosphereEventSinkImpl.java ---------------------------------------------------------------------- diff --git a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/atmosphere/SseAtmosphereEventSinkImpl.java b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/atmosphere/SseAtmosphereEventSinkImpl.java index a79eff7..af984e7 100644 --- a/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/atmosphere/SseAtmosphereEventSinkImpl.java +++ b/rt/rs/sse/src/main/java/org/apache/cxf/jaxrs/sse/atmosphere/SseAtmosphereEventSinkImpl.java @@ -38,18 +38,22 @@ import org.atmosphere.cpr.AtmosphereResource; import org.atmosphere.cpr.AtmosphereResponse; import org.atmosphere.cpr.Broadcaster; +import static org.atmosphere.cpr.ApplicationConfig.PROPERTY_USE_STREAM; + public class SseAtmosphereEventSinkImpl implements SseEventSink { private static final Logger LOG = LogUtils.getL7dLogger(SseAtmosphereEventSinkImpl.class); private final AtmosphereResource resource; private final MessageBodyWriter<OutboundSseEvent> writer; - + private final boolean usingStream; + private volatile boolean closed; public SseAtmosphereEventSinkImpl(final MessageBodyWriter<OutboundSseEvent> writer, final AtmosphereResource resource) { this.writer = writer; this.resource = resource; + this.usingStream = (Boolean)resource.getRequest().getAttribute(PROPERTY_USE_STREAM); if (!resource.isSuspended()) { LOG.fine("Atmosphere resource is not suspended, suspending"); @@ -73,17 +77,20 @@ public class SseAtmosphereEventSinkImpl implements SseEventSink { try { final AtmosphereResponse response = resource.getResponse(); - if (!response.isCommitted()) { - LOG.fine("Response is not committed, flushing buffer"); - try { - response.flushBuffer(); - } catch (IOException ex) { - //REVISIT: and throw a runtime exception ? - LOG.warning("Failed to flush AtmosphereResponse buffer"); - } + + try { + // The resource's request property is null here, we have to + // rely on initial request to understand what to close, response + // or stream. + if (usingStream) { + response.getOutputStream().close(); + } else { + response.getWriter().close(); + } + } catch (final IOException ex) { + LOG.warning("Failed to flush AtmosphereResponse buffer: " + + ex.getMessage()); } - - response.closeStreamOrWriter(); } finally { try { resource.close(); http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/pom.xml ---------------------------------------------------------------------- diff --git a/systests/rs-sse/pom.xml b/systests/rs-sse/pom.xml index c24dd79..dc0ff7c 100644 --- a/systests/rs-sse/pom.xml +++ b/systests/rs-sse/pom.xml @@ -113,5 +113,6 @@ <module>rs-sse-base</module> <module>rs-sse-tomcat</module> <module>rs-sse-jetty</module> + <module>rs-sse-undertow</module> </modules> </project> http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-base/src/main/java/org/apache/cxf/systest/jaxrs/sse/AbstractSseTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-base/src/main/java/org/apache/cxf/systest/jaxrs/sse/AbstractSseTest.java b/systests/rs-sse/rs-sse-base/src/main/java/org/apache/cxf/systest/jaxrs/sse/AbstractSseTest.java index 92c6cbf..0232d45 100644 --- a/systests/rs-sse/rs-sse-base/src/main/java/org/apache/cxf/systest/jaxrs/sse/AbstractSseTest.java +++ b/systests/rs-sse/rs-sse-base/src/main/java/org/apache/cxf/systest/jaxrs/sse/AbstractSseTest.java @@ -39,9 +39,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import org.junit.Test; -import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.hasItems; public abstract class AbstractSseTest extends AbstractSseBaseTest { @@ -59,27 +57,14 @@ public abstract class AbstractSseTest extends AbstractSseBaseTest { } // Easing the test verification here, it does not work well for Atm + Jetty - if (!isStrict()) { - if (!books.isEmpty()) { - assertThat(books, - anyOf( - hasItem(new Book("New Book #151", 151)), - hasItem(new Book("New Book #152", 152)), - hasItem(new Book("New Book #153", 153)), - hasItem(new Book("New Book #154", 154)) - ) - ); - } - } else { - assertThat(books, - hasItems( - new Book("New Book #151", 151), - new Book("New Book #152", 152), - new Book("New Book #153", 153), - new Book("New Book #154", 154) - ) - ); - } + assertThat(books, + hasItems( + new Book("New Book #151", 151), + new Book("New Book #152", 152), + new Book("New Book #153", 153), + new Book("New Book #154", 154) + ) + ); } @Test @@ -94,27 +79,14 @@ public abstract class AbstractSseTest extends AbstractSseBaseTest { awaitEvents(5000, books, 4); } // Easing the test verification here, it does not work well for Atm + Jetty - if (!isStrict()) { - if (!books.isEmpty()) { - assertThat(books, - anyOf( - hasItem(new Book("New Book #1", 1)), - hasItem(new Book("New Book #2", 2)), - hasItem(new Book("New Book #3", 3)), - hasItem(new Book("New Book #4", 4)) - ) - ); - } - } else { - assertThat(books, - hasItems( - new Book("New Book #1", 1), - new Book("New Book #2", 2), - new Book("New Book #3", 3), - new Book("New Book #4", 4) - ) - ); - } + assertThat(books, + hasItems( + new Book("New Book #1", 1), + new Book("New Book #2", 2), + new Book("New Book #3", 3), + new Book("New Book #4", 4) + ) + ); } @Test @@ -130,37 +102,22 @@ public abstract class AbstractSseTest extends AbstractSseBaseTest { awaitEvents(5000, books, 12); } - if (!isStrict()) { - if (!books.isEmpty()) { - assertThat(books, - anyOf( - hasItem(new Book("New Book #1", 1)), - hasItem(new Book("New Book #5", 5)), - hasItem(new Book("New Book #6", 6)), - hasItem(new Book("New Book #10", 10)), - hasItem(new Book("New Book #11", 11)), - hasItem(new Book("New Book #12", 12)) - ) - ); - } - } else { - assertThat(books, - hasItems( - new Book("New Book #1", 1), - new Book("New Book #2", 2), - new Book("New Book #3", 3), - new Book("New Book #4", 4), - new Book("New Book #5", 5), - new Book("New Book #6", 6), - new Book("New Book #7", 7), - new Book("New Book #8", 8), - new Book("New Book #9", 9), - new Book("New Book #10", 10), - new Book("New Book #11", 11), - new Book("New Book #12", 12) - ) - ); - } + assertThat(books, + hasItems( + new Book("New Book #1", 1), + new Book("New Book #2", 2), + new Book("New Book #3", 3), + new Book("New Book #4", 4), + new Book("New Book #5", 5), + new Book("New Book #6", 6), + new Book("New Book #7", 7), + new Book("New Book #8", 8), + new Book("New Book #9", 9), + new Book("New Book #10", 10), + new Book("New Book #11", 11), + new Book("New Book #12", 12) + ) + ); } @Test @@ -204,14 +161,6 @@ public abstract class AbstractSseTest extends AbstractSseBaseTest { r.close(); } - - /** - * Some test cases may fail under Jetty + Atm integration, the real cause(s) is - * unknown yet. To make them pass, we easy the verification a bit. - */ - protected boolean isStrict() { - return true; - } private static Consumer<InboundSseEvent> collect(final Collection< Book > books) { return event -> books.add(event.readData(Book.class, MediaType.APPLICATION_JSON_TYPE)); http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java b/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java index 9c125eb..cafdeec 100644 --- a/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java +++ b/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyEmbeddedTest.java @@ -46,9 +46,4 @@ public class JettyEmbeddedTest extends AbstractSseTest { protected int getPort() { return EmbeddedJettyServer.PORT; } - - @Override - protected boolean isStrict() { - return false; - } } http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyWarTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyWarTest.java b/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyWarTest.java index 2aaf397..10f435b 100644 --- a/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyWarTest.java +++ b/systests/rs-sse/rs-sse-jetty/src/test/java/org/apache/cxf/systest/jaxrs/sse/jetty/JettyWarTest.java @@ -45,9 +45,4 @@ public class JettyWarTest extends AbstractSseTest { protected int getPort() { return EmbeddedJettyServer.PORT; } - - @Override - protected boolean isStrict() { - return false; - } } http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-undertow/pom.xml ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-undertow/pom.xml b/systests/rs-sse/rs-sse-undertow/pom.xml new file mode 100644 index 0000000..be5283b --- /dev/null +++ b/systests/rs-sse/rs-sse-undertow/pom.xml @@ -0,0 +1,50 @@ +<?xml version="1.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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <groupId>org.apache.cxf.systests</groupId> + <artifactId>cxf-systests-rs-sse</artifactId> + <version>3.2.0-SNAPSHOT</version> + <relativePath>../</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>cxf-systests-rs-sse-undertow</artifactId> + <name>Apache CXF SSE Integration System Tests for Undertow</name> + <description>Apache CXF SSE Integration System Tests Undertow</description> + <url>http://cxf.apache.org</url> + <dependencies> + <dependency> + <groupId>io.undertow</groupId> + <artifactId>undertow-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.undertow</groupId> + <artifactId>undertow-servlet</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf.systests</groupId> + <artifactId>cxf-systests-rs-sse-base</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/AbstractUndertowServer.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/AbstractUndertowServer.java b/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/AbstractUndertowServer.java new file mode 100644 index 0000000..c6f0a8d --- /dev/null +++ b/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/AbstractUndertowServer.java @@ -0,0 +1,96 @@ +/** + * 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.undertow; + +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 io.undertow.Handlers; +import io.undertow.Undertow; +import io.undertow.server.handlers.PathHandler; +import io.undertow.servlet.api.DeploymentInfo; +import io.undertow.servlet.api.DeploymentManager; + +import static io.undertow.servlet.Servlets.defaultContainer; +import static io.undertow.servlet.Servlets.deployment; +import static io.undertow.servlet.Servlets.servlet; + +public abstract class AbstractUndertowServer extends AbstractBusTestServerBase { + private Undertow server; + private final String contextPath; + private final int port; + + protected AbstractUndertowServer(final String contextPath, int portNumber) { + this.contextPath = contextPath; + this.port = portNumber; + } + + protected void run() { + try { + final DeploymentInfo servletBuilder = deployment() + .setClassLoader(AbstractUndertowServer.class.getClassLoader()) + .setContextPath(contextPath) + .setDeploymentName("sse-test") + .addServlets( + servlet("MessageServlet", CXFNonSpringJaxrsServlet.class) + .addInitParam(CXFNonSpringJaxrsServlet.TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID) + .addInitParam("jaxrs.providers", JacksonJsonProvider.class.getName()) + .addInitParam("jaxrs.serviceClasses", BookStore.class.getName()) + .setAsyncSupported(true) + .setLoadOnStartup(1) + .addMapping("/rest/*") + ); + + final DeploymentManager manager = defaultContainer().addDeployment(servletBuilder); + manager.deploy(); + + PathHandler path = Handlers + .path(Handlers.redirect("/")) + .addPrefixPath("/", manager.start()); + + server = Undertow.builder() + .addHttpListener(port, "localhost") + .setHandler(path) + .build(); + + 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 = null; + } + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/f46d64c8/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/UndertowEmbeddedTest.java ---------------------------------------------------------------------- diff --git a/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/UndertowEmbeddedTest.java b/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/UndertowEmbeddedTest.java new file mode 100644 index 0000000..46bb2da --- /dev/null +++ b/systests/rs-sse/rs-sse-undertow/src/test/java/org/apache/cxf/systest/jaxrs/sse/undertow/UndertowEmbeddedTest.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.undertow; + +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 UndertowEmbeddedTest extends AbstractSseTest { + @Ignore + public static class EmbeddedTomcatServer extends AbstractUndertowServer { + public static final int PORT = allocatePortAsInt(EmbeddedTomcatServer.class); + + public EmbeddedTomcatServer() { + super("/", PORT); + } + } + + @BeforeClass + public static void startServers() throws Exception { + AbstractResourceInfo.clearAllMaps(); + assertTrue("server did not launch correctly", launchServer(EmbeddedTomcatServer.class, true)); + createStaticBus(); + } + + @Override + protected int getPort() { + return EmbeddedTomcatServer.PORT; + } + +}
