Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / namingservice / NamingClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
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.so.client.namingservice;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.onap.namingservice.model.NameGenDeleteRequest;
29 import org.onap.namingservice.model.NameGenDeleteResponse;
30 import org.onap.namingservice.model.NameGenRequest;
31 import org.onap.namingservice.model.NameGenResponse;
32 import org.onap.so.client.exception.BadResponseException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.core.env.Environment;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Component;
43 import org.springframework.web.client.HttpStatusCodeException;
44 import org.springframework.web.client.RestTemplate;
45
46
47
48 @Component
49 public class NamingClient {
50     private static final Logger logger = LoggerFactory.getLogger(NamingClient.class);
51     private static final String ENDPOINT = "mso.naming.endpoint";
52     private static final String AUTH = "mso.naming.auth";
53
54     @Autowired
55     private RestTemplate restTemplate;
56     @Autowired
57     private Environment env;
58     @Autowired
59     private NamingClientResponseValidator namingClientResponseValidator;
60
61     public String postNameGenRequest(NameGenRequest request) throws BadResponseException, IOException {
62         String targetUrl = env.getProperty(ENDPOINT);
63         HttpHeaders headers = setHeaders(env.getProperty(AUTH));
64         logger.info("Sending postNameGenRequest to url: {}", targetUrl);
65         HttpEntity<NameGenRequest> requestEntity = new HttpEntity<>(request, headers);
66         ResponseEntity<NameGenResponse> response;
67         try {
68             response = restTemplate.postForEntity(targetUrl, requestEntity, NameGenResponse.class);
69         } catch (HttpStatusCodeException e) {
70             throw new BadResponseException(namingClientResponseValidator.formatError(e));
71         }
72         return namingClientResponseValidator.validateNameGenResponse(response);
73     }
74
75     public String deleteNameGenRequest(NameGenDeleteRequest request) throws BadResponseException, IOException {
76         String targetUrl = env.getProperty(ENDPOINT);
77         HttpHeaders headers = setHeaders(env.getProperty(AUTH));
78         logger.info("Sending deleteNameGenRequest to url: {}", targetUrl);
79         HttpEntity<NameGenDeleteRequest> requestEntity = new HttpEntity<>(request, headers);
80         ResponseEntity<NameGenDeleteResponse> response;
81         try {
82             response = restTemplate.exchange(targetUrl, HttpMethod.DELETE, requestEntity, NameGenDeleteResponse.class);
83         } catch (HttpStatusCodeException e) {
84             throw new BadResponseException(namingClientResponseValidator.formatError(e));
85         }
86         return namingClientResponseValidator.validateNameGenDeleteResponse(response);
87     }
88
89     private HttpHeaders setHeaders(String auth) {
90         HttpHeaders headers = new HttpHeaders();
91         headers.setContentType(MediaType.APPLICATION_JSON);
92         List<MediaType> acceptableMediaTypes = new ArrayList<>();
93         acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
94         headers.setAccept(acceptableMediaTypes);
95         headers.add(HttpHeaders.AUTHORIZATION, auth);
96         return headers;
97     }
98 }