61869ca322838de8162f9862ee32155537ba25fc
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / db / request / RequestsDbClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 package org.onap.so.client.db.request;
22
23 import java.io.IOException;
24 import java.net.URI;
25
26 import org.onap.so.db.request.beans.InfraActiveRequests;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.http.HttpRequest;
29 import org.springframework.http.client.ClientHttpRequestExecution;
30 import org.springframework.http.client.ClientHttpRequestInterceptor;
31 import org.springframework.http.client.ClientHttpResponse;
32 import org.springframework.stereotype.Component;
33 import org.springframework.web.client.RestTemplate;
34
35 import uk.co.blackpepper.bowman.Client;
36 import uk.co.blackpepper.bowman.ClientFactory;
37 import uk.co.blackpepper.bowman.Configuration;
38 import uk.co.blackpepper.bowman.RestTemplateConfigurer;
39
40 @Component("RequestDbClient")
41 public class RequestsDbClient {
42
43         private Client<InfraActiveRequests> infraActiveRequestClient;
44
45         @Value("${mso.adapters.db.spring.endpoint}")
46         private String endpoint;
47         
48         @Value("${mso.db.auth}")
49         private String msoAdaptersAuth;
50
51         public RequestsDbClient() {
52                 ClientFactory clientFactory = Configuration.builder().setRestTemplateConfigurer(new RestTemplateConfigurer() {
53
54                         public void configure(RestTemplate restTemplate) {
55
56                                 restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {
57
58                                         public ClientHttpResponse intercept(HttpRequest request, byte[] body,
59                                                         ClientHttpRequestExecution execution) throws IOException {
60
61                                                 request.getHeaders().add("Authorization", msoAdaptersAuth);
62                                                 return execution.execute(request, body);
63                                         }
64                                 });
65                         }
66                 }).build().buildClientFactory();
67                 infraActiveRequestClient = clientFactory.create(InfraActiveRequests.class);
68        
69         }
70
71         public InfraActiveRequests getInfraActiveRequestbyRequestId(String requestId) {
72                 return this.getSingleInfraActiveRequests(this.getUri(endpoint + "/infraActiveRequests/" + requestId));
73         }
74
75         protected InfraActiveRequests getSingleInfraActiveRequests(URI uri) {
76                 return infraActiveRequestClient.get(uri);
77         }
78
79         public void updateInfraActiveRequests(InfraActiveRequests request) {            
80                 infraActiveRequestClient.put(request);
81         }
82
83         protected URI getUri(String uri) {
84                 return URI.create(uri);
85         }
86 }