2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.client.impl.protocol;
26 import com.att.nsa.cambria.client.CambriaClientBuilders.ConsumerBuilder;
27 import com.att.nsa.cambria.client.CambriaConsumer;
29 import java.io.IOException;
30 import java.lang.reflect.Field;
31 import java.net.MalformedURLException;
32 import java.security.GeneralSecurityException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.List;
37 class ConsumerImpl implements Consumer {
39 private static final int DEFAULT_LIMIT = 1000;
41 private Collection<String> hosts;
44 private String groupId;
47 private String authKey;
48 private String authSecret;
50 private CambriaConsumer consumer = null;
60 public ConsumerImpl(Collection<String> urls, String topicName, String consumerName, String consumerId, Integer timeout, String apiKey, String apiSecret) throws MalformedURLException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
62 this.topic = topicName;
63 this.group = consumerName;
64 this.groupId = consumerId;
65 this.authKey = apiKey;
66 this.authSecret = apiSecret;
67 this.timeout = timeout;
68 consumer = getConsumer();
72 public List<String> fetch() throws IOException {
74 return fetch(DEFAULT_LIMIT);
77 public List<String> fetch(int limit) throws IOException {
79 List<String> out = new ArrayList<String>();
81 for(String msg : consumer.fetch(timeout,limit)){
84 } catch (IOException e) {
90 public void registerForRead() throws IOException {
92 int waitForRegisteration = 1; //return from fetch after 1ms, no need to read any messages
93 consumer.fetch(waitForRegisteration, 1);
97 * init cambria consumer
98 * @return CambriaConsumer
100 private CambriaConsumer getConsumer() throws MalformedURLException, GeneralSecurityException, NoSuchFieldException, IllegalAccessException {
102 ConsumerBuilder builder = new ConsumerBuilder();
104 builder.usingHosts(hosts).onTopic(topic).knownAs(group, groupId);
105 builder.withSocketTimeout(timeout + 5000).waitAtServer(timeout);
106 builder.receivingAtMost(DEFAULT_LIMIT);
108 //added for loop for defect 354330
109 for (String url : hosts) {
110 if (url.contains("3905") || url.contains("https")) {
111 builder.usingHttps(true);
116 // Add credentials if provided
117 if (authKey != null && authSecret != null) {
119 Field apiKeyField = ConsumerBuilder.class.getDeclaredField("fApiKey");
120 apiKeyField.setAccessible(true);
121 apiKeyField.set(builder, "");
122 builder.authenticatedBy(authKey, authSecret);
125 return builder.build();
129 public void close() {