Merge "BUG-2511 Fix XXE vulnerability in Netconf"
[controller.git] / opendaylight / adsal / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / IteratableTypeInfo.java
1 package org.opendaylight.controller.northbound.commons.query;
2
3 import java.lang.reflect.ParameterizedType;
4 import java.lang.reflect.Type;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.List;
9
10 /**
11  */
12 /*package*/ class IteratableTypeInfo extends TypeInfo {
13
14     public IteratableTypeInfo(String name, Accessor accessor) {
15         super(name, accessor.getType(), accessor);
16     }
17
18     @Override
19     public Object retrieve(Object target, String[] query, int index) throws QueryException {
20         if (LOGGER.isDebugEnabled()) {
21             LOGGER.debug("retrieve collection: {}/{} type:{}", index, query.length,
22                     target.getClass());
23         }
24         explore();
25         Collection<?> c = (Collection<?>) target;
26         Iterator<?> it = c.iterator();
27         List<Object> objects = new ArrayList<Object>();
28         while (it.hasNext()) {
29             Object item = it.next();
30             for (TypeInfo child : _types.values()) {
31                 Object val = child.retrieve(item, query, index);
32                 if (val != null) {
33                     objects.add(val);
34                 }
35             }
36         }
37         return objects;
38
39     }
40
41     @Override
42     public synchronized void explore() {
43         if (_explored) {
44             return;
45         }
46         if (LOGGER.isDebugEnabled()) {
47             LOGGER.debug("exploring iteratable type: {} gtype: {}", _class,
48                     _accessor.getGenericType());
49         }
50         Type t = _accessor.getGenericType();
51         if (t instanceof ParameterizedType) {
52             Type[] pt = ((ParameterizedType) t).getActualTypeArguments();
53             // First type is a child, ignore rest
54             if (pt.length > 0) {
55                 _types.put(_name, new TypeInfo(_name, (Class)pt[0], null));
56             }
57         }
58         _explored = true;
59     }
60
61     @Override
62     public TypeInfo getCollectionChild(Class<?> childType) {
63         explore();
64         for (TypeInfo ti : _types.values()) {
65             if (ti.getType().equals(childType)) {
66                 return ti;
67             }
68         }
69         return null;
70     }
71 }