Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / PombaRestInterface.java
1 package org.onap.vid.aai;
2
3 import org.onap.vid.aai.util.AAIRestInterface;
4 import org.onap.vid.aai.util.HttpClientMode;
5 import org.onap.vid.aai.util.HttpsAuthClient;
6 import org.onap.vid.utils.Logging;
7 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
8 import org.springframework.http.HttpMethod;
9
10 import javax.ws.rs.client.Client;
11 import javax.ws.rs.client.Entity;
12 import javax.ws.rs.core.MediaType;
13 import javax.ws.rs.core.Response;
14 import java.util.Date;
15 import java.util.UUID;
16
17 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
18
19 public class PombaRestInterface extends AAIRestInterface {
20
21     public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory) {
22         super(httpsAuthClientFactory);
23     }
24
25     private Client client = null;
26
27     private void initRestClient()
28     {
29         if (client == null) {
30             try {
31                 client = httpsAuthClientFactory.getClient(HttpClientMode.UNSECURE);
32             }
33             catch (Exception e) {
34                 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== Exception in REST call to DB in initRestClient" + e.toString());
35                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== Exception in REST call to DB : " + e.toString());
36             }
37         }
38     }
39
40
41     public Response RestPost(String fromAppId, String url, String payload) {
42         String methodName = "RestPost";
43         String transId = UUID.randomUUID().toString();
44         try {
45             String responseType = MediaType.APPLICATION_JSON;
46             initRestClient();
47
48             Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
49             final Response cres = client.target(url)
50                     .request()
51                     .accept(responseType)
52                     .header(TRANSACTION_ID_HEADER, transId)
53                     .header(FROM_APP_ID_HEADER,  fromAppId)
54                     .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
55                     .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
56             Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
57
58             if (cres.getStatus() == 200 && cres.getStatus() <= 299) {
59                 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLARATION);
60                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLARATION);
61             } else {
62                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " with status="+cres.getStatus()+ URL_DECLARATION +url);
63             }
64             return cres;
65         } catch (Exception e) {
66             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLARATION +url+ ", Exception: " + e.toString());
67         }
68         return null;
69     }
70
71 }
72
73