2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package com.onap.sdnc.vnfbackupservice.scheduler;
23 import java.util.Arrays;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.HttpMethod;
29 import org.springframework.http.MediaType;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.client.RestTemplate;
36 public class VnfRestClient {
39 private RestTemplate restTemplate;
41 public String vnfRestClient(String url, String userName, String password) {
42 restTemplate = new RestTemplate();
43 HttpHeaders headers = new HttpHeaders();
44 HttpEntity<String> entity = new HttpEntity<String>(generateHeaders(headers, userName, password));
45 ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
46 String result = response.getBody();
50 public HttpHeaders generateHeaders(HttpHeaders headers, String userName, String password) {
51 headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
52 headers.setContentType(MediaType.APPLICATION_JSON);
53 String base64Username = userName + ":" + password;
54 byte[] message = base64Username.getBytes();
55 headers.set("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString(message));