283fef1d3bb27b6119d687157e5b20be132599e3
[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  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.catalogdb.catalogrest;
25
26 import java.io.ByteArrayOutputStream;
27
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.SerializationFeature;
35
36 public abstract class CatalogQueryExceptionCommon {
37         private String messageId;
38         protected static Logger logger = LoggerFactory.getLogger(CatalogQueryExceptionCommon.class);
39
40         public CatalogQueryExceptionCommon() { messageId = null; }
41         public CatalogQueryExceptionCommon(String messageId) { this.messageId = messageId; }
42
43         public String getMessageId() { return messageId; }
44         public void setMessageId(String messageId) { this.messageId = messageId; }
45
46         public String toJsonString() {
47                 try {
48                         String jsonString;
49                         ObjectMapper mapper = new ObjectMapper();
50                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
51                         jsonString = mapper.writeValueAsString(this);
52                         return jsonString;
53                 } catch (Exception e) {
54                     logger.error ("Exception:", e);
55                         return "";
56                 }
57         }
58
59         public String toXmlString() {
60                 try {
61                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
62                         JAXBContext context = JAXBContext.newInstance(this.getClass());
63                         Marshaller marshaller = context.createMarshaller();
64                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
65                         marshaller.marshal(this, bs);
66                         return bs.toString();
67                 } catch (Exception e) {
68                     logger.error ("Exception:", e);
69                         return "";
70                 }
71         }
72         
73         @Override 
74         public String toString(){
75                 return toJsonString();          
76         }
77 }