09a469d89359ca5b54e5617b7dc76da81b902bc5
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / openecomp / mso / adapters / catalogdb / catalogrest / CatalogQueryExceptionCommon.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 package org.openecomp.mso.adapters.catalogdb.catalogrest;
21
22 import org.codehaus.jackson.map.ObjectMapper;
23 import org.codehaus.jackson.map.SerializationConfig;
24
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.Marshaller;
27 import java.io.ByteArrayOutputStream;
28
29 public abstract class CatalogQueryExceptionCommon {
30         private String messageId;
31
32         public CatalogQueryExceptionCommon() { messageId = null; }
33         public CatalogQueryExceptionCommon(String messageId) { this.messageId = messageId; }
34
35         public String getMessageId() { return messageId; }
36         public void setMessageId(String messageId) { this.messageId = messageId; }
37
38         public String toJsonString() {
39                 try {
40                         String jsonString = null;
41                         ObjectMapper mapper = new ObjectMapper();
42                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
43                         jsonString = mapper.writeValueAsString(this);
44                         return jsonString;
45                 } catch (Exception e) {
46                         e.printStackTrace();
47                         return "";
48                 }
49         }
50
51         public String toXmlString() {
52                 try {
53                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
54                         JAXBContext context = JAXBContext.newInstance(this.getClass());
55                         Marshaller marshaller = context.createMarshaller();
56                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
57                         marshaller.marshal(this, bs);
58                         return bs.toString();
59                 } catch (Exception e) {
60                         e.printStackTrace();
61                         return "";
62                 }
63         }
64 }