1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
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;
30 import org.apache.http.HttpHost;
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;
39 public static String escape ( String s )
43 return URLEncoder.encode ( s, "UTF-8");
45 catch ( UnsupportedEncodingException e )
47 throw new IllegalArgumentException(e);
51 public static String makeUrl ( String rawTopic )
53 final String cleanTopic = escape ( rawTopic );
55 final StringBuffer url = new StringBuffer().
56 append ( MRConstants.context ).
57 append ( MRConstants.kBasePath ).
58 append ( cleanTopic );
59 return url.toString ();
62 public static String makeUrl ( final String host, final String rawTopic )
64 final String cleanTopic = escape ( rawTopic );
66 final StringBuffer url = new StringBuffer();
68 if (!host.startsWith("http") || !host.startsWith("https") ) {
69 url.append( PROTOCOL + "://" );
72 url.append ( MRConstants.context );
73 url.append ( MRConstants.kBasePath );
74 url.append ( cleanTopic );
75 return url.toString ();
78 public static String makeUrl ( final String host, final String rawTopic, final String transferprotocol,final String parttion )
80 final String cleanTopic = escape ( rawTopic );
82 final StringBuffer url = new StringBuffer();
84 if (transferprotocol !=null && !transferprotocol.equals("")) {
85 url.append( transferprotocol + "://" );
87 url.append( PROTOCOL + "://" );
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 ();
97 public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
99 final String cleanConsumerGroup = escape ( rawConsumerGroup );
100 final String cleanConsumerId = escape ( rawConsumerId );
101 return MRConstants.context + MRConstants.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
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.
109 * @return a list of hosts
111 public static List<HttpHost> createHostsList(Collection<String> hosts)
113 final ArrayList<HttpHost> convertedHosts = new ArrayList<> ();
114 for ( String host : hosts )
116 if ( host.length () == 0 ) continue;
117 convertedHosts.add ( hostForString ( host ) );
119 return convertedHosts;
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.
127 * @return a list of hosts
129 public static HttpHost hostForString ( String host )
131 if ( host.length() < 1 ) throw new IllegalArgumentException ( "An empty host entry is invalid." );
133 String hostPart = host;
134 int port = kStdMRServicePort;
136 final int colon = host.indexOf ( ':' );
137 if ( colon == 0 ) throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
140 hostPart = host.substring ( 0, colon ).trim();
142 final String portPart = host.substring ( colon + 1 ).trim();
143 if ( portPart.length () > 0 )
147 port = Integer.parseInt ( portPart );
149 catch ( NumberFormatException x )
151 throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
154 // else: use default port on "foo:"
157 return new HttpHost ( hostPart, port );
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 );
164 StringBuffer url = new StringBuffer();
166 if (transferprotocol !=null && !transferprotocol.equals("")) {
167 url.append( transferprotocol + "://" );
169 url.append( PROTOCOL + "://" );
174 url.append(kBasePath);
175 url.append(fTopic + "/" + cleanConsumerGroup + "/" + cleanConsumerId);
177 return url.toString();