Containerization feature of SO
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / onap / so / adapters / catalogdb / catalogrest / CatalogQueryExceptionCommon.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.catalogdb.catalogrest;
23
24 import java.io.ByteArrayOutputStream;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.Marshaller;
28
29 import org.onap.so.logger.MsoLogger;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33
34 public abstract class CatalogQueryExceptionCommon {
35         private String messageId;
36         protected static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA,CatalogQueryExceptionCommon.class);
37
38         public CatalogQueryExceptionCommon() { messageId = null; }
39         public CatalogQueryExceptionCommon(String messageId) { this.messageId = messageId; }
40
41         public String getMessageId() { return messageId; }
42         public void setMessageId(String messageId) { this.messageId = messageId; }
43
44         public String toJsonString() {
45                 try {
46                         String jsonString;
47                         ObjectMapper mapper = new ObjectMapper();
48                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
49                         jsonString = mapper.writeValueAsString(this);
50                         return jsonString;
51                 } catch (Exception e) {
52                     LOGGER.debug ("Exception:", e);
53                         return "";
54                 }
55         }
56
57         public String toXmlString() {
58                 try {
59                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
60                         JAXBContext context = JAXBContext.newInstance(this.getClass());
61                         Marshaller marshaller = context.createMarshaller();
62                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
63                         marshaller.marshal(this, bs);
64                         return bs.toString();
65                 } catch (Exception e) {
66                     LOGGER.debug ("Exception:", e);
67                         return "";
68                 }
69         }
70         
71         @Override 
72         public String toString(){
73                 return toJsonString();          
74         }
75 }