Coverage Report - org.kuali.rice.core.cxf.interceptors.ImmutableCollectionsInInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
ImmutableCollectionsInInterceptor
0%
0/38
0%
0/20
5
 
 1  
 package org.kuali.rice.core.cxf.interceptors;
 2  
 
 3  
 import org.apache.cxf.interceptor.Fault;
 4  
 import org.apache.cxf.message.Message;
 5  
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 6  
 import org.apache.cxf.phase.Phase;
 7  
 
 8  
 import java.lang.reflect.Field;
 9  
 import java.util.Collection;
 10  
 import java.util.Collections;
 11  
 import java.util.List;
 12  
 import java.util.Map;
 13  
 import java.util.Set;
 14  
 
 15  
 /**
 16  
  * A CXF Interceptor that binds itself to the USER_LOGICAL phase to be used on inbound
 17  
  * messages.  This interceptor is invoked in the interceptor chain after unmarshalling
 18  
  * from XML to Java has occurred.  The role of this interceptor is to ensure that any
 19  
  * Collection (and specifically List, Set, or Map) used in a @WebMethod is ultimately of the
 20  
  * expected immutable type returned from the local service.
 21  
  */
 22  
 @SuppressWarnings("unused")
 23  
 public class ImmutableCollectionsInInterceptor extends AbstractPhaseInterceptor<Message> {
 24  
 
 25  
     /**
 26  
      * Instantiates an ImmutableCollectionsInInterceptor and adds it to the USER_LOGICAL phase.
 27  
      */
 28  
     public ImmutableCollectionsInInterceptor() {
 29  0
         super(Phase.USER_LOGICAL);
 30  0
     }
 31  
 
 32  
     @Override
 33  
     public void handleMessage(final Message message) throws Fault {
 34  
         try {
 35  0
             List contents = message.getContent(List.class);
 36  0
             for (Object o : contents) {
 37  0
                 makeCollectionFieldsImmutable(o);
 38  
             }
 39  0
         } catch (Exception e) {
 40  0
             throw new Fault(e);
 41  0
         }
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Accepts an object whose fields of type List, Set, or Collection need to be made immutable irrespective of field
 46  
      * visibility.  The passed in object is mutated as a result.
 47  
      *
 48  
      * @param o - The object whose List, Set, or Collection fields will be made immutable
 49  
      * @throws IllegalAccessException
 50  
      */
 51  
     @SuppressWarnings("unchecked")
 52  
     void makeCollectionFieldsImmutable(Object o) throws IllegalAccessException {
 53  0
         Class<?> targetClass = o.getClass();
 54  0
         for (Field f : targetClass.getDeclaredFields()) {
 55  
 
 56  0
             f.setAccessible(true);
 57  
 
 58  0
             if (f.getType().isAssignableFrom(List.class)) {
 59  0
                 List original = (List) f.get(o);
 60  0
                 if (original == null) {
 61  0
                     original = Collections.emptyList();
 62  
                 }
 63  0
                 List immutable = Collections.unmodifiableList(original);
 64  0
                 f.set(o, immutable);
 65  
 
 66  0
             } else if (f.getType().isAssignableFrom(Set.class)) {
 67  0
                 Set original = (Set) f.get(o);
 68  0
                 if (original == null) {
 69  0
                     original = Collections.emptySet();
 70  
                 }
 71  0
                 Set immutable = Collections.unmodifiableSet(original);
 72  0
                 f.set(o, immutable);
 73  0
             } else if (f.getType().isAssignableFrom(Collection.class)) {
 74  0
                 Collection original = (Collection) f.get(o);
 75  0
                 if (original == null) {
 76  0
                     original = Collections.emptyList();
 77  
                 }
 78  0
                 Collection immutable = Collections.unmodifiableCollection(original);
 79  0
                 f.set(o, immutable);
 80  0
             } else if (f.getType().isAssignableFrom(Map.class)) {
 81  0
                 Map original = (Map) f.get(o);
 82  0
                 if (original == null) {
 83  0
                     original = Collections.emptyMap();
 84  
                 }
 85  0
                 Map immutable = Collections.unmodifiableMap(original);
 86  0
                 f.set(o, immutable);
 87  
             }
 88  
 
 89  0
             f.setAccessible(false);
 90  
         }
 91  0
     }
 92  
 }