Disable external entities reference
[ccsdk/sli/plugins.git] / properties-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / prop / XmlParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. *
6  * Modifications Copyright © 2018 IBM.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.prop;
23
24 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.xml.sax.Attributes;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.helpers.DefaultHandler;
30
31 import javax.xml.XMLConstants;
32 import javax.xml.parsers.ParserConfigurationException;
33 import javax.xml.parsers.SAXParser;
34 import javax.xml.parsers.SAXParserFactory;
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Map;
41 import java.util.Set;
42
43 import static com.google.common.base.Preconditions.checkNotNull;
44
45 public final class XmlParser {
46
47     private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
48
49     private XmlParser() {
50         // Preventing instantiation of the same.
51     }
52
53     public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
54         throws SvcLogicException {
55
56         checkNotNull(s, "Input should not be null.");
57
58         Handler handler = new Handler(listNameList);
59         try {
60             SAXParserFactory factory = SAXParserFactory.newInstance();
61
62             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
63             factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
64             factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
65             SAXParser saxParser = factory.newSAXParser();
66             InputStream in = new ByteArrayInputStream(s.getBytes());
67             saxParser.parse(in, handler);
68         } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
69             throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
70         }
71         return handler.getProperties();
72     }
73
74     private static class Handler extends DefaultHandler {
75
76         private Set<String> listNameList;
77
78         private Map<String, String> properties = new HashMap<>();
79
80         StringBuilder currentName = new StringBuilder();
81         StringBuilder currentValue = new StringBuilder();
82
83         public Handler(Set<String> listNameList) {
84             super();
85             this.listNameList = listNameList;
86             if (this.listNameList == null)
87                 this.listNameList = new HashSet<>();
88         }
89
90         public Map<String, String> getProperties() {
91             return properties;
92         }
93
94         @Override
95         public void startElement(String uri, String localName, String qName, Attributes attributes)
96                 throws SAXException {
97             super.startElement(uri, localName, qName, attributes);
98
99             String name = localName;
100             if (name == null || name.trim().length() == 0)
101                 name = qName;
102             int i2 = name.indexOf(':');
103             if (i2 >= 0)
104                 name = name.substring(i2 + 1);
105
106             if (currentName.length() > 0)
107                 currentName.append(Character.toString('.'));
108             currentName.append(name);
109
110             String listName = removeIndexes(currentName.toString());
111
112             if (listNameList.contains(listName)) {
113                 String n = currentName.toString() + "_length";
114                 int len = getInt(properties, n);
115                 properties.put(n, String.valueOf(len + 1));
116                 currentName.append("[").append(len).append("]");
117             }
118         }
119
120         @Override
121         public void endElement(String uri, String localName, String qName) throws SAXException {
122             super.endElement(uri, localName, qName);
123
124             String name = localName;
125             if (name == null || name.trim().length() == 0)
126                 name = qName;
127             int i2 = name.indexOf(':');
128             if (i2 >= 0)
129                 name = name.substring(i2 + 1);
130
131             String s = currentValue.toString().trim();
132             if (s.length() > 0) {
133                 properties.put(currentName.toString(), s);
134
135                 log.info("Added property: {} : {}", currentName, s);
136                 currentValue = new StringBuilder();
137             }
138
139             int i1 = currentName.lastIndexOf("." + name);
140             if (i1 <= 0)
141                 currentName = new StringBuilder();
142             else
143                 currentName = new StringBuilder(currentName.substring(0, i1));
144         }
145
146         @Override
147         public void characters(char[] ch, int start, int length) throws SAXException {
148             super.characters(ch, start, length);
149
150             String value = new String(ch, start, length);
151             currentValue.append(value);
152         }
153
154         private static int getInt(Map<String, String> mm, String name) {
155             String s = mm.get(name);
156             if (s == null)
157                 return 0;
158             return Integer.parseInt(s);
159         }
160
161         private String removeIndexes(String currentName) {
162             StringBuilder b = new StringBuilder();
163             boolean add = true;
164             for (int i = 0; i < currentName.length(); i++) {
165                 char c = currentName.charAt(i);
166                 if (c == '[')
167                     add = false;
168                 else if (c == ']')
169                     add = true;
170                 else if (add)
171                     b.append(Character.toString(c));
172             }
173             return b.toString();
174         }
175     }
176 }