2b08d1130f6f7ae17fcda2d3e2aa8220f9b8c04b
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import javax.xml.XMLConstants;
29 import javax.xml.transform.Source;
30 import javax.xml.transform.stream.StreamSource;
31 import javax.xml.validation.Schema;
32 import javax.xml.validation.SchemaFactory;
33 import javax.xml.validation.Validator;
34 import org.apache.commons.io.IOUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.xml.sax.SAXException;
38
39 public class XMLValidator {
40
41     private static String XSDS_PATH;
42
43     static {
44         String prefixMsoPropertiesPath = System.getProperty("mso.config.path");
45         if (prefixMsoPropertiesPath == null) {
46             prefixMsoPropertiesPath = "";
47         }
48         XSDS_PATH = prefixMsoPropertiesPath + "xsds/";
49     }
50
51     private String stringXsd;
52     private String errorMsg = null;
53     private SchemaFactory factory;
54     private Schema schema;
55
56     private static Logger logger = LoggerFactory.getLogger(XMLValidator.class);
57
58
59     public XMLValidator(String xsdFile) {
60
61         try (FileInputStream xsdStream = new FileInputStream(XSDS_PATH + xsdFile)) {
62
63             stringXsd = IOUtils.toString(xsdStream);
64
65             factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
66             factory.setResourceResolver(new PathResourceResolver(XSDS_PATH));
67             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
68
69             String quotedXsd = stringXsd.replaceAll(""", "\"");
70             Source src = new StreamSource(new java.io.StringReader(quotedXsd));
71             schema = factory.newSchema(src);
72
73         } catch (IOException | SAXException e) {
74
75             logger.debug("Cannot open file {}", XSDS_PATH + xsdFile, e);
76             errorMsg = "ErrorDetails: xsd file " + xsdFile + "could not be opened - " + e.getMessage();
77         }
78     }
79
80     // Returns null when XML valid, otherwise returns error details.
81     public String isXmlValid(String stringXml) {
82         try {
83             if (errorMsg != null && !errorMsg.isEmpty()) {
84                 return errorMsg;
85             }
86             Source src2 = new StreamSource(new java.io.StringReader(stringXml));
87             Validator validator = schema.newValidator();
88             validator.validate(src2);
89
90         } catch (IOException | SAXException e) {
91             logger.debug("Exception: ", e);
92             return "ErrorDetails: " + e.getMessage();
93
94         } catch (Exception e) {
95             logger.error("APIH cannot read schema file", e);
96
97             return "ErrorDetails: " + "Unable to read the schema file";
98         }
99
100         return null;
101     }
102 }