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