2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap.http;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.List;
35 import org.apache.http.client.methods.CloseableHttpResponse;
36 import org.apache.http.client.methods.HttpPost;
37 import org.apache.http.entity.StringEntity;
38 import org.onap.appc.adapter.message.Producer;
40 public class HttpDmaapProducerImpl extends CommonHttpClient implements Producer {
42 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(HttpDmaapProducerImpl.class);
44 private static final String CONTENT_TYPE = "application/cambria";
45 private static final String URL_TEMPLATE = "%s/events/%s";
47 private List<String> hosts;
48 private Set<String> topics;
50 public HttpDmaapProducerImpl() {
54 public HttpDmaapProducerImpl(Collection<String> urls, String topicName) {
55 topics = new HashSet<>();
56 topics.add(topicName);
58 hosts = new ArrayList<>();
59 for (String host : urls) {
60 hosts.add(formatHostString(host));
65 public void updateCredentials(String user, String pass) {
66 LOG.debug(String.format("Setting auth to %s for %s", user, this.toString()));
67 this.setBasicAuth(user, pass);
71 public boolean post(String partition, String data) {
74 HttpPost request = postReq(null);
75 request.setHeader("Content-Type", CONTENT_TYPE);
76 request.setEntity(new StringEntity(bodyLine(partition, data)));
78 sent = topics.stream()
79 .filter(topic -> sendRequest(request, topic))
82 } catch (Exception buildEx) {
84 String.format("Failed to build request with string [%s]. Message not sent to any topic. Reason: %s",
85 data, buildEx.getMessage()),
88 return sent == topics.size();
91 private boolean sendRequest(HttpPost request, String topic) {
92 boolean successful = false;
93 String uriStr = String.format(URL_TEMPLATE, hosts.get(0), topic);
95 request.setURI(new URI(uriStr));
96 CloseableHttpResponse response = getClient().execute(request);
97 if (response.getStatusLine().getStatusCode() == 200) {
101 } catch (Exception sendEx) {
102 LOG.error(String.format("Failed to send message to %s. Reason: %s", uriStr, sendEx.getMessage()),
104 if (hosts.size() > 1) {
105 String failedUrl = hosts.remove(0);
106 hosts.add(failedUrl);
107 LOG.debug(String.format("Moving host %s to the end of the pool. New primary host is %s",
108 failedUrl, hosts.get(0)));
115 * Format the body for the application/cambria content type with no partitioning. See
118 * The message body to format
119 * @return A string in the application/cambria content type
121 private String bodyLine(String partition, String message) {
122 String prt = (partition == null) ? "" : partition;
123 String msg = (message == null) ? "" : message;
124 return String.format("%d.%d.%s%s", prt.length(), msg.length(), prt, msg);