Replaced all tabs with spaces in java and pom.xml
[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 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.Marshaller;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
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 Logger logger = LoggerFactory.getLogger(CatalogQueryExceptionCommon.class);
37
38     public CatalogQueryExceptionCommon() {
39         messageId = null;
40     }
41
42     public CatalogQueryExceptionCommon(String messageId) {
43         this.messageId = messageId;
44     }
45
46     public String getMessageId() {
47         return messageId;
48     }
49
50     public void setMessageId(String messageId) {
51         this.messageId = messageId;
52     }
53
54     public String toJsonString() {
55         try {
56             String jsonString;
57             ObjectMapper mapper = new ObjectMapper();
58             mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
59             jsonString = mapper.writeValueAsString(this);
60             return jsonString;
61         } catch (Exception e) {
62             logger.error("Exception:", e);
63             return "";
64         }
65     }
66
67     public String toXmlString() {
68         try {
69             ByteArrayOutputStream bs = new ByteArrayOutputStream();
70             JAXBContext context = JAXBContext.newInstance(this.getClass());
71             Marshaller marshaller = context.createMarshaller();
72             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // pretty print XML
73             marshaller.marshal(this, bs);
74             return bs.toString();
75         } catch (Exception e) {
76             logger.error("Exception:", e);
77             return "";
78         }
79     }
80
81     @Override
82     public String toString() {
83         return toJsonString();
84     }
85 }