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