always convert values toString for xml marshalling
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / vnfrest / VfResponseCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
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.adapters.vnfrest;
24
25
26 import java.io.ByteArrayOutputStream;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.Marshaller;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Everything that is common between all VfModule and VolumeGroup Responses, except for QueryVfModuleResponse and
38  * QueryVolumeGroupResponse.
39  */
40 public abstract class VfResponseCommon {
41     private static final Logger logger = LoggerFactory.getLogger(VfResponseCommon.class);
42     private String messageId;
43
44     public VfResponseCommon() {
45         messageId = null;
46     }
47
48     public VfResponseCommon(String messageId) {
49         this.messageId = messageId;
50     }
51
52     public String getMessageId() {
53         return messageId;
54     }
55
56     public void setMessageId(String messageId) {
57         this.messageId = messageId;
58     }
59
60     public String toJsonString() {
61         try {
62             String jsonString;
63             ObjectMapper mapper = new ObjectMapper();
64             mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
65             jsonString = mapper.writeValueAsString(this);
66             return jsonString;
67         } catch (Exception e) {
68             logger.debug("Exception :", e);
69             return "";
70         }
71     }
72
73     public String toXmlString() {
74         try {
75             ByteArrayOutputStream bs = new ByteArrayOutputStream();
76             JAXBContext context = JAXBContext.newInstance(this.getClass(), ArrayList.class, HashMap.class);
77             Marshaller marshaller = context.createMarshaller();
78             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // pretty print XML
79             marshaller.marshal(this, bs);
80             return bs.toString();
81         } catch (Exception e) {
82             logger.debug("Exception :", e);
83             return "";
84         }
85     }
86 }