make Logging a service and inject it to SyncRestClient
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / PombaRestInterface.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved.
7  * Modifications Copyright (C) 2019 IBM.
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.vid.aai;
24
25 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
26
27 import java.util.UUID;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.onap.vid.aai.util.AAIRestInterface;
34 import org.onap.vid.aai.util.HttpClientMode;
35 import org.onap.vid.aai.util.HttpsAuthClient;
36 import org.onap.vid.aai.util.ServletRequestHelper;
37 import org.onap.vid.aai.util.SystemPropertyHelper;
38 import org.onap.vid.utils.Logging;
39 import org.springframework.http.HttpMethod;
40
41 public class PombaRestInterface extends AAIRestInterface {
42         
43     private Client client = null;
44
45     public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory,
46         ServletRequestHelper servletRequestHelper,
47         SystemPropertyHelper systemPropertyHelper,
48         Logging loggingService) {
49         super(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService);
50     }
51
52     @Override
53     protected void initRestClient()
54     {
55         if (client == null) {
56             try {
57                 client = httpsAuthClientFactory.getClient(HttpClientMode.WITH_KEYSTORE);
58             }
59             catch (Exception e) {
60                 logger.info(EELFLoggerDelegate.errorLogger, "Exception in REST call to DB in initRestClient", e);
61                 logger.debug(EELFLoggerDelegate.debugLogger, "Exception in REST call to DB : ", e);
62             }
63         }
64     }
65
66
67     public Response RestPost(String fromAppId, String url, String payload) {
68         String methodName = "RestPost";
69         String transId = UUID.randomUUID().toString();
70         try {
71             initRestClient();
72
73             loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
74             final Response cres = client.target(url)
75                     .request()
76                     .accept(MediaType.APPLICATION_JSON)
77                     .header(TRANSACTION_ID_HEADER, transId)
78                     .header(FROM_APP_ID_HEADER,  fromAppId)
79                     .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
80                     .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
81             loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
82
83             if (cres.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
84                 logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
85                 logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
86             } else {
87                 logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, cres));
88             }
89             return cres;
90         } catch (Exception e) {
91             logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
92         }
93         return null;
94     }
95
96 }
97
98