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