Repository: cxf Updated Branches: refs/heads/master 50d3f4878 -> 9f82ccd9d
CXF-7276: Atmosphere init code should be moved out of AtmosphereWebSocketServlet Destination Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9f82ccd9 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9f82ccd9 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9f82ccd9 Branch: refs/heads/master Commit: 9f82ccd9d9a6f7db5b555c4040c41faf177f8e0b Parents: 50d3f48 Author: reta <[email protected]> Authored: Wed Apr 5 08:33:20 2017 -0400 Committer: reta <[email protected]> Committed: Wed Apr 5 08:33:20 2017 -0400 ---------------------------------------------------------------------- .../samples/jax_rs/websocket_web/pom.xml | 3 +- .../transport/http/DestinationRegistryImpl.java | 15 +++++- .../transport/servlet/AbstractHTTPServlet.java | 2 +- .../transport/servlet/CXFNonSpringServlet.java | 9 ++++ .../transport/servlet/ServletConfigAware.java | 27 ++++++++++ .../transport/servlet/ServletDestination.java | 2 +- .../AtmosphereWebSocketServletDestination.java | 57 ++++++++++++++++---- 7 files changed, 100 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/distribution/src/main/release/samples/jax_rs/websocket_web/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/websocket_web/pom.xml b/distribution/src/main/release/samples/jax_rs/websocket_web/pom.xml index d9fac05..b279852 100644 --- a/distribution/src/main/release/samples/jax_rs/websocket_web/pom.xml +++ b/distribution/src/main/release/samples/jax_rs/websocket_web/pom.xml @@ -32,7 +32,7 @@ <properties> <cxf.version>${project.version}</cxf.version> <!-- TODO remove these local entries after making the referenced dependency managed in parent/pom.xml --> - <cxf.atmosphere.version>2.4.7</cxf.atmosphere.version> + <cxf.atmosphere.version>2.4.9</cxf.atmosphere.version> <cxf.jetty92.version>9.2.15.v20160210</cxf.jetty92.version> <cxf.jetty93.version>9.3.5.v20151012</cxf.jetty93.version> <cxf.jetty.version>${cxf.jetty93.version}</cxf.jetty.version> @@ -118,6 +118,5 @@ <artifactId>atmosphere-runtime</artifactId> <version>${cxf.atmosphere.version}</version> </dependency> - </dependencies> </project> http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java index 2e75059..3dfc11d 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/DestinationRegistryImpl.java @@ -30,10 +30,14 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + import org.apache.cxf.service.model.InterfaceInfo; import org.apache.cxf.transport.AbstractDestination; +import org.apache.cxf.transport.servlet.ServletConfigAware; -public class DestinationRegistryImpl implements DestinationRegistry { +public class DestinationRegistryImpl implements DestinationRegistry, ServletConfigAware { private static final String SLASH = "/"; private ConcurrentMap<String, AbstractHTTPDestination> destinations = new ConcurrentHashMap<String, AbstractHTTPDestination>(); @@ -170,5 +174,14 @@ public class DestinationRegistryImpl implements DestinationRegistry { } return path; } + + @Override + public void onServletConfigAvailable(ServletConfig config) throws ServletException { + for (final AbstractHTTPDestination destination: getDestinations()) { + if (destination instanceof ServletConfigAware) { + ((ServletConfigAware)destination).onServletConfigAvailable(config); + } + } + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java index 5819686..55d330a 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java @@ -124,7 +124,7 @@ public abstract class AbstractHTTPServlet extends HttpServlet implements Filter FileUtils.maybeDeleteDefaultTempDir(); } - protected void finalizeServletInit(ServletConfig servletConfig) { + protected void finalizeServletInit(ServletConfig servletConfig) throws ServletException { InputStream is = getResourceAsStream("/WEB-INF" + STATIC_RESOURCES_MAP_RESOURCE); if (is == null) { is = getResourceAsStream(STATIC_RESOURCES_MAP_RESOURCE); http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java index eab153a..0d3e254 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/CXFNonSpringServlet.java @@ -90,6 +90,15 @@ public class CXFNonSpringServlet extends AbstractHTTPServlet { this.controller = createServletController(sc); finalizeServletInit(sc); } + + @Override + protected void finalizeServletInit(ServletConfig servletConfig) throws ServletException { + super.finalizeServletInit(servletConfig); + + if (this.destinationRegistry instanceof ServletConfigAware) { + ((ServletConfigAware)this.destinationRegistry).onServletConfigAvailable(servletConfig); + } + } protected void registerServletContextResolver(ServletConfig sc) { if (Boolean.valueOf(sc.getInitParameter(IGNORE_SERVLET_CONTEXT_RESOLVER))) { http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletConfigAware.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletConfigAware.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletConfigAware.java new file mode 100644 index 0000000..07a5f62 --- /dev/null +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletConfigAware.java @@ -0,0 +1,27 @@ +/** + * 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.transport.servlet; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + +public interface ServletConfigAware { + default void onServletConfigAvailable(ServletConfig config) throws ServletException { + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java index 2690ad1..0e87c5d 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletDestination.java @@ -30,7 +30,7 @@ import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; -public class ServletDestination extends AbstractHTTPDestination { +public class ServletDestination extends AbstractHTTPDestination implements ServletConfigAware { static final Logger LOG = LogUtils.getL7dLogger(ServletDestination.class); http://git-wip-us.apache.org/repos/asf/cxf/blob/9f82ccd9/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/atmosphere/AtmosphereWebSocketServletDestination.java ---------------------------------------------------------------------- diff --git a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/atmosphere/AtmosphereWebSocketServletDestination.java b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/atmosphere/AtmosphereWebSocketServletDestination.java index f2e1bf9..e7c7551 100644 --- a/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/atmosphere/AtmosphereWebSocketServletDestination.java +++ b/rt/transports/websocket/src/main/java/org/apache/cxf/transport/websocket/atmosphere/AtmosphereWebSocketServletDestination.java @@ -43,7 +43,7 @@ import org.atmosphere.cpr.AtmosphereResponseImpl; import org.atmosphere.handler.AbstractReflectorAtmosphereHandler; /** - * + * WebSocket Servlet Destination based on Atmosphere */ public class AtmosphereWebSocketServletDestination extends ServletDestination implements WebSocketDestinationService { @@ -54,24 +54,61 @@ public class AtmosphereWebSocketServletDestination extends ServletDestination im public AtmosphereWebSocketServletDestination(Bus bus, DestinationRegistry registry, EndpointInfo ei, String path) throws IOException { super(bus, registry, ei, path); - framework = new AtmosphereFramework(false, true); - framework.setUseNativeImplementation(false); - framework.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true"); - framework.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true"); - framework.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true"); - framework.addInitParameter(ApplicationConfig.WEBSOCKET_PROTOCOL_EXECUTION, "true"); + framework = create(bus); + } + + private AtmosphereFramework create(Bus bus) { + final AtmosphereFramework instance = new AtmosphereFramework(false, true); + + instance.setUseNativeImplementation(false); + instance.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true"); + instance.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true"); + instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true"); + instance.addInitParameter(ApplicationConfig.WEBSOCKET_PROTOCOL_EXECUTION, "true"); // workaround for atmosphere's jsr356 initialization requiring servletConfig - framework.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true"); - AtmosphereUtils.addInterceptors(framework, bus); - framework.addAtmosphereHandler("/", new DestinationHandler()); + instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true"); + AtmosphereUtils.addInterceptors(instance, bus); + instance.addAtmosphereHandler("/", new DestinationHandler()); + + return instance; + } + + @Override + public void finalizeConfig() { framework.init(); } @Override + public void onServletConfigAvailable(ServletConfig config) throws ServletException { + // Very likely there is JSR-356 implementation available, let us reconfigure the Atmosphere framework + // to use it since ServletConfig instance is already available. + final Object container = config.getServletContext() + .getAttribute("javax.websocket.server.ServerContainer"); + + if (container != null) { + if (framework.initialized()) { + framework.destroy(); + } + + framework = create(getBus()); + framework.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "false"); + framework.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "false"); + + framework.init(config); + } + } + + @Override public void invoke(ServletConfig config, ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException { if (AtmosphereUtils.useAtmosphere(req)) { try { +// if (!framework.initialized()) { +// framework.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "false"); +// framework.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "false"); +// framework.init(config); +// } + framework.doCometSupport(AtmosphereRequestImpl.wrap(req), AtmosphereResponseImpl.wrap(resp)); } catch (ServletException e) {
