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