Release 1.13.5 docker artifact
[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
21 package org.onap.aai.web;
22
23 import java.io.UnsupportedEncodingException;
24 import java.util.Base64;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Value;
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.web.client.RestTemplate;
35
36 @Configuration
37 public class EventClientPublisher {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(EventClientPublisher.class);
40
41     @Value("${dmaap.ribbon.listOfServers:}")
42     private String hosts;
43
44     @Value("${dmaap.ribbon.username:}")
45     private String username;
46
47     @Value("${dmaap.ribbon.password:}")
48     private String password;
49
50     @Value("${dmaap.ribbon.topic:AAI-EVENT}")
51     private String topic;
52
53     @Value("${dmaap.ribbon.batchSize:100}")
54     private int maxBatchSize;
55
56     @Value("${dmaap.ribbon.maxAgeMs:250}")
57     private int maxAgeMs;
58
59     @Value("${dmaap.ribbon.delayBetweenBatches:100}")
60     private int delayBetweenBatches;
61
62     @Value("${dmaap.ribbon.protocol:http}")
63     private String protocol;
64
65     @Value("${dmaap.ribbon.transportType:HTTPNOAUTH}")
66     private String tranportType;
67
68     @Value("${dmaap.ribbon.contentType:application/json}")
69     private String contentType;
70
71     @Bean(name = "dmaapRestTemplate")
72     public RestTemplate dmaapRestTemplate() {
73         return new RestTemplate();
74     }
75
76     @Bean(name = "dmaapHeaders")
77     public HttpHeaders dmaapHeaders() throws UnsupportedEncodingException {
78
79         HttpHeaders httpHeaders = new HttpHeaders();
80         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
81
82         if (username != null && password != null) {
83
84             if (!StringUtils.EMPTY.equals(username) && !StringUtils.EMPTY.equals(password)) {
85
86                 byte[] userPass = (username + ":" + password).getBytes("UTF-8");
87
88                 httpHeaders.set("Authorization", "Basic " + Base64.getEncoder().encodeToString(userPass));
89             }
90         }
91
92         return httpHeaders;
93     }
94
95 }