778a329979fcf617164999e8394c298ce77d1b15
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / 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.openecomp.mso.adapters.tenantrest;
23
24
25 import java.io.ByteArrayOutputStream;
26
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.Marshaller;
29
30 import org.codehaus.jackson.map.ObjectMapper;
31 import org.codehaus.jackson.map.SerializationConfig;
32
33 import org.openecomp.mso.logger.MsoLogger;
34
35 public class TenantRequestCommon {
36         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
37         public String toJsonString() {
38                 try {
39                         String jsonString;
40                         ObjectMapper mapper = new ObjectMapper();
41                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
42                         jsonString = mapper.writeValueAsString(this);
43                         return jsonString;
44                 } catch (Exception e) {
45                         LOGGER.debug("Exception :",e);
46                         return "";
47                 }
48         }
49
50         public String toXmlString() {
51                 try {
52                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
53                         JAXBContext context = JAXBContext.newInstance(this.getClass());
54                         Marshaller marshaller = context.createMarshaller();
55                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
56                         marshaller.marshal(this, bs);
57                         return bs.toString();
58                 } catch (Exception e) {
59                         LOGGER.debug("Exception :",e);
60                         return "";
61                 }
62         }
63 }