Integrate aai-schema-ingest library into aai-core
[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.springframework.beans.factory.annotation.Value;
26 import org.springframework.context.annotation.Bean;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.MediaType;
30 import org.springframework.web.client.RestTemplate;
31
32 import java.io.UnsupportedEncodingException;
33 import java.util.Base64;
34
35 @Configuration
36 public class EventClientPublisher {
37
38     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EventClientPublisher.class);
39
40     @Value("${dmaap.ribbon.listOfServers}")
41     private String hosts;
42
43     @Value("${dmaap.ribbon.username:}")
44     private String username;
45
46     @Value("${dmaap.ribbon.password:}")
47     private String password;
48
49     @Value("${dmaap.ribbon.topic:AAI-EVENT}")
50     private String topic;
51
52     @Value("${dmaap.ribbon.batchSize:100}")
53     private int maxBatchSize;
54
55     @Value("${dmaap.ribbon.maxAgeMs:250}")
56     private int maxAgeMs;
57
58     @Value("${dmaap.ribbon.delayBetweenBatches:100}")
59     private int delayBetweenBatches;
60
61     @Value("${dmaap.ribbon.protocol:http}")
62     private String protocol;
63
64     @Value("${dmaap.ribbon.transportType:HTTPNOAUTH}")
65     private String tranportType;
66
67     @Value("${dmaap.ribbon.contentType:application/json}")
68     private String contentType;
69
70     @Bean(name="dmaapRestTemplate")
71     public RestTemplate dmaapRestTemplate(){
72         return new RestTemplate();
73     }
74
75     @Bean(name="dmaapHeaders")
76     public HttpHeaders dmaapHeaders() throws UnsupportedEncodingException
77     {
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 }