fixed code smells
[dmaap/messagerouter/msgrtr.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 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 ){
102                                 continue;
103                         }
104                         
105                         convertedHosts.add ( hostForString ( host ) );
106                 }
107                 return convertedHosts;
108         }
109
110         /**
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.
113          * 
114          * @param hosts
115          * @return a list of hosts
116          * if host.length<1 throws IllegalArgumentException
117          * 
118          */
119         public static HttpHost hostForString ( String host )
120         {
121                 if ( host.length() < 1 ){
122                         throw new IllegalArgumentException ( "An empty host entry is invalid." );
123                 }
124                 
125                 String hostPart = host;
126                 int port = kStdCambriaServicePort;
127
128                 final int colon = host.indexOf ( ':' );
129                 if ( colon == 0 ){
130                         throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
131                 }
132                 
133                 if ( colon > 0 )
134                 {
135                         hostPart = host.substring ( 0, colon ).trim();
136
137                         final String portPart = host.substring ( colon + 1 ).trim();
138                         if ( portPart.length () > 0 )
139                         {
140                                 try
141                                 {
142                                         port = Integer.parseInt ( portPart );
143                                 }
144                                 catch ( NumberFormatException x )
145                                 {
146                                         throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
147                                 }
148                         }
149                         // else: use default port on "foo:"
150                 }
151
152                 return new HttpHost ( hostPart, port );
153         }
154 }