You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/06/02 00:34:57 UTC
[cxf] 01/02: CXF-8532: @Priority support for ParamConverterProvider
(#807)
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 6328f906357ed7f0d8e20e4538c91fd4b34338b9
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Tue Jun 1 17:42:34 2021 -0400
CXF-8532: @Priority support for ParamConverterProvider (#807)
(cherry picked from commit 33478ad5cfcbab35404680bfb40cb481be760a03)
(cherry picked from commit cc9efac873f7dd69753ef5674adf1127e60ce06d)
---
.../apache/cxf/jaxrs/provider/ProviderFactory.java | 33 +++++++++++++-
.../jaxrs/PriorityCustomerParameterHandler.java | 53 ++++++++++++++++++++++
.../cxf/jaxrs/provider/ProviderFactoryTest.java | 45 ++++++++++++++++++
.../jaxrs/client/ClientProviderFactoryTest.java | 51 +++++++++++++++++++++
.../java/org/apache/cxf/jaxrs/client/Customer.java | 32 +++++++++++++
.../cxf/jaxrs/client/CustomerParameterHandler.java | 50 ++++++++++++++++++++
.../client/PriorityCustomerParameterHandler.java | 53 ++++++++++++++++++++++
7 files changed, 316 insertions(+), 1 deletion(-)
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index f3d23b4..3c62ebc 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -662,6 +662,7 @@ public abstract class ProviderFactory {
sortReaders();
sortWriters();
sortContextResolvers();
+ sortParamConverterProviders();
mapInterceptorFilters(readerInterceptors, readInts, ReaderInterceptor.class, true);
mapInterceptorFilters(writerInterceptors, writeInts, WriterInterceptor.class, true);
@@ -742,6 +743,14 @@ public abstract class ProviderFactory {
doCustomSort(messageWriters);
}
}
+
+ private <T> void sortParamConverterProviders() {
+ if (!customComparatorAvailable(ParamConverterProvider.class)) {
+ paramConverters.sort(new ParamConverterProviderComparator(bus));
+ } else {
+ doCustomSort(paramConverters);
+ }
+ }
private boolean customComparatorAvailable(Class<?> providerClass) {
if (providerComparator != null) {
@@ -912,6 +921,27 @@ public abstract class ProviderFactory {
}
}
+ private static class ParamConverterProviderComparator implements Comparator<ProviderInfo<ParamConverterProvider>> {
+ private final Bus bus;
+
+ ParamConverterProviderComparator(Bus bus) {
+ this.bus = bus;
+ }
+
+ @Override
+ public int compare(ProviderInfo<ParamConverterProvider> p1, ProviderInfo<ParamConverterProvider> p2) {
+ final int result = compareCustomStatus(p1, p2);
+ if (result != 0) {
+ return result;
+ }
+
+ final Class<?> cl1 = ClassHelper.getRealClass(bus, p1.getProvider());
+ final Class<?> cl2 = ClassHelper.getRealClass(bus, p2.getProvider());
+
+ return comparePriorityStatus(cl1, cl2);
+ }
+ }
+
protected static int compareCustomStatus(ProviderInfo<?> p1, ProviderInfo<?> p2) {
Boolean custom1 = p1.isCustom();
Boolean custom2 = p2.isCustom();
@@ -1480,7 +1510,8 @@ public abstract class ProviderFactory {
sortReaders();
sortWriters();
-
+ sortParamConverterProviders();
+
NameKeyMap<ProviderInfo<ReaderInterceptor>> sortedReaderInterceptors =
new NameKeyMap<>(
(Comparator<ProviderInfo<?>>) providerComparator, true);
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/PriorityCustomerParameterHandler.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/PriorityCustomerParameterHandler.java
new file mode 100644
index 0000000..d5fe732
--- /dev/null
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/PriorityCustomerParameterHandler.java
@@ -0,0 +1,53 @@
+/**
+ * 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.jaxrs;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.annotation.Priority;
+import javax.ws.rs.Priorities;
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+
+
+@Priority(Priorities.USER - 1)
+public class PriorityCustomerParameterHandler implements ParamConverterProvider, ParamConverter<Customer> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
+ if (Customer.class == cls) {
+ return (ParamConverter<T>)this;
+ }
+ return null;
+ }
+
+ public Customer fromString(String s) throws IllegalArgumentException {
+ Customer c = new Customer();
+ c.setName(s);
+ return c;
+ }
+
+ @Override
+ public String toString(Customer arg0) throws IllegalArgumentException {
+ return null;
+ }
+}
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index cb0435a..21a07af 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -67,6 +67,7 @@ import org.apache.cxf.jaxrs.Customer;
import org.apache.cxf.jaxrs.CustomerParameterHandler;
import org.apache.cxf.jaxrs.JAXBContextProvider;
import org.apache.cxf.jaxrs.JAXBContextProvider2;
+import org.apache.cxf.jaxrs.PriorityCustomerParameterHandler;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
@@ -669,8 +670,52 @@ public class ProviderFactoryTest {
new MessageImpl());
assertSame(h2, h);
}
+
+ @Test
+ public void testParameterHandlerProviderWithPriority() throws Exception {
+ ProviderFactory pf = ServerProviderFactory.getInstance();
+ ParamConverterProvider h = new CustomerParameterHandler();
+ ParamConverterProvider hp = new PriorityCustomerParameterHandler();
+ pf.registerUserProvider(h);
+ pf.registerUserProvider(hp);
+ ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null,
+ new MessageImpl());
+ assertSame(h2, hp);
+ }
@Test
+ public void testCustomProviderSortingParamConverterProvider() {
+ ParamConverterProvider h = new CustomerParameterHandler();
+ ParamConverterProvider hp = new PriorityCustomerParameterHandler();
+
+ ProviderFactory pf = ServerProviderFactory.getInstance();
+ pf.setUserProviders(Arrays.asList(h, hp));
+
+ Comparator<ProviderInfo<ParamConverterProvider>> comp =
+ new Comparator<ProviderInfo<ParamConverterProvider>>() {
+
+ @Override
+ public int compare(
+ ProviderInfo<ParamConverterProvider> o1,
+ ProviderInfo<ParamConverterProvider> o2) {
+
+ ParamConverterProvider provider1 = o1.getProvider();
+ ParamConverterProvider provider2 = o2.getProvider();
+
+ return provider1.getClass().getName().compareTo(
+ provider2.getClass().getName());
+ }
+
+ };
+
+ pf.setProviderComparator(comp);
+
+ ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null,
+ new MessageImpl());
+ assertSame(h2, h);
+ }
+
+ @Test
public void testGetStringProvider() throws Exception {
verifyProvider(String.class, StringTextProvider.class, "text/plain");
}
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/ClientProviderFactoryTest.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/ClientProviderFactoryTest.java
new file mode 100644
index 0000000..2d4815a
--- /dev/null
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/ClientProviderFactoryTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.jaxrs.client;
+
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertSame;
+
+
+public class ClientProviderFactoryTest {
+
+ @Test
+ public void testParameterHandlerProviderWithPriority() throws Exception {
+ final Bus bus = new ExtensionManagerBus();
+ final ProviderFactory pf = ClientProviderFactory.createInstance(bus);
+
+ ParamConverterProvider h = new CustomerParameterHandler();
+ ParamConverterProvider hp = new PriorityCustomerParameterHandler();
+ pf.registerUserProvider(h);
+ pf.registerUserProvider(hp);
+ ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null,
+ new MessageImpl());
+ assertSame(h2, hp);
+ }
+
+}
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/Customer.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/Customer.java
new file mode 100644
index 0000000..f7506e0
--- /dev/null
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/Customer.java
@@ -0,0 +1,32 @@
+/**
+ * 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.jaxrs.client;
+
+public class Customer {
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/CustomerParameterHandler.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/CustomerParameterHandler.java
new file mode 100644
index 0000000..b04294e
--- /dev/null
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/CustomerParameterHandler.java
@@ -0,0 +1,50 @@
+/**
+ * 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.jaxrs.client;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+
+
+public class CustomerParameterHandler implements ParamConverterProvider, ParamConverter<Customer> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
+ if (Customer.class == cls) {
+ return (ParamConverter<T>)this;
+ }
+ return null;
+ }
+
+ public Customer fromString(String s) throws IllegalArgumentException {
+ Customer c = new Customer();
+ c.setName(s);
+ return c;
+ }
+
+ @Override
+ public String toString(Customer arg0) throws IllegalArgumentException {
+ return null;
+ }
+}
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/PriorityCustomerParameterHandler.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/PriorityCustomerParameterHandler.java
new file mode 100644
index 0000000..f84eeef
--- /dev/null
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/PriorityCustomerParameterHandler.java
@@ -0,0 +1,53 @@
+/**
+ * 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.jaxrs.client;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.annotation.Priority;
+import javax.ws.rs.Priorities;
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+
+
+@Priority(Priorities.USER - 1)
+public class PriorityCustomerParameterHandler implements ParamConverterProvider, ParamConverter<Customer> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> ParamConverter<T> getConverter(Class<T> cls, Type arg1, Annotation[] arg2) {
+ if (Customer.class == cls) {
+ return (ParamConverter<T>)this;
+ }
+ return null;
+ }
+
+ public Customer fromString(String s) throws IllegalArgumentException {
+ Customer c = new Customer();
+ c.setName(s);
+ return c;
+ }
+
+ @Override
+ public String toString(Customer arg0) throws IllegalArgumentException {
+ return null;
+ }
+}