2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap.http;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.List;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
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.openecomp.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 private boolean useHttps = false;
51 public HttpDmaapProducerImpl(Collection<String> urls, String topicName) {
52 hosts = new ArrayList<String>();
53 topics = new HashSet<String>();
54 topics.add(topicName);
56 for (String host : urls) {
57 hosts.add(formatHostString(host));
61 public HttpDmaapProducerImpl(Collection<String> urls, Set<String> topicNames) {
62 hosts = new ArrayList<String>();
65 for (String host : urls) {
66 hosts.add(formatHostString(host));
71 public void updateCredentials(String user, String pass) {
72 LOG.debug(String.format("Setting auth to %s for %s", user, this.toString()));
73 this.setBasicAuth(user, pass);
77 public boolean post(String partition, String data) {
80 HttpPost request = postReq(null);
81 request.setHeader("Content-Type", CONTENT_TYPE);
82 request.setEntity(new StringEntity(bodyLine(partition, data)));
84 for (String topic : topics) {
85 String uriStr = String.format(URL_TEMPLATE, hosts.get(0), topic);
87 request.setURI(new URI(uriStr));
88 CloseableHttpResponse response = getClient().execute(request);
89 if (response.getStatusLine().getStatusCode() == 200) {
93 } catch (Exception sendEx) {
94 LOG.error(String.format("Failed to send message to %s. Reason: %s", uriStr, sendEx.getMessage()),
96 if (hosts.size() > 1) {
97 String failedUrl = hosts.remove(0);
99 LOG.debug(String.format("Moving host %s to the end of the pool. New primary host is %s",
100 failedUrl, hosts.get(0)));
104 } catch (Exception buildEx) {
106 String.format("Failed to build request with string [%s]. Message not sent to any topic. Reason: %s",
107 data, buildEx.getMessage()),
110 return sent == topics.size();
114 public void useHttps(boolean yes) {
119 * Format the body for the application/cambria content type with no partitioning.
122 * The message body to format
123 * @return A string in the application/cambria content type
125 private String bodyLine(String partition, String msg) {
126 String p = (partition == null) ? "" : partition;
127 String m = (msg == null) ? "" : msg;
128 return String.format("%d.%d.%s%s", p.length(), m.length(), p, m);
132 public void close() {