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