cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1207484 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/
Date Mon, 28 Nov 2011 17:51:27 GMT
Author: sergeyb
Date: Mon Nov 28 17:51:27 2011
New Revision: 1207484

URL: http://svn.apache.org/viewvc?rev=1207484&view=rev
Log:
Merged revisions 1207482 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1207482 | sergeyb | 2011-11-28 17:48:58 +0000 (Mon, 28 Nov 2011) | 1 line
  
  [CXF-3938] Injecting contexts into the providers at the selection stage
........

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
    cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Nov 28 17:51:27 2011
@@ -1 +1 @@
-/cxf/trunk:1205786,1205830,1205996-1205997,1206800,1207334
+/cxf/trunk:1205786,1205830,1205996-1205997,1206800,1207334,1207482

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1207484&r1=1207483&r2=1207484&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Mon Nov 28 17:51:27 2011
@@ -118,7 +118,7 @@ public final class ProviderFactory {
             jaxbReaders.add(new ProviderInfo<MessageBodyReader>((MessageBodyReader)jsonProvider));
             jaxbWriters.add(new ProviderInfo<MessageBodyWriter>((MessageBodyWriter)jsonProvider));
         }
-        injectContexts(jaxbReaders, jaxbWriters);
+        injectContextProxies(jaxbReaders, jaxbWriters);
     }
     
     
