| 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 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
@SuppressWarnings("unused") |
| 23 | |
public class ImmutableCollectionsInInterceptor extends AbstractPhaseInterceptor<Message> { |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
} |