fbaf12b337c25a69984f09659929dbad493a862a
[so.git] /
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 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.SerializationFeature;
34
35 public abstract class CatalogQueryExceptionCommon {
36         private String messageId;
37         protected static Logger logger = LoggerFactory.getLogger(CatalogQueryExceptionCommon.class);
38
39         public CatalogQueryExceptionCommon() { messageId = null; }
40         public CatalogQueryExceptionCommon(String messageId) { this.messageId = messageId; }
41
42         public String getMessageId() { return messageId; }
43         public void setMessageId(String messageId) { this.messageId = messageId; }
44
45         public String toJsonString() {
46                 try {
47                         String jsonString;
48                         ObjectMapper mapper = new ObjectMapper();
49                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
50                         jsonString = mapper.writeValueAsString(this);
51                         return jsonString;
52                 } catch (Exception e) {
53                     logger.error ("Exception:", e);
54                         return "";
55                 }
56         }
57
58         public String toXmlString() {
59                 try {
60                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
61                         JAXBContext context = JAXBContext.newInstance(this.getClass());
62                         Marshaller marshaller = context.createMarshaller();
63                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
64                         marshaller.marshal(this, bs);
65                         return bs.toString();
66                 } catch (Exception e) {
67                     logger.error ("Exception:", e);
68                         return "";
69                 }
70         }
71         
72         @Override 
73         public String toString(){
74                 return toJsonString();          
75         }
76 }