2 * ============LICENSE_START=======================================================
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
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 org.onap.aai.web;
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;
38 import javax.net.ssl.SSLContext;
39 import java.io.FileNotFoundException;
40 import java.io.UnsupportedEncodingException;
41 import java.util.Base64;
44 public class EventClientPublisher {
46 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EventClientPublisher.class);
48 @Value("${dmaap.ribbon.listOfServers}")
51 @Value("${dmaap.ribbon.username:}")
52 private String username;
54 @Value("${dmaap.ribbon.password:}")
55 private String password;
57 @Value("${dmaap.ribbon.topic:AAI-EVENT}")
60 @Value("${dmaap.ribbon.batchSize:100}")
61 private int maxBatchSize;
63 @Value("${dmaap.ribbon.maxAgeMs:250}")
66 @Value("${dmaap.ribbon.delayBetweenBatches:100}")
67 private int delayBetweenBatches;
69 @Value("${dmaap.ribbon.protocol:http}")
70 private String protocol;
72 @Value("${dmaap.ribbon.transportType:http}")
73 private String transportType;
75 @Value("${dmaap.ribbon.contentType:application/json}")
76 private String contentType;
78 @Value("${server.ssl.trust-store:aai_keystore}")
79 private String trustStoreFile;
81 @Value("${server.ssl.trust-store-password:somepass}")
82 private String trustStorePass;
84 @Bean(name="dmaapRestTemplate")
85 public RestTemplate dmaapRestTemplate() throws Exception {
87 if(transportType.equals("https")){
89 RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
91 SSLContext sslContext = SSLContextBuilder
93 .loadTrustMaterial(ResourceUtils.getFile(trustStoreFile), trustStorePass.toCharArray())
96 HttpClient client = HttpClients
98 .setSSLContext(sslContext)
101 LOGGER.info("Creating a dmaap rest template with https using truststore {}", trustStoreFile);
102 return restTemplateBuilder
103 .requestFactory(new HttpComponentsClientHttpRequestFactory(client))
107 LOGGER.info("Creating a dmaap rest template using http");
108 return new RestTemplate();
111 @Bean(name="dmaapHeaders")
112 public HttpHeaders dmaapHeaders() throws UnsupportedEncodingException
115 HttpHeaders httpHeaders = new HttpHeaders();
116 httpHeaders.setContentType(MediaType.APPLICATION_JSON);
118 if(username != null && password != null){
120 if(!StringUtils.EMPTY.equals(username) && !StringUtils.EMPTY.equals(password)){
122 byte[] userPass = (username + ":" + password).getBytes("UTF-8");
124 httpHeaders.set("Authorization", "Basic " + Base64.getEncoder().encodeToString(userPass));