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