bump the version
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / metrics / publisher / CambriaPublisherUtility.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.dmf.mr.metrics.publisher;
23
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;
29
30 import org.apache.http.HttpHost;
31 /**
32  * 
33  * @author anowarul.islam
34  *
35  */
36 public class CambriaPublisherUtility
37 {
38         public static final String kBasePath = "/events/";
39         public static final int kStdCambriaServicePort = 3904;
40 /**
41  * 
42  * Translates a string into <code>application/x-www-form-urlencoded</code>
43  * format using a specific encoding scheme.
44  * @param s
45  * @return
46  * 
47  */
48         public static String escape ( String s )
49         {
50                 try
51                 {
52                         return URLEncoder.encode ( s, "UTF-8");
53                 }
54                 catch ( UnsupportedEncodingException e )
55                 {
56                         throw new RuntimeException ( e );
57                 }
58         }
59 /**
60  * 
61  * building url
62  * @param rawTopic
63  * @return
64  */
65         public static String makeUrl ( String rawTopic )
66         {
67                 final String cleanTopic = escape ( rawTopic );
68                 
69                 final StringBuffer url = new StringBuffer().
70                         append ( CambriaPublisherUtility.kBasePath ).
71                         append ( cleanTopic );
72                 return url.toString ();
73         }
74 /**
75  * 
76  * building consumerUrl
77  * @param topic
78  * @param rawConsumerGroup
79  * @param rawConsumerId
80  * @return
81  */
82         public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
83         {
84                 final String cleanConsumerGroup = escape ( rawConsumerGroup );
85                 final String cleanConsumerId = escape ( rawConsumerId );
86                 return CambriaPublisherUtility.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
87         }
88
89         /**
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.
92          * 
93          * @param hosts
94          * @return a list of hosts
95          */
96         public static List<HttpHost> createHostsList(Collection<String> hosts)
97         {
98                 final ArrayList<HttpHost> convertedHosts = new ArrayList<>();
99                 for ( String host : hosts )
100                 {
101                         if ( host.length () == 0 ) continue;
102                         convertedHosts.add ( hostForString ( host ) );
103                 }
104                 return convertedHosts;
105         }
106
107         /**
108          * Return an HttpHost from an input string. Input string has
109          * host[:port] as format. If the port section is not provided, the default port is used.
110          * 
111          * @param hosts
112          * @return a list of hosts
113          * if host.length<1 throws IllegalArgumentException
114          * 
115          */
116         public static HttpHost hostForString ( String host )
117         {
118                 if ( host.length() < 1 ) throw new IllegalArgumentException ( "An empty host entry is invalid." );
119                 
120                 String hostPart = host;
121                 int port = kStdCambriaServicePort;
122
123                 final int colon = host.indexOf ( ':' );
124                 if ( colon == 0 ) throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
125                 if ( colon > 0 )
126                 {
127                         hostPart = host.substring ( 0, colon ).trim();
128
129                         final String portPart = host.substring ( colon + 1 ).trim();
130                         if ( portPart.length () > 0 )
131                         {
132                                 try
133                                 {
134                                         port = Integer.parseInt ( portPart );
135                                 }
136                                 catch ( NumberFormatException x )
137                                 {
138                                         throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
139                                 }
140                         }
141                         // else: use default port on "foo:"
142                 }
143
144                 return new HttpHost ( hostPart, port );
145         }
146 }