@@ -172,9 +172,7 @@ public final class ProviderFactory {
                     Type[] args = pt.getActualTypeArguments();
                     for (int i = 0; i < args.length; i++) {
                         if (contextType == args[i]) {
-                            
-                            InjectionUtils.injectContextFields(cr.getProvider(), cr, m);
-                            InjectionUtils.injectContextMethods(cr.getProvider(), cr, m);
+                            injectContextValues(cr, m);
                             return cr.getProvider();
                         }
                     }
@@ -202,7 +200,7 @@ public final class ProviderFactory {
         List<ExceptionMapper<T>> candidates = new LinkedList<ExceptionMapper<T>>();
         
         for (ProviderInfo<ExceptionMapper> em : exceptionMappers) {
-            handleMapper((List)candidates, em, exceptionType, m, ExceptionMapper.class);
+            handleMapper((List)candidates, em, exceptionType, m, ExceptionMapper.class, true);
         }
         if (candidates.size() == 0) {
             return null;
@@ -217,7 +215,7 @@ public final class ProviderFactory {
         List<ParameterHandler<T>> candidates = new LinkedList<ParameterHandler<T>>();
         
         for (ProviderInfo<ParameterHandler> em : paramHandlers) {
-            handleMapper((List)candidates, em, paramType, null, ParameterHandler.class);
+            handleMapper((List)candidates, em, paramType, null, ParameterHandler.class, true);
         }
         if (candidates.size() == 0) {
             return null;
@@ -233,7 +231,7 @@ public final class ProviderFactory {
         List<ResponseExceptionMapper<T>> candidates = new LinkedList<ResponseExceptionMapper<T>>();
         
         for (ProviderInfo<ResponseExceptionMapper> em : responseExceptionMappers) {
-            handleMapper((List)candidates, em, paramType, null, ResponseExceptionMapper.class);
+            handleMapper((List)candidates, em, paramType, null, ResponseExceptionMapper.class,
true);
         }
         if (candidates.size() == 0) {
             return null;
@@ -242,8 +240,12 @@ public final class ProviderFactory {
         return candidates.get(0);
     }
     
-    private static void handleMapper(List<Object> candidates, ProviderInfo em, 
-                                     Class<?> expectedType, Message m, Class<?>
providerClass) {
+    private static void handleMapper(List<Object> candidates, 
+                                     ProviderInfo em, 
+                                     Class<?> expectedType, 
+                                     Message m, 
+                                     Class<?> providerClass,
+                                     boolean injectContext) {
         
         Class<?> mapperClass =  ClassHelper.getRealClass(em.getProvider());
         Type[] types = getGenericInterfaces(mapperClass);
@@ -267,9 +269,8 @@ public final class ProviderFactory {
                         if (!isResolved) {
                             return;
                         }
-                        if (m != null) {
-                            InjectionUtils.injectContextFields(em.getProvider(), em, m);
-                            InjectionUtils.injectContextMethods(em.getProvider(), em, m);
+                        if (injectContext) {
+                            injectContextValues(em, m);
                         }
                         candidates.add(em.getProvider());
                         return;
@@ -279,15 +280,17 @@ public final class ProviderFactory {
                         continue;
                     }
                     if (actualClass.isAssignableFrom(expectedType)) {
-                        if (m != null) {
-                            InjectionUtils.injectContextFields(em.getProvider(), em, m);
-                            InjectionUtils.injectContextMethods(em.getProvider(), em, m);
+                        if (injectContext) {
+                            injectContextValues(em, m);
                         }
                         candidates.add(em.getProvider());
                         return;
                     }
                 }
             } else if (t instanceof Class && ((Class<?>)t).isAssignableFrom(providerClass))
{
+                if (injectContext) {
+                    injectContextValues(em, m);
+                }
                 candidates.add(em.getProvider());
             }
         }
@@ -437,12 +440,19 @@ public final class ProviderFactory {
         sortReaders();
         sortWriters();
         
-        injectContexts(messageReaders, messageWriters, contextResolvers, requestHandlers,
responseHandlers,
+        injectContextProxies(messageReaders, messageWriters, contextResolvers, requestHandlers,
responseHandlers,
                        exceptionMappers);
     }
 //CHECKSTYLE:ON
     
-    void injectContexts(List<?> ... providerLists) {
+    static void injectContextValues(ProviderInfo pi, Message m) {
+        if (m != null) {
+            InjectionUtils.injectContextFields(pi.getProvider(), pi, m);
+            InjectionUtils.injectContextMethods(pi.getProvider(), pi, m);
+        }
+    }
+    
+    void injectContextProxies(List<?> ... providerLists) {
         for (List<?> list : providerLists) {
             for (Object p : list) {
                 ProviderInfo pi = (ProviderInfo)p;
@@ -490,15 +500,11 @@ public final class ProviderFactory {
                                                          Message m) {
         List<MessageBodyReader<T>> candidates = new LinkedList<MessageBodyReader<T>>();
         for (ProviderInfo<MessageBodyReader> ep : readers) {
-            if (matchesReaderCriterias(ep.getProvider(), type, genericType, annotations,
mediaType)) {
+            if (matchesReaderCriterias(ep, type, genericType, annotations, mediaType, m))
{
                 if (this == SHARED_FACTORY) {
-                    if (!isJaxbBasedProvider(ep.getProvider())) {
-                        InjectionUtils.injectContextFields(ep.getProvider(), ep, m);
-                        InjectionUtils.injectContextMethods(ep.getProvider(), ep, m);
-                    }
                     return ep.getProvider();
                 }
-                handleMapper((List)candidates, ep, type, m, MessageBodyReader.class);
+                handleMapper((List)candidates, ep, type, m, MessageBodyReader.class, false);
             }
         }     
         
@@ -510,11 +516,13 @@ public final class ProviderFactory {
         
     }
     
-    private <T> boolean matchesReaderCriterias(MessageBodyReader<T> ep,
+    private <T> boolean matchesReaderCriterias(ProviderInfo<MessageBodyReader>
pi,
                                                Class<T> type,
                                                Type genericType,
                                                Annotation[] annotations,
-                                               MediaType mediaType) {
+                                               MediaType mediaType,
+                                               Message m) {
+        MessageBodyReader<?> ep = pi.getProvider();
         List<MediaType> supportedMediaTypes = JAXRSUtils.getProviderConsumeTypes(ep);
         
         List<MediaType> availableMimeTypes = 
@@ -523,9 +531,16 @@ public final class ProviderFactory {
         if (availableMimeTypes.size() == 0) {
             return false;
         }
-        
-        return ep.isReadable(type, genericType, annotations, mediaType);
-        
+        boolean injected = false;
+        if (this != SHARED_FACTORY || !isJaxbBasedProvider(ep)) {
+            injectContextValues(pi, m);
+            injected = true;
+        }
+        boolean matches = ep.isReadable(type, genericType, annotations, mediaType);
+        if (!matches && injected) {
+            pi.clearThreadLocalProxies();
+        }
+        return matches;
     }
         
     /**
@@ -547,15 +562,11 @@ public final class ProviderFactory {
                                                          Message m) {
         List<MessageBodyWriter<T>> candidates = new LinkedList<MessageBodyWriter<T>>();
         for (ProviderInfo<MessageBodyWriter> ep : writers) {
-            if (matchesWriterCriterias(ep.getProvider(), type, genericType, annotations,
mediaType)) {
+            if (matchesWriterCriterias(ep, type, genericType, annotations, mediaType, m))
{
                 if (this == SHARED_FACTORY) {
-                    if (!isJaxbBasedProvider(ep.getProvider())) {
-                        InjectionUtils.injectContextFields(ep.getProvider(), ep, m);
-                        InjectionUtils.injectContextMethods(ep.getProvider(), ep, m);
-                    }
                     return ep.getProvider();
                 }
-                handleMapper((List)candidates, ep, type, m, MessageBodyWriter.class);
+                handleMapper((List)candidates, ep, type, m, MessageBodyWriter.class, false);
             }
         }     
         if (candidates.size() == 0) {
@@ -565,11 +576,13 @@ public final class ProviderFactory {
         return candidates.get(0);
     }
     
-    private <T> boolean matchesWriterCriterias(MessageBodyWriter<T> ep,
+    private <T> boolean matchesWriterCriterias(ProviderInfo<MessageBodyWriter>
pi,
                                                Class<T> type,
                                                Type genericType,
                                                Annotation[] annotations,
-                                               MediaType mediaType) {
+                                               MediaType mediaType,
+                                               Message m) {
+        MessageBodyWriter<?> ep = pi.getProvider();
         List<MediaType> supportedMediaTypes = JAXRSUtils.getProviderProduceTypes(ep);
         
         List<MediaType> availableMimeTypes = 
@@ -579,7 +592,16 @@ public final class ProviderFactory {
         if (availableMimeTypes.size() == 0) {
             return false;
         }
-        return ep.isWriteable(type, genericType, annotations, mediaType); 
+        boolean injected = false;
+        if (this != SHARED_FACTORY || !isJaxbBasedProvider(ep)) {
+            injectContextValues(pi, m);
+            injected = true;
+        }
+        boolean matches = ep.isWriteable(type, genericType, annotations, mediaType);
+        if (!matches && injected) {
+            pi.clearThreadLocalProxies();
+        }
+        return matches;
     }
     
     List<ProviderInfo<MessageBodyReader>> getMessageReaders() {

Modified: cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java?rev=1207484&r1=1207483&r2=1207484&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
(original)
+++ cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/StringTextWriter.java
Mon Nov 28 17:51:27 2011
@@ -24,18 +24,34 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
 
 import org.apache.cxf.jaxrs.provider.PrimitiveTextProvider;
 
 public class StringTextWriter extends PrimitiveTextProvider {
+    
+    @Context
+    private UriInfo ui;
+    
     @Override
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
         return false;
     }
     
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+        String path = ui.getAbsolutePath().toString();
+        if (path.endsWith("/webapp/resources/bookstore/nonexistent")) {
+            return super.isWriteable(type, genericType, annotations, mt);
+        } else {
+            return false;
+        }
+    }
+    
     public void writeTo(Object obj, Class<?> type, Type genType, Annotation[] anns,

                         MediaType mt, MultivaluedMap<String, Object> headers,
                         OutputStream os) throws IOException {

Modified: cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml?rev=1207484&r1=1207483&r2=1207484&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
(original)
+++ cxf/branches/2.4.x-fixes/systests/jaxrs/src/test/resources/jaxrs_spring_providers/WEB-INF/beans.xml
Mon Nov 28 17:51:27 2011
@@ -48,4 +48,5 @@ http://cxf.apache.org/schemas/jaxrs.xsd"
     </bean>   
     <bean id="exceptionMapper2" class="org.apache.cxf.systest.jaxrs.RuntimeExceptionMapper"/>
     <bean id="plainTextProvider" class="org.apache.cxf.systest.jaxrs.StringTextWriter"/>
+    
 </beans>



Mime
View raw message