code improvement
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / client / impl / MRConstants.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.mr.client.impl;
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 class MRConstants
33 {
34         private static final String PROTOCOL = "http";
35         public static final String context = "/";
36         public static final String kBasePath = "events/";
37         public static final int kStdMRServicePort = 8080;
38
39         public static String escape ( String s )
40         {
41                 try
42                 {
43                         return URLEncoder.encode ( s, "UTF-8");
44                 }
45                 catch ( UnsupportedEncodingException e )
46                 {
47                         throw new IllegalArgumentException(e);
48                 }
49         }
50
51         public static String makeUrl ( String rawTopic )
52         {
53                 final String cleanTopic = escape ( rawTopic );
54                 
55                 final StringBuffer url = new StringBuffer().
56                         append ( MRConstants.context ).
57                         append ( MRConstants.kBasePath ).
58                         append ( cleanTopic );
59                 return url.toString ();
60         }
61         
62         public static String makeUrl ( final String host, final String rawTopic )
63         {
64                 final String cleanTopic = escape ( rawTopic );
65                 final StringBuffer url = new StringBuffer();
66                 
67                 if (!host.startsWith("http") && !host.startsWith("https") ) {
68                         url.append( PROTOCOL + "://" );
69                 }
70                 url.append(host);
71                 url.append ( MRConstants.context );
72                 url.append ( MRConstants.kBasePath );
73                 url.append ( cleanTopic );
74                 return url.toString ();
75         }
76
77         public static String makeUrl ( final String host, final String rawTopic, final String transferprotocol,final String parttion )
78         {
79                 final String cleanTopic = escape ( rawTopic );
80
81                 final StringBuffer url = new StringBuffer();
82                 
83                 if (transferprotocol !=null && !transferprotocol.equals("")) {
84                         url.append( transferprotocol + "://" );
85                 }else{
86                         url.append( PROTOCOL + "://" );
87                 }
88                 url.append(host);
89                 url.append ( MRConstants.context );
90                 url.append ( MRConstants.kBasePath );
91                 url.append ( cleanTopic );
92                 if(parttion!=null && !parttion.equalsIgnoreCase(""))
93                         url.append("?partitionKey=").append(parttion);
94                 return url.toString ();
95         }
96         public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
97         {
98                 final String cleanConsumerGroup = escape ( rawConsumerGroup );
99                 final String cleanConsumerId = escape ( rawConsumerId );
100                 return MRConstants.context + MRConstants.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
101         }
102
103         /**
104          * Create a list of HttpHosts from an input list of strings. Input strings have
105          * host[:port] as format. If the port section is not provided, the default port is used.
106          * 
107          * @param hosts
108          * @return a list of hosts
109          */
110         public static List<HttpHost> createHostsList(Collection<String> hosts)
111         {
112                 final ArrayList<HttpHost> convertedHosts = new ArrayList<> ();
113                 for ( String host : hosts )
114                 {
115                         if ( host.length () == 0 ) continue;
116                         convertedHosts.add ( hostForString ( host ) );
117                 }
118                 return convertedHosts;
119         }
120
121         /**
122          * Return an HttpHost from an input string. Input string has
123          * host[:port] as format. If the port section is not provided, the default port is used.
124          * 
125          * @param hosts
126          * @return a list of hosts
127          */
128         public static HttpHost hostForString ( String host )
129         {
130                 if ( host.length() < 1 ) throw new IllegalArgumentException ( "An empty host entry is invalid." );
131                 
132                 String hostPart = host;
133                 int port = kStdMRServicePort;
134
135                 final int colon = host.indexOf ( ':' );
136                 if ( colon == 0 ) throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
137                 if ( colon > 0 )
138                 {
139                         hostPart = host.substring ( 0, colon ).trim();
140
141                         final String portPart = host.substring ( colon + 1 ).trim();
142                         if ( portPart.length () > 0 )
143                         {
144                                 try
145                                 {
146                                         port = Integer.parseInt ( portPart );
147                                 }
148                                 catch ( NumberFormatException x )
149                                 {
150                                         throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
151                                 }
152                         }
153                         // else: use default port on "foo:"
154                 }
155
156                 return new HttpHost ( hostPart, port );
157         }
158
159         public static String makeConsumerUrl(String host, String fTopic, String fGroup, String fId,final String transferprotocol) {
160                 final String cleanConsumerGroup = escape ( fGroup );
161                 final String cleanConsumerId = escape ( fId );
162                 
163                 StringBuffer url = new StringBuffer();
164                 
165                 if (transferprotocol !=null && !transferprotocol.equals("")) {
166                         url.append( transferprotocol + "://" );
167                 }else{
168                         url.append( PROTOCOL + "://" );
169                 }
170                 
171                 url.append(host);
172                 url.append(context);
173                 url.append(kBasePath);
174                 url.append(fTopic + "/" + cleanConsumerGroup + "/" + cleanConsumerId);
175                 
176                 return url.toString();
177         }
178 }