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