Merge "add fluent type builder support to A&AI client"
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / main / java / org / onap / so / adapters / etsi / sol003 / adapter / packagemanagement / subscriptionmanagement / BasicAuthNotificationServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.subscriptionmanagement;
21
22 import static org.slf4j.LoggerFactory.getLogger;
23 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthentication;
24 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.model.SubscriptionsAuthentication.AuthTypeEnum;
25 import org.onap.so.configuration.rest.HttpHeadersProvider;
26 import org.onap.so.rest.service.HttpRestServiceProvider;
27 import org.slf4j.Logger;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Service;
30
31 /**
32  * Implementation of a NotificationServiceProvider which supports Basic Authentication
33  *
34  * @author Waqas Ikram (waqas.ikram@est.tech)
35  * @author Andrew Lamb (andrew.a.lamb@est.tech)
36  */
37 @Service
38 public class BasicAuthNotificationServiceProvider extends AbstractNotificationServiceProvider
39         implements NotificationServiceProvider {
40
41     private static final Logger logger = getLogger(BasicAuthNotificationServiceProvider.class);
42
43     @Override
44     public boolean send(final Object notification, final SubscriptionsAuthentication subscriptionsAuthentication,
45             final String callbackUri) {
46         logger.info("Sending notification to uri: {}", callbackUri);
47         final HttpHeadersProvider httpHeadersProvider =
48                 getBasicHttpHeadersProviderWithBasicAuth(subscriptionsAuthentication.getParamsBasic().getUserName(),
49                         subscriptionsAuthentication.getParamsBasic().getPassword());
50         final HttpRestServiceProvider httpRestServiceProvider = getHttpRestServiceProvider();
51
52         final ResponseEntity<Void> responseEntity = httpRestServiceProvider.postHttpRequest(notification, callbackUri,
53                 httpHeadersProvider.getHttpHeaders(), Void.class);
54         if (responseEntity.getStatusCode().is2xxSuccessful()) {
55             logger.info("Notification sent successfully.");
56             return true;
57         }
58
59         logger.error("Failed to send notification.");
60         return false;
61     }
62
63     @Override
64     public AuthTypeEnum getAuthType() {
65         return AuthTypeEnum.BASIC;
66     }
67
68 }