cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6588] Support for injecting contexts into ParamConverterProvider
Date Fri, 11 Sep 2015 12:42:06 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 91278f9fd -> 1e6f12f65


[CXF-6588] Support for injecting contexts into ParamConverterProvider


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1e6f12f6
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1e6f12f6
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1e6f12f6

Branch: refs/heads/master
Commit: 1e6f12f65bc490a5ba5f274b6e407abbf0a19d43
Parents: 91278f9
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Sep 11 13:41:49 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Sep 11 13:41:49 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/provider/ProviderFactory.java     | 55 +++++++++------
 .../apache/cxf/jaxrs/utils/InjectionUtils.java  |  2 +-
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java  |  2 +-
 .../cxf/jaxrs/provider/ProviderFactoryTest.java |  3 +-
 .../apache/cxf/jaxrs/client/AbstractClient.java | 20 +++++-
 .../search/AbstractSearchConditionParser.java   |  2 +-
 .../apache/cxf/systest/jaxrs/BookServer.java    | 71 ++++++++++++++++++++
 .../org/apache/cxf/systest/jaxrs/BookStore.java |  7 ++
 .../jaxrs/JAXRSClientServerBookTest.java        |  9 +++
 9 files changed, 143 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
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 7fd25a2..780cd94 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
@@ -32,7 +32,6 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -107,8 +106,9 @@ public abstract class ProviderFactory {
     private List<ProviderInfo<ContextProvider<?>>> contextProviders = 
         new ArrayList<ProviderInfo<ContextProvider<?>>>(1);
     
-    private Set<ParamConverterProvider> newParamConverters;
-    
+    private List<ProviderInfo<ParamConverterProvider>> paramConverters =
+        new ArrayList<ProviderInfo<ParamConverterProvider>>(1);
+    private boolean paramConverterContextsAvailable;
     // List of injected providers
     private Collection<ProviderInfo<?>> injectedProviders = 
         new LinkedList<ProviderInfo<?>>();
@@ -250,17 +250,19 @@ public abstract class ProviderFactory {
     
     public <T> ParamConverter<T> createParameterHandler(Class<T> paramType,

                                                         Type genericType,
-                                                        Annotation[] anns) {
+                                                        Annotation[] anns,
+                                                        Message m) {
         
-        if (newParamConverters != null) {
-            anns = anns != null ? anns : new Annotation[]{};
-            for (ParamConverterProvider newParamConverter : newParamConverters) {
-                ParamConverter<T> converter = newParamConverter.getConverter(paramType,
genericType, anns);
-                if (converter != null) {
-                    return converter;
-                }
+        anns = anns != null ? anns : new Annotation[]{};
+        for (ProviderInfo<ParamConverterProvider> pi : paramConverters) {
+            injectContextValues(pi, m);
+            ParamConverter<T> converter = pi.getProvider().getConverter(paramType,
genericType, anns);
+            if (converter != null) {
+                return converter;
+            } else {
+                pi.clearThreadLocalProxies();
             }
-        } 
+        }
         return null;
     }
     
@@ -349,7 +351,7 @@ public abstract class ProviderFactory {
                 List<ProviderInfo<ReaderInterceptor>> readers =
                     getBoundFilters(readerInterceptors, names);
                 for (ProviderInfo<ReaderInterceptor> p : readers) {
-                    InjectionUtils.injectContexts(p.getProvider(), p, m);
+                    injectContextValues(p, m);
                     interceptors.add(p.getProvider());
                 }
                 interceptors.add(mbrReader);
@@ -388,7 +390,7 @@ public abstract class ProviderFactory {
                 List<ProviderInfo<WriterInterceptor>> writers =
                     getBoundFilters(writerInterceptors, names);
                 for (ProviderInfo<WriterInterceptor> p : writers) {
-                    InjectionUtils.injectContexts(p.getProvider(), p, m);
+                    injectContextValues(p, m);
                     interceptors.add(p.getProvider());
                 }
                 interceptors.add(mbwWriter);
@@ -501,12 +503,7 @@ public abstract class ProviderFactory {
             }
             
             if (ParamConverterProvider.class.isAssignableFrom(providerCls)) {
-                //TODO: review the possibility of ParamConverterProvider needing to have
Contexts injected
-                Object converter = provider.getProvider();
-                if (newParamConverters == null) {
-                    newParamConverters = new LinkedHashSet<ParamConverterProvider>();
-                }
-                newParamConverters.add((ParamConverterProvider)converter);
+                paramConverters.add((ProviderInfo<ParamConverterProvider>)provider);
             }
         }
         sortReaders();
@@ -516,10 +513,25 @@ public abstract class ProviderFactory {
         mapInterceptorFilters(readerInterceptors, readInts, ReaderInterceptor.class, true);
         mapInterceptorFilters(writerInterceptors, writeInts, WriterInterceptor.class, true);
         
-        injectContextProxies(messageReaders, messageWriters, contextResolvers, 
+        injectContextProxies(messageReaders, messageWriters, contextResolvers, paramConverters,
             readerInterceptors.values(), writerInterceptors.values());
+        checkParamConverterContexts();
     }
     
+    private void checkParamConverterContexts() {
+        for (ProviderInfo<ParamConverterProvider> pi : paramConverters) {
+            if (pi.contextsAvailable()) {
+                paramConverterContextsAvailable = true;
+            }
+        }
+        
+    }
+    public boolean isParamConverterContextsAvailable() {
+        return paramConverterContextsAvailable;
+    }
+    
+    
+    
     protected void injectContextValues(ProviderInfo<?> pi, Message m) {
         if (m != null) {
             InjectionUtils.injectContexts(pi.getProvider(), pi, m);
@@ -790,6 +802,7 @@ public abstract class ProviderFactory {
         contextProviders.clear();
         readerInterceptors.clear();
         writerInterceptors.clear();
+        paramConverters.clear();
     }
     
     public void setBus(Bus bus) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
index 36ee7f2..3850393 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
@@ -470,7 +470,7 @@ public final class InjectionUtils {
         T result = null;
         if (message != null) {
             ServerProviderFactory pf = ServerProviderFactory.getInstance(message);
-            ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns);
+            ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns,
message);
             if (pm != null) {
                 result = pm.fromString(value);
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index af9fc58..9669902 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -1065,7 +1065,7 @@ public final class JAXRSUtils {
     public static <T> T createContextValue(Message m, Type genericType, Class<T>
clazz) {
  
         Message contextMessage = m.getExchange() != null ? m.getExchange().getInMessage()
: m;
-        if (contextMessage == null && Boolean.FALSE.equals(m.get(Message.INBOUND_MESSAGE)))
{
+        if (contextMessage == null && !PropertyUtils.isTrue(m.get(Message.INBOUND_MESSAGE)))
{
             contextMessage = m;
         }
         Object o = null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
----------------------------------------------------------------------
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 5bec732..2f66a70 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
@@ -512,7 +512,8 @@ public class ProviderFactoryTest extends Assert {
         ProviderFactory pf = ServerProviderFactory.getInstance();
         ParamConverterProvider h = new CustomerParameterHandler();
         pf.registerUserProvider(h);
-        ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class,
null);
+        ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class,
null, 
+                                                                new MessageImpl());
         assertSame(h2, h);
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
index b16269a..a25d0f7 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
@@ -84,6 +84,7 @@ import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageContentsList;
+import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
@@ -740,13 +741,26 @@ public abstract class AbstractClient implements Client {
         }
         ProviderFactory pf = ClientProviderFactory.getInstance(cfg.getEndpoint());
         if (pf != null) {
+            Message m = null;
+            if (pf.isParamConverterContextsAvailable()) {
+                m = new MessageImpl();
+                m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
+                m.setExchange(new ExchangeImpl());
+                m.getExchange().setOutMessage(m);
+                m.getExchange().put(Endpoint.class, cfg.getEndpoint());
+            }
             Class<?> pClass = pValue.getClass();
-            
             @SuppressWarnings("unchecked")
             ParamConverter<Object> prov = 
-                (ParamConverter<Object>)pf.createParameterHandler(pClass, pClass, anns);
+                (ParamConverter<Object>)pf.createParameterHandler(pClass, pClass, anns,
m);
             if (prov != null) {
-                return prov.toString(pValue);
+                try {
+                    return prov.toString(pValue);
+                } finally {
+                    if (m != null) {
+                        pf.clearThreadLocalProxies();
+                    }
+                }
             }
         }
         return pValue.toString();

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
index 74c7a68..e0be04b 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/AbstractSearchConditionParser.java
@@ -233,7 +233,7 @@ public abstract class AbstractSearchConditionParser<T> implements
SearchConditio
     private boolean paramConverterAvailable(Class<?> pClass) {
         Message m = JAXRSUtils.getCurrentMessage();
         ServerProviderFactory pf = m == null ? null : ServerProviderFactory.getInstance(m);
-        return pf != null && pf.createParameterHandler(pClass, pClass, EMPTY_ANNOTTAIONS)
!= null;
+        return pf != null && pf.createParameterHandler(pClass, pClass, EMPTY_ANNOTTAIONS,
m) != null;
     }
 
     private CollectionCheck getCollectionCheck(String propName, boolean isCollection, Class<?>
actualCls) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
index 4ef1294..5d9cb09 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
@@ -19,13 +19,18 @@
 
 package org.apache.cxf.systest.jaxrs;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.BadRequestException;
 import javax.ws.rs.client.ClientRequestContext;
 import javax.ws.rs.client.ClientResponseContext;
 import javax.ws.rs.client.ClientResponseFilter;
@@ -34,8 +39,15 @@ import javax.ws.rs.container.ContainerRequestFilter;
 import javax.ws.rs.container.ContainerResponseContext;
 import javax.ws.rs.container.ContainerResponseFilter;
 import javax.ws.rs.container.PreMatching;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
+import javax.ws.rs.ext.Providers;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
@@ -50,6 +62,7 @@ import org.apache.cxf.jaxrs.ext.search.QueryContextProvider;
 import org.apache.cxf.jaxrs.ext.search.SearchBean;
 import org.apache.cxf.jaxrs.ext.search.SearchContextProvider;
 import org.apache.cxf.jaxrs.ext.search.sql.SQLPrinterVisitor;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
 import org.apache.cxf.jaxrs.provider.BinaryDataProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
@@ -111,6 +124,7 @@ public class BookServer extends AbstractBusTestServerBase {
         providers.add(new BlockingRequestFilter());
         providers.add(new FaultyResponseFilter());
         providers.add(new BlockedExceptionMapper());
+        providers.add(new XmlBookParamConverter());
         sf.setProviders(providers);
         List<Interceptor<? extends Message>> inInts = new ArrayList<Interceptor<?
extends Message>>();
         inInts.add(new CustomInFaultyInterceptor());
@@ -256,4 +270,61 @@ public class BookServer extends AbstractBusTestServerBase {
         }
         
     }
+    public static class XmlBookParamConverter implements ParamConverterProvider {
+
+        @Context
+        private Providers providers;
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> ParamConverter<T> getConverter(Class<T> rawType, Type
genericType,
+                                                  Annotation[] annotations) {
+            if (rawType != Book.class) {
+                return null;
+            }
+            MessageBodyReader<Book> mbr = providers.getMessageBodyReader(Book.class,

+                                                                         Book.class, 
+                                                                         annotations, 
+                                                                         MediaType.APPLICATION_XML_TYPE);
+            MessageBodyWriter<Book> mbw = providers.getMessageBodyWriter(Book.class,

+                                                                         Book.class, 
+                                                                         annotations, 
+                                                                         MediaType.APPLICATION_XML_TYPE);
+            return (ParamConverter<T>)new XmlParamConverter(mbr, mbw);
+            
+        }
+        private static class XmlParamConverter implements ParamConverter<Book> {
+            private MessageBodyReader<Book> mbr;
+            private MessageBodyWriter<Book> mbw;
+            public XmlParamConverter(MessageBodyReader<Book> mbr, MessageBodyWriter<Book>
mbw) {  
+                this.mbr = mbr;
+                this.mbw = mbw;
+            }
+            @Override
+            public Book fromString(String value) {
+                try {
+                    return mbr.readFrom(Book.class, Book.class, 
+                                        new Annotation[]{}, 
+                                        MediaType.APPLICATION_XML_TYPE, 
+                                        new MetadataMap<String, String>(), 
+                                        new ByteArrayInputStream(value.getBytes()));
+                } catch (IOException ex) {
+                    throw new BadRequestException(ex);
+                }
+            }
+            @Override
+            public String toString(Book value) {
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                try {
+                    mbw.writeTo(value, Book.class, Book.class, 
+                                new Annotation[]{}, 
+                                MediaType.APPLICATION_XML_TYPE, 
+                                new MetadataMap<String, Object>(),
+                                bos);
+                } catch (IOException ex) {
+                    throw new BadRequestException(ex);
+                }
+                return bos.toString();
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 06ced26..5e957b6 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -304,6 +304,13 @@ public class BookStore {
     }
     
     @POST
+    @Path("/echoxmlbookquery")
+    @Produces("application/xml")
+    public Book echoXmlBookQuery(@QueryParam("book") Book book) {
+        return book;
+    }
+    
+    @POST
     @Path("/emptyform")
     @Produces("text/plain")
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)

http://git-wip-us.apache.org/repos/asf/cxf/blob/1e6f12f6/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 07a93f6..9109acf 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -132,6 +132,15 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase
{
         assertEquals(125L, b.getId());
     }
     @Test
+    public void testEchoXmlBookQuery() throws Exception {
+        String address = "http://localhost:" + PORT;
+        BookStore store = JAXRSClientFactory.create(address, BookStore.class,
+            Collections.singletonList(new BookServer.XmlBookParamConverter()));
+        Book b = store.echoXmlBookQuery(new Book("query", 125L));
+        assertEquals(125L, b.getId());
+        assertEquals("query", b.getName());
+    }
+    @Test
     public void testGetBookRoot() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/;JSESSIONID=xxx";
         WebClient wc = WebClient.create(address);


Mime
View raw message