Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.aai;
|
| 24 |
|
| 25 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 26 | import org.onap.osam.aai.util.*;
|
| 27 | import org.onap.osam.utils.Logging;
|
| 28 | import org.springframework.http.HttpMethod;
|
| 29 |
|
| 30 | import javax.ws.rs.client.Client;
|
| 31 | import javax.ws.rs.client.Entity;
|
| 32 | import javax.ws.rs.core.MediaType;
|
| 33 | import javax.ws.rs.core.Response;
|
| 34 | import java.util.UUID;
|
| 35 |
|
| 36 | import static org.onap.osam.utils.Logging.REQUEST_ID_HEADER_KEY;
|
| 37 |
|
| 38 | public class PombaRestInterface extends AAIRestInterface {
|
| 39 |
|
| 40 | public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
|
| 41 | super(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper);
|
| 42 | }
|
| 43 |
|
| 44 | private Client client = null;
|
| 45 |
|
| 46 | private void initRestClient()
|
| 47 | {
|
| 48 | if (client == null) {
|
| 49 | try {
|
| 50 | client = httpsAuthClientFactory.getClient(HttpClientMode.UNSECURE);
|
| 51 | }
|
| 52 | catch (Exception e) {
|
| 53 | logger.info(EELFLoggerDelegate.errorLogger, "Exception in REST call to DB in initRestClient" + e.toString());
|
| 54 | logger.debug(EELFLoggerDelegate.debugLogger, "Exception in REST call to DB : " + e.toString());
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 |
|
| 60 | public Response RestPost(String fromAppId, String url, String payload) {
|
| 61 | String methodName = "RestPost";
|
| 62 | String transId = UUID.randomUUID().toString();
|
| 63 | try {
|
| 64 | initRestClient();
|
| 65 |
|
| 66 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
|
| 67 | final Response cres = client.target(url)
|
| 68 | .request()
|
| 69 | .accept(MediaType.APPLICATION_JSON)
|
| 70 | .header(TRANSACTION_ID_HEADER, transId)
|
| 71 | .header(FROM_APP_ID_HEADER, fromAppId)
|
| 72 | .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
|
| 73 | .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
|
| 74 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
|
| 75 |
|
| 76 | if (cres.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
|
| 77 | logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
|
| 78 | logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
|
| 79 | } else {
|
| 80 | logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, cres));
|
| 81 | }
|
| 82 | } catch (Exception e) {
|
| 83 | logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
|
| 84 | }
|
| 85 | return null;
|
| 86 | }
|
| 87 |
|
| 88 | }
|
| 89 |
|
| 90 |
|