b9a06ac1d9a52040bc3eb450f7888e34e3a0b47f
[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 com.google.common.base.Strings;
35 import org.apache.commons.io.IOUtils;
36 import org.onap.so.logger.ErrorCode;
37 import org.onap.so.logger.MessageEnum;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.xml.sax.SAXException;
41
42 public class XMLValidator {
43
44     private static String XSDS_PATH;
45
46     static {
47         String prefixMsoPropertiesPath = System.getProperty("mso.config.path");
48         if (prefixMsoPropertiesPath == null) {
49             prefixMsoPropertiesPath = "";
50         }
51         XSDS_PATH = prefixMsoPropertiesPath + "xsds/";
52     }
53
54     private String stringXsd;
55     private String errorMsg = null;
56     private SchemaFactory factory;
57     private Schema schema;
58
59     private static Logger logger = LoggerFactory.getLogger(XMLValidator.class);
60
61
62     public XMLValidator(String xsdFile) {
63
64         try (FileInputStream xsdStream = new FileInputStream(XSDS_PATH + xsdFile)) {
65
66             stringXsd = IOUtils.toString(xsdStream);
67
68             factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
69             factory.setResourceResolver(new PathResourceResolver(XSDS_PATH));
70             factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
71
72             String quotedXsd = stringXsd.replaceAll(""", "\"");
73             Source src = new StreamSource(new java.io.StringReader(quotedXsd));
74             schema = factory.newSchema(src);
75
76         } catch (IOException | SAXException e) {
77
78             logger.debug("Cannot open file {}", XSDS_PATH + xsdFile, e);
79             errorMsg = "ErrorDetails: xsd file " + xsdFile + "could not be opened - " + e.getMessage();
80         }
81     }
82
83     // Returns null when XML valid, otherwise returns error details.
84     public String isXmlValid(String stringXml) {
85         try {
86             if (errorMsg != null && !errorMsg.isEmpty()) {
87                 return errorMsg;
88             }
89             Source src2 = new StreamSource(new java.io.StringReader(stringXml));
90             Validator validator = schema.newValidator();
91             validator.validate(src2);
92
93         } catch (IOException | SAXException e) {
94             logger.debug("Exception: ", e);
95             return "ErrorDetails: " + e.getMessage();
96
97         } catch (Exception e) {
98             logger.error(Strings.repeat("{} ", 3), MessageEnum.APIH_CANNOT_READ_SCHEMA.toString(),
99                     ErrorCode.SchemaError.getValue(), "APIH cannot read schema file", e);
100
101             return "ErrorDetails: " + "Unable to read the schema file";
102         }
103
104         return null;
105     }
106 }