Containerization feature of SO
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / tenantrest / TenantRequestCommon.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  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.adapters.tenantrest;
23
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.Serializable;
27
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30
31 import org.onap.so.logger.MsoLogger;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.SerializationFeature;
35
36 public abstract class TenantRequestCommon implements Serializable {
37
38         private static final long serialVersionUID = 1486834308868170854L;
39         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, TenantRequestCommon.class);
40         public String toJsonString() {
41                 try {
42                         String jsonString;
43                         ObjectMapper mapper = new ObjectMapper();
44                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
45                         jsonString = mapper.writeValueAsString(this);
46                         return jsonString;
47                 } catch (Exception e) {
48                         LOGGER.debug("Exception :",e);
49                         return "";
50                 }
51         }
52
53         public String toXmlString() {
54                 try {
55                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
56                         JAXBContext context = JAXBContext.newInstance(this.getClass());
57                         Marshaller marshaller = context.createMarshaller();
58                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
59                         marshaller.marshal(this, bs);
60                         return bs.toString();
61                 } catch (Exception e) {
62                         LOGGER.debug("Exception :",e);
63                         return "";
64                 }
65         }
66 }