1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.metrics.publisher;
24 import org.apache.http.HttpHost;
26 import java.io.UnsupportedEncodingException;
27 import java.net.URLEncoder;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
34 * @author anowarul.islam
37 public class CambriaPublisherUtility
39 public static final String kBasePath = "/events/";
40 public static final int kStdCambriaServicePort = 3904;
43 * Translates a string into <code>application/x-www-form-urlencoded</code>
44 * format using a specific encoding scheme.
49 public static String escape ( String s )
53 return URLEncoder.encode ( s, "UTF-8");
55 catch ( UnsupportedEncodingException e )
57 throw new RuntimeException ( e );
66 public static String makeUrl ( String rawTopic )
68 final String cleanTopic = escape ( rawTopic );
70 final StringBuffer url = new StringBuffer().
71 append ( CambriaPublisherUtility.kBasePath ).
72 append ( cleanTopic );
73 return url.toString ();
77 * building consumerUrl
79 * @param rawConsumerGroup
80 * @param rawConsumerId
83 public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
85 final String cleanConsumerGroup = escape ( rawConsumerGroup );
86 final String cleanConsumerId = escape ( rawConsumerId );
87 return CambriaPublisherUtility.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
91 * Create a list of HttpHosts from an input list of strings. Input strings have
92 * host[:port] as format. If the port section is not provided, the default port is used.
95 * @return a list of hosts
97 public static List<HttpHost> createHostsList(Collection<String> hosts)
99 final ArrayList<HttpHost> convertedHosts = new ArrayList<>();
100 for ( String host : hosts )
102 if ( host.length () == 0 ){
106 convertedHosts.add ( hostForString ( host ) );
108 return convertedHosts;
112 * Return an HttpHost from an input string. Input string has
113 * host[:port] as format. If the port section is not provided, the default port is used.
116 * @return a list of hosts
117 * if host.length<1 throws IllegalArgumentException
120 public static HttpHost hostForString ( String host )
122 if ( host.length() < 1 ){
123 throw new IllegalArgumentException ( "An empty host entry is invalid." );
126 String hostPart = host;
127 int port = kStdCambriaServicePort;
129 final int colon = host.indexOf ( ':' );
131 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 )
143 port = Integer.parseInt ( portPart );
145 catch ( NumberFormatException x )
147 throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
150 // else: use default port on "foo:"
153 return new HttpHost ( hostPart, port );