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 java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
30 import org.apache.http.HttpHost;
33 * @author anowarul.islam
36 public class CambriaPublisherUtility
38 public static final String kBasePath = "/events/";
39 public static final int kStdCambriaServicePort = 3904;
42 * Translates a string into <code>application/x-www-form-urlencoded</code>
43 * format using a specific encoding scheme.
48 public static String escape ( String s )
52 return URLEncoder.encode ( s, "UTF-8");
54 catch ( UnsupportedEncodingException e )
56 throw new RuntimeException ( e );
65 public static String makeUrl ( String rawTopic )
67 final String cleanTopic = escape ( rawTopic );
69 final StringBuffer url = new StringBuffer().
70 append ( CambriaPublisherUtility.kBasePath ).
71 append ( cleanTopic );
72 return url.toString ();
76 * building consumerUrl
78 * @param rawConsumerGroup
79 * @param rawConsumerId
82 public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
84 final String cleanConsumerGroup = escape ( rawConsumerGroup );
85 final String cleanConsumerId = escape ( rawConsumerId );
86 return CambriaPublisherUtility.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
90 * Create a list of HttpHosts from an input list of strings. Input strings have
91 * host[:port] as format. If the port section is not provided, the default port is used.
94 * @return a list of hosts
96 public static List<HttpHost> createHostsList(Collection<String> hosts)
98 final ArrayList<HttpHost> convertedHosts = new ArrayList<>();
99 for ( String host : hosts )
101 if ( host.length () == 0 ){
105 convertedHosts.add ( hostForString ( host ) );
107 return convertedHosts;
111 * Return an HttpHost from an input string. Input string has
112 * host[:port] as format. If the port section is not provided, the default port is used.
115 * @return a list of hosts
116 * if host.length<1 throws IllegalArgumentException
119 public static HttpHost hostForString ( String host )
121 if ( host.length() < 1 ){
122 throw new IllegalArgumentException ( "An empty host entry is invalid." );
125 String hostPart = host;
126 int port = kStdCambriaServicePort;
128 final int colon = host.indexOf ( ':' );
130 throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
135 hostPart = host.substring ( 0, colon ).trim();
137 final String portPart = host.substring ( colon + 1 ).trim();
138 if ( portPart.length () > 0 )
142 port = Integer.parseInt ( portPart );
144 catch ( NumberFormatException x )
146 throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
149 // else: use default port on "foo:"
152 return new HttpHost ( hostPart, port );