86ca59cffee7b2b705d40265d25c699e16886929
[so.git] /
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.vnfmadapter.packagemanagement.subscriptionmanagement;
21
22 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
23 import java.nio.charset.StandardCharsets;
24 import org.apache.commons.codec.binary.Base64;
25 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
26 import org.onap.so.configuration.rest.HttpHeadersProvider;
27 import org.onap.so.rest.service.HttpRestServiceProvider;
28 import org.onap.so.rest.service.HttpRestServiceProviderImpl;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Qualifier;
31 import org.springframework.web.client.RestTemplate;
32
33 /**
34  * A base class that can be extended by classes for providing notification services. Provides common methods that will
35  * be useful to those classes.
36  *
37  * @author Waqas Ikram (waqas.ikram@est.tech)
38  * @author Andrew Lamb (andrew.a.lamb@est.tech)
39  */
40 public abstract class AbstractNotificationServiceProvider {
41
42     @Autowired
43     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
44     private RestTemplate restTemplate;
45
46     protected HttpRestServiceProvider getHttpRestServiceProvider(final HttpHeadersProvider httpHeadersProvider) {
47         final HttpRestServiceProvider httpRestServiceProvider =
48                 new HttpRestServiceProviderImpl(restTemplate, httpHeadersProvider);
49         return httpRestServiceProvider;
50     }
51
52     protected BasicHttpHeadersProvider getBasicHttpHeadersProviderWithBasicAuth(final String username,
53             final String password) {
54         final byte[] encodedAuth = getBasicAuth(username, password);
55         final String authHeader = "Basic " + new String(encodedAuth);
56         return new BasicHttpHeadersProvider(authHeader);
57     }
58
59     protected byte[] getBasicAuth(final String username, final String password) {
60         final String auth = username + ":" + password;
61         return Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
62     }
63
64 }