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 );
65 final StringBuffer url = new StringBuffer();
67 if (!host.startsWith("http") && !host.startsWith("https") ) {
68 url.append( PROTOCOL + "://" );
71 url.append ( MRConstants.context );
72 url.append ( MRConstants.kBasePath );
73 url.append ( cleanTopic );
74 return url.toString ();
77 public static String makeUrl ( final String host, final String rawTopic, final String transferprotocol,final String parttion )
79 final String cleanTopic = escape ( rawTopic );
81 final StringBuffer url = new StringBuffer();
83 if (transferprotocol !=null && !transferprotocol.equals("")) {
84 url.append( transferprotocol + "://" );
86 url.append( PROTOCOL + "://" );
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 ();
96 public static String makeConsumerUrl ( String topic, String rawConsumerGroup, String rawConsumerId )
98 final String cleanConsumerGroup = escape ( rawConsumerGroup );
99 final String cleanConsumerId = escape ( rawConsumerId );
100 return MRConstants.context + MRConstants.kBasePath + topic + "/" + cleanConsumerGroup + "/" + cleanConsumerId;
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.
108 * @return a list of hosts
110 public static List<HttpHost> createHostsList(Collection<String> hosts)
112 final ArrayList<HttpHost> convertedHosts = new ArrayList<> ();
113 for ( String host : hosts )
115 if ( host.length () == 0 ) continue;
116 convertedHosts.add ( hostForString ( host ) );
118 return convertedHosts;
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.
126 * @return a list of hosts
128 public static HttpHost hostForString ( String host )
130 if ( host.length() < 1 ) throw new IllegalArgumentException ( "An empty host entry is invalid." );
132 String hostPart = host;
133 int port = kStdMRServicePort;
135 final int colon = host.indexOf ( ':' );
136 if ( colon == 0 ) throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid." );
139 hostPart = host.substring ( 0, colon ).trim();
141 final String portPart = host.substring ( colon + 1 ).trim();
142 if ( portPart.length () > 0 )
146 port = Integer.parseInt ( portPart );
148 catch ( NumberFormatException x )
150 throw new IllegalArgumentException ( "Host entry '" + host + "' is invalid.", x );
153 // else: use default port on "foo:"
156 return new HttpHost ( hostPart, port );
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 );
163 StringBuffer url = new StringBuffer();
165 if (transferprotocol !=null && !transferprotocol.equals("")) {
166 url.append( transferprotocol + "://" );
168 url.append( PROTOCOL + "://" );
173 url.append(kBasePath);
174 url.append(fTopic + "/" + cleanConsumerGroup + "/" + cleanConsumerId);
176 return url.toString();