1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.client.impl;
27 import java.net.URLEncoder;
28 import java.nio.charset.StandardCharsets;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32 import org.apache.http.HttpHost;
36 private MRConstants() {
40 private static final String PROTOCOL = "http";
41 public static final String CONTEXT = "/";
42 public static final String BASE_PATH = "events/";
43 public static final int STD_MR_SERVICE_PORT = 8080;
45 public static String escape(String url) {
46 return URLEncoder.encode(url, StandardCharsets.UTF_8);
49 public static String makeUrl(String rawTopic) {
50 final String cleanTopic = escape(rawTopic);
52 return new StringBuilder()
53 .append(MRConstants.CONTEXT)
54 .append(MRConstants.BASE_PATH)
55 .append(cleanTopic).toString();
58 public static String makeUrl(final String host, final String rawTopic) {
59 final String cleanTopic = escape(rawTopic);
60 final StringBuilder url = new StringBuilder();
62 if (!host.startsWith("http") && !host.startsWith("https")) {
63 url.append(PROTOCOL).append("://");
66 url.append(MRConstants.CONTEXT);
67 url.append(MRConstants.BASE_PATH);
68 url.append(cleanTopic);
69 return url.toString();
72 public static String makeUrl(final String host, final String rawTopic, final String transferProtocol, final String partition) {
73 final String cleanTopic = escape(rawTopic);
75 final StringBuilder url = new StringBuilder();
77 if (transferProtocol != null && !transferProtocol.isEmpty()) {
78 url.append(transferProtocol).append("://");
80 url.append(PROTOCOL).append("://");
83 url.append(MRConstants.CONTEXT);
84 url.append(MRConstants.BASE_PATH);
85 url.append(cleanTopic);
86 if (partition != null && !partition.isEmpty()) {
87 url.append("?partitionKey=").append(partition);
89 return url.toString();
92 public static String makeConsumerUrl(String topic, String rawConsumerGroup, String rawConsumerId) {
93 final String cleanConsumerGroup = escape(rawConsumerGroup);
94 final String cleanConsumerId = escape(rawConsumerId);
95 return MRConstants.CONTEXT + MRConstants.BASE_PATH + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
99 * Create a list of HttpHosts from an input list of strings. Input strings have
100 * host[:port] as format. If the port section is not provided, the default port is used.
103 * @return a list of hosts
105 public static List<HttpHost> createHostsList(Collection<String> hosts) {
106 final ArrayList<HttpHost> convertedHosts = new ArrayList<>();
107 for (String host : hosts) {
108 if (host.length() == 0) {
111 convertedHosts.add(hostForString(host));
113 return convertedHosts;
117 * Return an HttpHost from an input string. Input string has
118 * host[:port] as format. If the port section is not provided, the default port is used.
121 * @return a list of hosts
123 public static HttpHost hostForString(String host) {
124 if (host.length() < 1) {
125 throw new IllegalArgumentException("An empty host entry is invalid.");
128 String hostPart = host;
129 int port = STD_MR_SERVICE_PORT;
131 final int colon = host.indexOf(':');
133 throw new IllegalArgumentException("Host entry '" + host + "' is invalid.");
136 hostPart = host.substring(0, colon).trim();
138 final String portPart = host.substring(colon + 1).trim();
139 if (portPart.length() > 0) {
141 port = Integer.parseInt(portPart);
142 } catch (NumberFormatException x) {
143 throw new IllegalArgumentException("Host entry '" + host + "' is invalid.", x);
146 // else: use default port on "foo:"
149 return new HttpHost(hostPart, port);
152 public static String makeConsumerUrl(String host, String topic, String group, String id, final String transferprotocol) {
153 final String cleanConsumerGroup = escape(group);
154 final String cleanConsumerId = escape(id);
156 StringBuilder url = new StringBuilder();
158 if (transferprotocol != null && !transferprotocol.equals("")) {
159 url.append(transferprotocol).append("://");
161 url.append(PROTOCOL).append("://");
168 .append("/").append(cleanConsumerGroup)
169 .append("/").append(cleanConsumerId);
171 return url.toString();