[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / client / impl / MRConstants.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
13  *
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=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.client.impl;
26
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;
33
34 class MRConstants {
35
36     private MRConstants() {
37
38     }
39
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;
44
45     public static String escape(String url) {
46         return URLEncoder.encode(url, StandardCharsets.UTF_8);
47     }
48
49     public static String makeUrl(String rawTopic) {
50         final String cleanTopic = escape(rawTopic);
51
52         return new StringBuilder()
53                 .append(MRConstants.CONTEXT)
54                 .append(MRConstants.BASE_PATH)
55                 .append(cleanTopic).toString();
56     }
57
58     public static String makeUrl(final String host, final String rawTopic) {
59         final String cleanTopic = escape(rawTopic);
60         final StringBuilder url = new StringBuilder();
61
62         if (!host.startsWith("http") && !host.startsWith("https")) {
63             url.append(PROTOCOL).append("://");
64         }
65         url.append(host);
66         url.append(MRConstants.CONTEXT);
67         url.append(MRConstants.BASE_PATH);
68         url.append(cleanTopic);
69         return url.toString();
70     }
71
72     public static String makeUrl(final String host, final String rawTopic, final String transferProtocol, final String partition) {
73         final String cleanTopic = escape(rawTopic);
74
75         final StringBuilder url = new StringBuilder();
76
77         if (transferProtocol != null && !transferProtocol.isEmpty()) {
78             url.append(transferProtocol).append("://");
79         } else {
80             url.append(PROTOCOL).append("://");
81         }
82         url.append(host);
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);
88         }
89         return url.toString();
90     }
91
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;
96     }
97
98     /**
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.
101      *
102      * @param hosts
103      * @return a list of hosts
104      */
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) {
109                 continue;
110             }
111             convertedHosts.add(hostForString(host));
112         }
113         return convertedHosts;
114     }
115
116     /**
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.
119      *
120      * @param host
121      * @return a list of hosts
122      */
123     public static HttpHost hostForString(String host) {
124         if (host.length() < 1) {
125             throw new IllegalArgumentException("An empty host entry is invalid.");
126         }
127
128         String hostPart = host;
129         int port = STD_MR_SERVICE_PORT;
130
131         final int colon = host.indexOf(':');
132         if (colon == 0) {
133             throw new IllegalArgumentException("Host entry '" + host + "' is invalid.");
134         }
135         if (colon > 0) {
136             hostPart = host.substring(0, colon).trim();
137
138             final String portPart = host.substring(colon + 1).trim();
139             if (portPart.length() > 0) {
140                 try {
141                     port = Integer.parseInt(portPart);
142                 } catch (NumberFormatException x) {
143                     throw new IllegalArgumentException("Host entry '" + host + "' is invalid.", x);
144                 }
145             }
146             // else: use default port on "foo:"
147         }
148
149         return new HttpHost(hostPart, port);
150     }
151
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);
155
156         StringBuilder url = new StringBuilder();
157
158         if (transferprotocol != null && !transferprotocol.equals("")) {
159             url.append(transferprotocol).append("://");
160         } else {
161             url.append(PROTOCOL).append("://");
162         }
163
164         url.append(host)
165                 .append(CONTEXT)
166                 .append(BASE_PATH)
167                 .append(topic)
168                 .append("/").append(cleanConsumerGroup)
169                 .append("/").append(cleanConsumerId);
170
171         return url.toString();
172     }
173 }