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