972d5eb6fa731cac2a3560c9e51cf97636d64db6
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / tenantrest / TenantRequestCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.adapters.tenantrest;
22
23
24 import java.io.ByteArrayOutputStream;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.Marshaller;
28
29 import org.codehaus.jackson.map.ObjectMapper;
30 import org.codehaus.jackson.map.SerializationConfig;
31
32 public class TenantRequestCommon {
33         public String toJsonString() {
34                 try {
35                         String jsonString = null;
36                         ObjectMapper mapper = new ObjectMapper();
37                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
38                         jsonString = mapper.writeValueAsString(this);
39                         return jsonString;
40                 } catch (Exception e) {
41                         // Shouldn't happen...
42                         e.printStackTrace();
43                         return "";
44                 }
45         }
46
47         public String toXmlString() {
48                 try {
49                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
50                         JAXBContext context = JAXBContext.newInstance(this.getClass());
51                         Marshaller marshaller = context.createMarshaller();
52                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
53                         marshaller.marshal(this, bs);
54                         return bs.toString();
55                 } catch (Exception e) {
56                         // Shouldn't happen...
57                         e.printStackTrace();
58                         return "";
59                 }
60         }
61 }