5e4e0155562549c01e7a1279fe7ba73b47e13676
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / nwrest / NetworkRequestCommon.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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.nwrest;
24
25
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.Serializable;
29
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.Marshaller;
32
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.annotation.JsonProperty;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Everything that is common between all Network Requests.
42  */
43 public abstract class NetworkRequestCommon implements Serializable {
44         private static final long serialVersionUID = -6732431343649282079L;
45         private static final Logger logger =  LoggerFactory.getLogger(NetworkRequestCommon.class);
46         private Boolean skipAAI = false;
47         private String messageId;
48         private String notificationUrl;
49         @JsonProperty
50         private boolean synchronous = true;
51         public Boolean getSkipAAI() {
52                 return skipAAI;
53         }
54
55         public void setSkipAAI(Boolean skipAAI) {
56                 this.skipAAI = skipAAI;
57         }
58
59         public String getMessageId() {
60                 return messageId;
61         }
62
63         public void setMessageId(String messageId) {
64                 this.messageId = messageId;
65         }
66
67         public String getNotificationUrl() {
68                 return notificationUrl;
69         }
70
71         public void setNotificationUrl(String notificationUrl) {
72                 this.notificationUrl = notificationUrl;
73                 this.synchronous = notificationUrl == null || (notificationUrl.isEmpty());
74         }
75
76         public boolean isSynchronous() {
77                 return this.synchronous; 
78         }
79         
80         @JsonIgnore
81         protected void setSynchronous(boolean synchronous) {
82                 this.synchronous = synchronous;
83         }
84
85         public String toJsonString() {
86                 String jsonString = null;
87                 try {
88                         ObjectMapper mapper = new ObjectMapper();
89                         mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
90                         jsonString = mapper.writeValueAsString(this);
91                 } catch (Exception e) {
92                     logger.debug("Exception:", e);
93                 }
94                 return jsonString;
95         }
96
97         public String toXmlString() {
98                 try {
99                         ByteArrayOutputStream bs = new ByteArrayOutputStream();
100                         JAXBContext context = JAXBContext.newInstance(this.getClass());
101                         Marshaller marshaller = context.createMarshaller();
102                         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //pretty print XML
103                         marshaller.marshal(this, bs);
104                         return bs.toString();
105                 } catch (Exception e) {
106                     logger.debug("Exception:", e);
107                         return "";
108                 }
109         }
110 }