2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.http;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.List;
34 import org.apache.http.client.methods.CloseableHttpResponse;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.StringEntity;
37 import org.onap.appc.adapter.message.Producer;
39 public class HttpDmaapProducerImpl extends CommonHttpClient implements Producer {
41 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(HttpDmaapProducerImpl.class);
43 private static final String CONTENT_TYPE = "application/cambria";
44 private static final String URL_TEMPLATE = "%s/events/%s";
46 private List<String> hosts;
47 private Set<String> topics;
49 public HttpDmaapProducerImpl() {
53 public HttpDmaapProducerImpl(Collection<String> urls, String topicName, String username, String password) {
54 this(urls, topicName);
55 updateCredentials(username, password);
58 public HttpDmaapProducerImpl(Collection<String> urls, String topicName) {
59 topics = new HashSet<>();
60 topics.add(topicName);
62 hosts = new ArrayList<>();
63 for (String host : urls) {
64 hosts.add(formatHostString(host));
69 public void updateCredentials(String user, String pass) {
70 LOG.debug(String.format("Setting auth to %s for %s", user, this.toString()));
71 this.setBasicAuth(user, pass);
75 public boolean post(String partition, String data) {
76 LOG.debug("Entering HttpDmaapProducerImpl::: post ");
79 HttpPost request = postReq(null);
80 request.setHeader("Content-Type", CONTENT_TYPE);
81 request.setEntity(new StringEntity(bodyLine(partition, data)));
83 LOG.debug("Before sendRequest HttpDmaapProducerImpl::: post ");
84 sent = topics.stream()
85 .filter(topic -> sendRequest(request, topic))
88 } catch (Exception buildEx) {
90 String.format("Failed to build request with string [%s]. Message not sent to any topic. Reason: %s",
91 data, buildEx.getMessage()),
94 LOG.debug("Exiting HttpDmaapProducerImpl::: post ");
95 return sent == topics.size();
98 private boolean sendRequest(HttpPost request, String topic) {
99 boolean successful = false;
100 String uriStr = String.format(URL_TEMPLATE, hosts.get(0), topic);
102 request.setURI(new URI(uriStr));
103 LOG.debug("HttpDmaapProducerImpl::: before sendRequest()");
104 CloseableHttpResponse response = getClient().execute(request);
105 LOG.debug("HttpDmaapProducerImpl::: after sendRequest()");
106 if (response.getStatusLine().getStatusCode() == 200) {
110 LOG.debug("HttpDmaapProducerImpl::: did not receive 200 for sendRequest");
113 } catch (Exception sendEx) {
114 LOG.error(String.format("Failed to send message to %s. Reason: %s", uriStr, sendEx.getMessage()),
116 if (hosts.size() > 1) {
117 String failedUrl = hosts.remove(0);
118 hosts.add(failedUrl);
119 LOG.debug(String.format("Moving host %s to the end of the pool. New primary host is %s",
120 failedUrl, hosts.get(0)));
127 * Format the body for the application/cambria content type with no partitioning. See
130 * The message body to format
131 * @return A string in the application/cambria content type
133 private String bodyLine(String partition, String message) {
134 String prt = (partition == null) ? "" : partition;
135 String msg = (message == null) ? "" : message;
136 return String.format("%d.%d.%s%s", prt.length(), msg.length(), prt, msg);