| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.util; |
| 19 |
|
|
| 20 |
|
import java.io.UnsupportedEncodingException; |
| 21 |
|
import java.net.URI; |
| 22 |
|
import java.net.URISyntaxException; |
| 23 |
|
import java.net.URLDecoder; |
| 24 |
|
import java.net.URLEncoder; |
| 25 |
|
import java.util.ArrayList; |
| 26 |
|
import java.util.Collections; |
| 27 |
|
import java.util.HashMap; |
| 28 |
|
import java.util.Iterator; |
| 29 |
|
import java.util.Map; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
0 |
public class URISupport { |
| 35 |
0 |
public static class CompositeData { |
| 36 |
|
String scheme; |
| 37 |
|
String path; |
| 38 |
|
URI components[]; |
| 39 |
|
Map parameters; |
| 40 |
|
String fragment; |
| 41 |
|
public String host; |
| 42 |
|
|
| 43 |
|
public URI[] getComponents() { |
| 44 |
0 |
return components; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
public String getFragment() { |
| 48 |
0 |
return fragment; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
public Map getParameters() { |
| 52 |
0 |
return parameters; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
public String getScheme() { |
| 56 |
0 |
return scheme; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
public String getPath() { |
| 60 |
0 |
return path; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
public String getHost() { |
| 64 |
0 |
return host; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
public URI toURI() throws URISyntaxException { |
| 68 |
0 |
StringBuffer sb = new StringBuffer(); |
| 69 |
0 |
if (scheme != null) { |
| 70 |
0 |
sb.append(scheme); |
| 71 |
0 |
sb.append(':'); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
0 |
if (host != null && host.length() != 0) { |
| 75 |
0 |
sb.append(host); |
| 76 |
0 |
} |
| 77 |
|
else { |
| 78 |
0 |
sb.append('('); |
| 79 |
0 |
for (int i = 0; i < components.length; i++) { |
| 80 |
0 |
if (i != 0) { |
| 81 |
0 |
sb.append(','); |
| 82 |
|
} |
| 83 |
0 |
sb.append(components[i].toString()); |
| 84 |
|
} |
| 85 |
0 |
sb.append(')'); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
0 |
if (path != null) { |
| 89 |
0 |
sb.append('/'); |
| 90 |
0 |
sb.append(path); |
| 91 |
|
} |
| 92 |
0 |
if (!parameters.isEmpty()) { |
| 93 |
0 |
sb.append("?"); |
| 94 |
0 |
sb.append(createQueryString(parameters)); |
| 95 |
|
} |
| 96 |
0 |
if (fragment != null) { |
| 97 |
0 |
sb.append("#"); |
| 98 |
0 |
sb.append(fragment); |
| 99 |
|
} |
| 100 |
0 |
return new URI(sb.toString()); |
| 101 |
|
} |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
public static Map parseQuery(String uri) throws URISyntaxException { |
| 105 |
|
try { |
| 106 |
1 |
Map rc = new HashMap(); |
| 107 |
1 |
if (uri != null) { |
| 108 |
1 |
String[] parameters = uri.split("&"); |
| 109 |
4 |
for (int i = 0; i < parameters.length; i++) { |
| 110 |
3 |
int p = parameters[i].indexOf("="); |
| 111 |
3 |
if (p >= 0) { |
| 112 |
3 |
String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8"); |
| 113 |
3 |
String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8"); |
| 114 |
3 |
rc.put(name, value); |
| 115 |
3 |
} |
| 116 |
|
else { |
| 117 |
0 |
rc.put(parameters[i], null); |
| 118 |
|
} |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
1 |
return rc; |
| 122 |
|
} |
| 123 |
0 |
catch (UnsupportedEncodingException e) { |
| 124 |
0 |
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e); |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
public static Map parseParamters(URI uri) throws URISyntaxException { |
| 129 |
104 |
String query = uri.getQuery(); |
| 130 |
104 |
if (query == null) { |
| 131 |
103 |
String schemeSpecificPart = uri.getSchemeSpecificPart(); |
| 132 |
103 |
int idx = schemeSpecificPart.lastIndexOf('?'); |
| 133 |
103 |
if (idx < 0) { |
| 134 |
103 |
return Collections.EMPTY_MAP; |
| 135 |
|
} |
| 136 |
|
else { |
| 137 |
0 |
query = schemeSpecificPart.substring(idx + 1); |
| 138 |
|
} |
| 139 |
0 |
} |
| 140 |
|
else { |
| 141 |
1 |
query = stripPrefix(query, "?"); |
| 142 |
|
} |
| 143 |
1 |
return parseQuery(query); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
public static URI removeQuery(URI uri) throws URISyntaxException { |
| 150 |
0 |
return createURIWithQuery(uri, null); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException { |
| 157 |
0 |
return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), query, uri.getFragment()); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
public static CompositeData parseComposite(URI uri) throws URISyntaxException { |
| 161 |
|
|
| 162 |
0 |
CompositeData rc = new CompositeData(); |
| 163 |
0 |
rc.scheme = uri.getScheme(); |
| 164 |
0 |
String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim(); |
| 165 |
|
|
| 166 |
0 |
parseComposite(uri, rc, ssp); |
| 167 |
|
|
| 168 |
0 |
rc.fragment = uri.getFragment(); |
| 169 |
0 |
return rc; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
private static void parseComposite(URI uri, CompositeData rc, String ssp) throws URISyntaxException { |
| 179 |
|
String componentString; |
| 180 |
|
String params; |
| 181 |
|
|
| 182 |
0 |
if (!checkParenthesis(ssp)) { |
| 183 |
0 |
throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis"); |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
int p; |
| 187 |
0 |
int intialParen = ssp.indexOf("("); |
| 188 |
0 |
if (intialParen == 0) { |
| 189 |
0 |
rc.host = ssp.substring(0, intialParen); |
| 190 |
0 |
p = rc.host.indexOf("/"); |
| 191 |
0 |
if (p >= 0) { |
| 192 |
0 |
rc.path = rc.host.substring(p); |
| 193 |
0 |
rc.host = rc.host.substring(0, p); |
| 194 |
|
} |
| 195 |
0 |
p = ssp.lastIndexOf(")"); |
| 196 |
0 |
componentString = ssp.substring(intialParen + 1, p); |
| 197 |
0 |
params = ssp.substring(p + 1).trim(); |
| 198 |
0 |
} |
| 199 |
|
else { |
| 200 |
0 |
componentString = ssp; |
| 201 |
0 |
params = ""; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
0 |
String components[] = splitComponents(componentString); |
| 205 |
0 |
rc.components = new URI[components.length]; |
| 206 |
0 |
for (int i = 0; i < components.length; i++) { |
| 207 |
0 |
rc.components[i] = new URI(components[i].trim()); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
0 |
p = params.indexOf("?"); |
| 211 |
0 |
if (p >= 0) { |
| 212 |
0 |
if (p > 0) { |
| 213 |
0 |
rc.path = stripPrefix(params.substring(0, p), "/"); |
| 214 |
|
} |
| 215 |
0 |
rc.parameters = parseQuery(params.substring(p + 1)); |
| 216 |
0 |
} |
| 217 |
|
else { |
| 218 |
0 |
if (params.length() > 0) { |
| 219 |
0 |
rc.path = stripPrefix(params, "/"); |
| 220 |
|
} |
| 221 |
0 |
rc.parameters = Collections.EMPTY_MAP; |
| 222 |
|
} |
| 223 |
0 |
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
private static String[] splitComponents(String str) { |
| 230 |
0 |
ArrayList l = new ArrayList(); |
| 231 |
|
|
| 232 |
0 |
int last = 0; |
| 233 |
0 |
int depth = 0; |
| 234 |
0 |
char chars[] = str.toCharArray(); |
| 235 |
0 |
for (int i = 0; i < chars.length; i++) { |
| 236 |
0 |
switch (chars[i]) { |
| 237 |
|
case '(': |
| 238 |
0 |
depth++; |
| 239 |
0 |
break; |
| 240 |
|
case ')': |
| 241 |
0 |
depth--; |
| 242 |
0 |
break; |
| 243 |
|
case ',': |
| 244 |
0 |
if (depth == 0) { |
| 245 |
0 |
String s = str.substring(last, i); |
| 246 |
0 |
l.add(s); |
| 247 |
0 |
last = i + 1; |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
|
} |
| 251 |
|
|
| 252 |
0 |
String s = str.substring(last); |
| 253 |
0 |
if (s.length() != 0) { |
| 254 |
0 |
l.add(s); |
| 255 |
|
} |
| 256 |
|
|
| 257 |
0 |
String rc[] = new String[l.size()]; |
| 258 |
0 |
l.toArray(rc); |
| 259 |
0 |
return rc; |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
public static String stripPrefix(String value, String prefix) { |
| 263 |
1 |
if (value.startsWith(prefix)) { |
| 264 |
0 |
return value.substring(prefix.length()); |
| 265 |
|
} |
| 266 |
1 |
return value; |
| 267 |
|
} |
| 268 |
|
|
| 269 |
|
public static URI stripScheme(URI uri) throws URISyntaxException { |
| 270 |
0 |
return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//")); |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
public static String createQueryString(Map options) throws URISyntaxException { |
| 274 |
|
try { |
| 275 |
0 |
if (options.size() > 0) { |
| 276 |
0 |
StringBuffer rc = new StringBuffer(); |
| 277 |
0 |
boolean first = true; |
| 278 |
0 |
for (Iterator iter = options.keySet().iterator(); iter.hasNext();) { |
| 279 |
0 |
if (first) { |
| 280 |
0 |
first = false; |
| 281 |
0 |
} |
| 282 |
|
else { |
| 283 |
0 |
rc.append("&"); |
| 284 |
|
} |
| 285 |
|
|
| 286 |
0 |
String key = (String) iter.next(); |
| 287 |
0 |
String value = (String) options.get(key); |
| 288 |
0 |
rc.append(URLEncoder.encode(key, "UTF-8")); |
| 289 |
0 |
rc.append("="); |
| 290 |
0 |
rc.append(URLEncoder.encode(value, "UTF-8")); |
| 291 |
0 |
} |
| 292 |
0 |
return rc.toString(); |
| 293 |
|
} |
| 294 |
|
else { |
| 295 |
0 |
return ""; |
| 296 |
|
} |
| 297 |
|
} |
| 298 |
0 |
catch (UnsupportedEncodingException e) { |
| 299 |
0 |
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e); |
| 300 |
|
} |
| 301 |
|
} |
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
public static URI createRemainingURI(URI originalURI, Map params) throws URISyntaxException { |
| 309 |
0 |
String s = createQueryString(params); |
| 310 |
0 |
if (s.length() == 0) { |
| 311 |
0 |
s = null; |
| 312 |
|
} |
| 313 |
0 |
return createURIWithQuery(originalURI, s); |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
static public URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException { |
| 317 |
0 |
return new URI(scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr.getPath(), bindAddr.getQuery(), bindAddr.getFragment()); |
| 318 |
|
} |
| 319 |
|
|
| 320 |
|
public static boolean checkParenthesis(String str) { |
| 321 |
0 |
boolean result = true; |
| 322 |
0 |
if (str != null) { |
| 323 |
0 |
int open = 0; |
| 324 |
0 |
int closed = 0; |
| 325 |
|
|
| 326 |
0 |
int i = 0; |
| 327 |
0 |
while ((i = str.indexOf('(', i)) >= 0) { |
| 328 |
0 |
i++; |
| 329 |
0 |
open++; |
| 330 |
0 |
} |
| 331 |
0 |
i = 0; |
| 332 |
0 |
while ((i = str.indexOf(')', i)) >= 0) { |
| 333 |
0 |
i++; |
| 334 |
0 |
closed++; |
| 335 |
0 |
} |
| 336 |
0 |
result = open == closed; |
| 337 |
|
} |
| 338 |
0 |
return result; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
public int indexOfParenthesisMatch(String str) { |
| 342 |
0 |
int result = -1; |
| 343 |
|
|
| 344 |
0 |
return result; |
| 345 |
|
} |
| 346 |
|
} |