BUG-2511 Fix XXE vulnerability in Netconf
[controller.git] / opendaylight / adsal / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / WrapperTypeInfo.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.northbound.commons.query;
9
10 import java.lang.reflect.AccessibleObject;
11 import java.lang.reflect.Field;
12 import java.lang.reflect.Method;
13
14 import javax.xml.bind.annotation.XmlElement;
15
16 public class WrapperTypeInfo extends TypeInfo {
17
18     protected WrapperTypeInfo(String name, Accessor accessor) {
19         super(name, accessor.getType(), accessor);
20     }
21
22     @Override
23     public Object retrieve(Object target, String[] query, int index) throws QueryException {
24         if (LOGGER.isDebugEnabled()) {
25             LOGGER.debug("retrieve collection: {}/{}", index, query.length);
26         }
27         if (index >= query.length) return null;
28         explore();
29         TypeInfo child = getChild(query[index]);
30         if (child == null) return null;
31         if (query.length == index+1) { // skipping this node
32             return target;
33         }else { // if list of list go to next node to get value
34             return child.retrieve(target, query, index+1);
35         }
36     }
37
38     @Override
39     public synchronized void explore() {
40         if (_explored) return;
41         if (LOGGER.isDebugEnabled()) {
42             LOGGER.debug("exploring wrapper type: {} gtype: {}", _class,
43                     _accessor.getGenericType());
44         }
45         String tn = null;
46         AccessibleObject accessibleObject = _accessor.getAccessibleObject();
47         XmlElement xmlElement = accessibleObject.getAnnotation(XmlElement.class);
48         if (accessibleObject instanceof Field) {
49             Field f = (Field) accessibleObject;
50             tn = DEFAULT_NAME.equals(xmlElement.name())?f.getName() : xmlElement.name();
51         }else if (accessibleObject instanceof Method) {
52             Method m = (Method) accessibleObject;
53             tn = DEFAULT_NAME.equals(xmlElement.name())?m.getName() : xmlElement.name();
54         }
55         this._types.put(tn, new IteratableTypeInfo(tn, this._accessor));
56         _explored = true;
57     }
58
59 }