Change the header to SO
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / nwrest / NetworkRequestCommon.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.adapters.nwrest;
22
23
24
25 import java.io.ByteArrayOutputStream;
26
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.Marshaller;
29
30 import org.codehaus.jackson.map.ObjectMapper;
31 import org.codehaus.jackson.map.SerializationConfig;
32
33 /**
34  * Everything that is common between all Network Requests.
35  */
36 public abstract class NetworkRequestCommon {
37         private Boolean skipAAI = false;
38         private String messageId;
39         private String notificationUrl;
40
41         public Boolean getSkipAAI() {
42                 return skipAAI;
43         }
44
45         public void setSkipAAI(Boolean skipAAI) {
46                 this.skipAAI = skipAAI;
47         }
48
49         public String getMessageId() {
50                 return messageId;
51         }
52
53         public void setMessageId(String messageId) {
54                 this.messageId = messageId;
55         }
56
57         public String getNotificationUrl() {
58                 return notificationUrl;
59         }
60
61         public void setNotificationUrl(String notificationUrl) {
62                 this.notificationUrl = notificationUrl;
63         }
64
65         public boolean isSynchronous() {
66                 return notificationUrl == null || (notificationUrl != null && notificationUrl.isEmpty());
67         }
68
69         public String toJsonString() {
70                 String jsonString = null;
71                 try {
72                         ObjectMapper mapper = new ObjectMapper();
73                         mapper.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE);
74                         jsonString = mapper.writeValueAsString(this);
75                 } catch (Exception e) {
76                         // ignore
77                 }
78                 return jsonString;
79         }
80
81         public String toXmlString() {
82                 try {
83                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
84                         JAXBContext context = JAXBContext.newInstance(this.getClass());
85                         Marshaller marshaller = context.createMarshaller();
86                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
87                         marshaller.marshal(this, bs);
88                         return bs.toString();
89                 } catch (Exception e) {
90                         // Shouldn't happen...
91                         e.printStackTrace();
92                         return "";
93                 }
94         }
95 }