You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2023/05/25 14:45:17 UTC
[cxf] branch 3.6.x-fixes updated: Use a little bit of reflection to allow services to still run with jetty9 (in Karaf for example) as well as jetty 10.
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
new 2ff1093580 Use a little bit of reflection to allow services to still run with jetty9 (in Karaf for example) as well as jetty 10.
2ff1093580 is described below
commit 2ff109358027653d1afd6f252cf6f25048cafbb9
Author: Daniel Kulp <da...@kulp.com>
AuthorDate: Thu May 25 10:44:33 2023 -0400
Use a little bit of reflection to allow services to still run with jetty9 (in Karaf for example) as well as jetty 10.
---
parent/pom.xml | 1 +
.../http_jetty/JettyHTTPServerEngine.java | 61 +++++++++++++---------
.../NoUpgradeHTTP2CServerConnectionFactory.java | 44 ++++++++++++++++
.../http_jetty/JettyHTTPDestinationTest.java | 20 +++----
4 files changed, 91 insertions(+), 35 deletions(-)
diff --git a/parent/pom.xml b/parent/pom.xml
index fd7a5af5cc..61f41e9624 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -153,6 +153,7 @@
<cxf.jettison.version>1.5.4</cxf.jettison.version>
<cxf.jetty.osgi.version>[9.2,11)</cxf.jetty.osgi.version>
<cxf.jetty10.version>10.0.15</cxf.jetty10.version>
+ <cxf.jetty9.version>9.4.51.v20230217</cxf.jetty9.version>
<cxf.jetty.version>${cxf.jetty10.version}</cxf.jetty.version>
<cxf.jexl.version>3.2.1</cxf.jexl.version>
<cxf.joda.time.version>2.10.10</cxf.joda.time.version>
diff --git a/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
index 2cfe49fdba..658f155e8a 100644
--- a/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
+++ b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
@@ -23,6 +23,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
@@ -56,13 +57,9 @@ import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.transport.HttpUriMapper;
import org.apache.cxf.transport.http.HttpServerEngineSupport;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
-import org.eclipse.jetty.http.BadMessageException;
-import org.eclipse.jetty.http.HttpFields.Mutable;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
-import org.eclipse.jetty.io.Connection;
-import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.ConnectionFactory;
@@ -608,7 +605,13 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp
try {
Container container = getContainer(server);
- container.addEventListener(mBeanContainer);
+ Method[] methods = ReflectionUtil.getDeclaredMethods(container.getClass());
+ for (Method m : methods) {
+ if ("addEventListener".equals(m.getName())) {
+ ReflectionUtil.setAccessible(m).invoke(container, mBeanContainer);
+ }
+ }
+ //container.addEventListener(mBeanContainer);
mBeanContainer.beanAdded(null, server);
} catch (RuntimeException rex) {
throw rex;
@@ -693,9 +696,18 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp
// additional dependency.
final ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol(httpFactory.getProtocol());
+
+ Constructor<SslConnectionFactory>[] cons
+ = ReflectionUtil.getDeclaredConstructors(SslConnectionFactory.class);
+ for (Constructor<SslConnectionFactory> c : cons) {
+ if (c.getParameterCount() == 2
+ && c.getParameterTypes()[1] == String.class
+ && c.getParameterTypes()[0].isInstance(sslcf)) {
+ SslConnectionFactory scf = c.newInstance(sslcf, alpn.getProtocol());
+ connectionFactories.add(scf);
+ }
+ }
- final SslConnectionFactory scf = new SslConnectionFactory(sslcf, alpn.getProtocol());
- connectionFactories.add(scf);
connectionFactories.add(alpn);
connectionFactories.add(new HTTP2ServerConnectionFactory(httpConfig));
} catch (Throwable ex) {
@@ -705,8 +717,16 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp
}
}
if (connectionFactories.size() == 1) {
- final SslConnectionFactory scf = new SslConnectionFactory(sslcf, httpFactory.getProtocol());
- connectionFactories.add(scf);
+ Constructor<SslConnectionFactory>[] cons
+ = ReflectionUtil.getDeclaredConstructors(SslConnectionFactory.class);
+ for (Constructor<SslConnectionFactory> c : cons) {
+ if (c.getParameterCount() == 2
+ && c.getParameterTypes()[1] == String.class
+ && c.getParameterTypes()[0].isInstance(sslcf)) {
+ SslConnectionFactory scf = c.newInstance(sslcf, httpFactory.getProtocol());
+ connectionFactories.add(scf);
+ }
+ }
}
// Has to be set before the default protocol change
@@ -717,22 +737,13 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp
} else if (isHttp2Enabled(bus)) {
connectionFactories.add(httpFactory);
try {
- connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfig) {
-
- @Override
- public Connection upgradeConnection(Connector c, EndPoint endPoint,
- org.eclipse.jetty.http.MetaData.Request request,
- Mutable response101)
- throws BadMessageException {
- if (request.getContentLength() > 0
- || request.getFields().contains("Transfer-Encoding")) {
- // if there is a body, we cannot upgrade
- return null;
- }
- return super.upgradeConnection(c, endPoint, request, response101);
- }
- });
+ connectionFactories.add(new NoUpgradeHTTP2CServerConnectionFactory(httpConfig));
} catch (Throwable ex) {
+ try {
+ connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfig));
+ } catch (Throwable e2) {
+ //ignore
+ }
if (isHttp2Required(bus)) {
throw ex;
}
@@ -1104,7 +1115,7 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp
}
//After upgrade Jetty to 10.0.12, server.destroy() will clear all MBeans from container
//The old version doesn't behavior like this and this
- //server.destroy();
+ server.destroy();
server = null;
}
}
diff --git a/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/NoUpgradeHTTP2CServerConnectionFactory.java b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/NoUpgradeHTTP2CServerConnectionFactory.java
new file mode 100644
index 0000000000..b7f1b0e373
--- /dev/null
+++ b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/NoUpgradeHTTP2CServerConnectionFactory.java
@@ -0,0 +1,44 @@
+/**
+ * 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.http_jetty;
+
+import org.eclipse.jetty.http.BadMessageException;
+import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
+import org.eclipse.jetty.io.Connection;
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.HttpConfiguration;
+
+class NoUpgradeHTTP2CServerConnectionFactory extends HTTP2CServerConnectionFactory {
+ NoUpgradeHTTP2CServerConnectionFactory(HttpConfiguration httpConfiguration) {
+ super(httpConfiguration);
+ }
+ @Override
+ public Connection upgradeConnection(Connector c, EndPoint endPoint,
+ org.eclipse.jetty.http.MetaData.Request request,
+ org.eclipse.jetty.http.HttpFields.Mutable response101)
+ throws BadMessageException {
+ if (request.getContentLength() > 0
+ || request.getFields().contains("Transfer-Encoding")) {
+ // if there is a body, we cannot upgrade
+ return null;
+ }
+ return super.upgradeConnection(c, endPoint, request, response101);
+ }
+}
\ No newline at end of file
diff --git a/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
index 670b656d94..dab7139483 100644
--- a/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
+++ b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
@@ -24,6 +24,7 @@ import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -70,7 +71,6 @@ import org.apache.cxf.ws.addressing.AddressingProperties;
import org.apache.cxf.ws.addressing.EndpointReferenceType;
import org.apache.cxf.ws.addressing.EndpointReferenceUtils;
import org.apache.cxf.ws.addressing.JAXWSAConstants;
-import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
@@ -676,17 +676,17 @@ public class JettyHTTPDestinationTest {
EasyMock.expect(request.getAttribute("org.eclipse.jetty.ajax.Continuation")).andReturn(null);
EasyMock.expect(request.getAttribute("http.service.redirection")).andReturn(null).anyTimes();
- HttpFields.Mutable httpFields = HttpFields.build();
- httpFields.add("content-type", "text/xml");
- httpFields.add("content-type", "charset=utf8");
- httpFields.put(JettyHTTPDestinationTest.AUTH_HEADER, JettyHTTPDestinationTest.BASIC_AUTH);
-
- EasyMock.expect(request.getHeaderNames()).andReturn(httpFields.getFieldNames());
+ List<String> headers = Arrays.asList(new String[] {"content-type",
+ JettyHTTPDestinationTest.AUTH_HEADER});
+ List<String> ct = Arrays.asList(new String[] {"text/xml", "charset=utf8"});
+ EasyMock.expect(request.getHeaderNames()).andReturn(Collections.enumeration(headers));
request.getHeaders("content-type");
- EasyMock.expectLastCall().andReturn(httpFields.getValues("content-type"));
+ EasyMock.expectLastCall().andReturn(Collections.enumeration(ct));
request.getHeaders(JettyHTTPDestinationTest.AUTH_HEADER);
- EasyMock.expectLastCall().andReturn(
- httpFields.getValues(JettyHTTPDestinationTest.AUTH_HEADER));
+ EasyMock.expectLastCall()
+ .andReturn(Collections
+ .enumeration(Collections
+ .singletonList(JettyHTTPDestinationTest.BASIC_AUTH)));
EasyMock.expect(request.getInputStream()).andReturn(is);
request.setHandled(true);