Repository: cxf Updated Branches: refs/heads/3.1.x-fixes 73f8c955d -> bf6ef93b0
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/bf6ef93b Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/bf6ef93b Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/bf6ef93b Branch: refs/heads/3.1.x-fixes Commit: bf6ef93b02520a4d860049bbc1cf142212cba9b1 Parents: 73f8c95 Author: reta <[email protected]> Authored: Wed Apr 5 08:33:20 2017 -0400 Committer: reta <[email protected]> Committed: Tue Apr 11 18:13:54 2017 -0400 ---------------------------------------------------------------------- .../samples/jax_rs/websocket_web/pom.xml | 1 - .../transport/http/DestinationRegistryImpl.java | 15 +++- .../transport/servlet/AbstractHTTPServlet.java | 4 +- .../transport/servlet/CXFNonSpringServlet.java | 9 +++ .../transport/servlet/ServletConfigAware.java | 30 ++++++++ .../transport/servlet/ServletDestination.java | 11 ++- .../AtmosphereWebSocketServletDestination.java | 78 ++++++++++++++++---- 7 files changed, 129 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/bf6ef93b/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 9cc66f8..91ec9dc 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 @@ -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/bf6ef93b/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 78f0803..cfec4bd 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/bf6ef93b/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 d013bdf..01b95f2 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 @@ -123,8 +123,8 @@ public abstract class AbstractHTTPServlet extends HttpServlet implements Filter public void destroy() { 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/bf6ef93b/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 902221b..9350d36 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 @@ -87,6 +87,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/bf6ef93b/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..fed5507 --- /dev/null +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletConfigAware.java @@ -0,0 +1,30 @@ +/** + * 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; + +/** + * Interface to be implemented by any Destination that wishes to be notified of the + * ServletConfig availability. + */ +public interface ServletConfigAware { + void onServletConfigAvailable(ServletConfig config) throws ServletException; +} http://git-wip-us.apache.org/repos/asf/cxf/blob/bf6ef93b/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 6c9c2bc..7024a1f 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 @@ -23,6 +23,9 @@ import java.io.IOException; import java.net.URI; import java.util.logging.Logger; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + import org.apache.cxf.Bus; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.service.model.EndpointInfo; @@ -30,8 +33,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); /** @@ -77,5 +79,10 @@ public class ServletDestination extends AbstractHTTPDestination { return contextPath + address; } + + @Override + public void onServletConfigAvailable(ServletConfig config) throws ServletException { + // Do nothing + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/bf6ef93b/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 a4e702c..c90b4d2 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 @@ -31,19 +31,20 @@ import javax.servlet.http.HttpServletResponse; import org.apache.cxf.Bus; import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.message.Message; import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.cxf.transport.servlet.ServletDestination; import org.apache.cxf.transport.websocket.WebSocketDestinationService; import org.atmosphere.cpr.ApplicationConfig; import org.atmosphere.cpr.AtmosphereFramework; -import org.atmosphere.cpr.AtmosphereRequest; +import org.atmosphere.cpr.AtmosphereRequestImpl; import org.atmosphere.cpr.AtmosphereResource; -import org.atmosphere.cpr.AtmosphereResponse; +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 +55,54 @@ 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"); - AtmosphereUtils.addInterceptors(framework, bus); - framework.addAtmosphereHandler("/", new DestinationHandler()); + 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"); + 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.init(config); + } + } + + @Override public void invoke(ServletConfig config, ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException { if (AtmosphereUtils.useAtmosphere(req)) { try { - framework.doCometSupport(AtmosphereRequest.wrap(req), - AtmosphereResponse.wrap(resp)); + framework.doCometSupport(AtmosphereRequestImpl.wrap(req), + AtmosphereResponseImpl.wrap(resp)); } catch (ServletException e) { throw new IOException(e); } @@ -85,6 +116,27 @@ public class AtmosphereWebSocketServletDestination extends ServletDestination im HttpServletResponse resp) throws IOException { super.invoke(config, context, req, resp); } + + @Override + protected void setupMessage(Message inMessage, ServletConfig config, ServletContext context, + HttpServletRequest req, HttpServletResponse resp) throws IOException { + + super.setupMessage(inMessage, config, context, req, resp); + + // There are some complications with detecting a full request URL in JSR-356 spec, so + // every WS Container has different interpretation. + // + // https://bz.apache.org/bugzilla/show_bug.cgi?id=56573 + // https://java.net/jira/browse/WEBSOCKET_SPEC-228 + // + // We have do manually inject the transport endpoint address, otherwise the + // JAX-RS resources won't be found. + final Object address = req.getAttribute("org.apache.cxf.transport.endpoint.address"); + if (address == null) { + String basePath = (String)inMessage.get(Message.BASE_PATH); + req.setAttribute("org.apache.cxf.transport.endpoint.address", basePath); + } + } @Override public void shutdown() {
