2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.utils;
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;
43 public class XmlMarshaller {
45 private static Logger logger = LoggerFactory.getLogger(XmlMarshaller.class);
47 public static String marshal(Object object) throws MarshallerException {
49 StringWriter stringWriter = new StringWriter();
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(),
57 throw new MarshallerException(e.getMessage(), ErrorCode.SchemaError.getValue(), e);
60 return stringWriter.toString();
63 public static Object unMarshal(String input, Object object) throws MarshallerException {
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();
73 JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
74 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
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(),
82 throw new MarshallerException(e.getMessage(), ErrorCode.SchemaError.getValue(), e);