Removed MsoLogger class
[so.git] / common / src / main / java / org / onap / so / utils / XmlMarshaller.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.utils;
24
25
26 import java.io.StringReader;
27 import java.io.StringWriter;
28
29 import javax.xml.XMLConstants;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.parsers.SAXParserFactory;
35 import javax.xml.transform.sax.SAXSource;
36
37 import org.onap.so.exceptions.MarshallerException;
38 import org.onap.so.logger.ErrorCode;
39 import org.onap.so.logger.MessageEnum;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.InputSource;
43 import org.xml.sax.XMLReader;
44
45 public class XmlMarshaller {
46
47     private static Logger logger = LoggerFactory.getLogger(XmlMarshaller.class);
48
49     public static String marshal(Object object) throws MarshallerException {
50
51         StringWriter stringWriter = new StringWriter();
52         try {
53             JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
54             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
55             jaxbMarshaller.marshal(object, stringWriter);
56         } catch (JAXBException e) {
57             logger.error("{} {} {}", MessageEnum.GENERAL_EXCEPTION.toString(),
58                 ErrorCode.SchemaError.getValue(), e.getMessage(), e);
59             throw new MarshallerException(e.getMessage(), ErrorCode.SchemaError.getValue(), e);
60         }
61
62         return stringWriter.toString();
63     }
64
65     public static Object unMarshal(String input, Object object) throws MarshallerException {
66
67         try {
68             SAXParserFactory spf = SAXParserFactory.newInstance();
69             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
70             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
71             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
72             spf.setNamespaceAware(true);
73             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
74
75             JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
76             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
77
78             InputSource inputSource = new InputSource(new StringReader(input));
79             SAXSource source = new SAXSource(xmlReader, inputSource);
80             object = jaxbUnmarshaller.unmarshal(source, object.getClass()).getValue();
81         } catch (Exception e) {
82             logger.error("{} {} {}", MessageEnum.GENERAL_EXCEPTION.toString(),
83                 ErrorCode.SchemaError.getValue(), e.getMessage(), e);
84             throw new MarshallerException(e.getMessage(), ErrorCode.SchemaError.getValue(), e);
85         }
86
87         return object;
88     }
89
90 }