Add the https rest template
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / web / EventClientPublisher.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.web;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.http.client.HttpClient;
26 import org.apache.http.impl.client.HttpClients;
27 import org.apache.http.ssl.SSLContextBuilder;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.web.client.RestTemplateBuilder;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
35 import org.springframework.util.ResourceUtils;
36 import org.springframework.web.client.RestTemplate;
37
38 import javax.net.ssl.SSLContext;
39 import java.io.FileNotFoundException;
40 import java.io.UnsupportedEncodingException;
41 import java.util.Base64;
42
43 @Configuration
44 public class EventClientPublisher {
45
46     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EventClientPublisher.class);
47
48     @Value("${dmaap.ribbon.listOfServers}")
49     private String hosts;
50
51     @Value("${dmaap.ribbon.username:}")
52     private String username;
53
54     @Value("${dmaap.ribbon.password:}")
55     private String password;
56
57     @Value("${dmaap.ribbon.topic:AAI-EVENT}")
58     private String topic;
59
60     @Value("${dmaap.ribbon.batchSize:100}")
61     private int maxBatchSize;
62
63     @Value("${dmaap.ribbon.maxAgeMs:250}")
64     private int maxAgeMs;
65
66     @Value("${dmaap.ribbon.delayBetweenBatches:100}")
67     private int delayBetweenBatches;
68
69     @Value("${dmaap.ribbon.protocol:http}")
70     private String protocol;
71
72     @Value("${dmaap.ribbon.transportType:http}")
73     private String transportType;
74
75     @Value("${dmaap.ribbon.contentType:application/json}")
76     private String contentType;
77
78     @Value("${server.ssl.trust-store:aai_keystore}")
79     private String trustStoreFile;
80
81     @Value("${server.ssl.trust-store-password:somepass}")
82     private String trustStorePass;
83
84     @Bean(name="dmaapRestTemplate")
85     public RestTemplate dmaapRestTemplate() throws Exception {
86
87         if(transportType.equals("https")){
88
89             RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
90
91             SSLContext sslContext = SSLContextBuilder
92                     .create()
93                     .loadTrustMaterial(ResourceUtils.getFile(trustStoreFile), trustStorePass.toCharArray())
94                     .build();
95
96             HttpClient client = HttpClients
97                     .custom()
98                     .setSSLContext(sslContext)
99                     .build();
100
101             LOGGER.info("Creating a dmaap rest template with https using truststore {}", trustStoreFile);
102             return restTemplateBuilder
103                     .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
104                     .build();
105         }
106
107         LOGGER.info("Creating a dmaap rest template using http");
108         return new RestTemplate();
109     }
110
111     @Bean(name="dmaapHeaders")
112     public HttpHeaders dmaapHeaders() throws UnsupportedEncodingException
113     {
114
115         HttpHeaders httpHeaders = new HttpHeaders();
116         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
117
118         if(username != null && password != null){
119
120             if(!StringUtils.EMPTY.equals(username) && !StringUtils.EMPTY.equals(password)){
121
122                 byte[] userPass = (username + ":" + password).getBytes("UTF-8");
123
124                 httpHeaders.set("Authorization", "Basic " + Base64.getEncoder().encodeToString(userPass));
125             }
126         }
127
128         return httpHeaders;
129     }
130
131 }