1 10 11 package org.mule.impl.endpoint; 12 13 import java.net.URI ; 14 import java.net.URISyntaxException ; 15 import java.util.Properties ; 16 17 import org.apache.commons.lang.StringUtils; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.mule.MuleManager; 21 import org.mule.providers.service.ConnectorFactory; 22 import org.mule.providers.service.ConnectorFactoryException; 23 import org.mule.providers.service.ConnectorServiceDescriptor; 24 import org.mule.umo.endpoint.MalformedEndpointException; 25 import org.mule.umo.endpoint.UMOEndpointURI; 26 import org.mule.util.PropertiesUtils; 27 28 38 39 public class MuleEndpointURI implements UMOEndpointURI 40 { 41 44 private static final long serialVersionUID = 3906735768171252877L; 45 46 49 protected static final Log logger = LogFactory.getLog(MuleEndpointURI.class); 50 51 public static boolean isMuleUri(String url) 52 { 53 return url.indexOf(":/") != -1; 54 } 55 56 private String address; 57 private String filterAddress; 58 private String endpointName; 59 private String connectorName; 60 private String transformers; 61 private String responseTransformers; 62 private int createConnector = ConnectorFactory.GET_OR_CREATE_CONNECTOR; 63 private Properties params = new Properties (); 64 private URI uri; 65 private String uriString; 66 private String userInfo; 67 private String schemeMetaInfo; 68 private String resourceInfo; 69 70 MuleEndpointURI(String address, 71 String endpointName, 72 String connectorName, 73 String transformers, 74 String responseTransformers, 75 int createConnector, 76 Properties properties, 77 URI uri, 78 String userInfo) 79 { 80 this(address, endpointName, connectorName, transformers, responseTransformers, createConnector, 81 properties, uri); 82 if (userInfo != null) 83 { 84 this.userInfo = userInfo; 85 } 86 } 87 88 public MuleEndpointURI(String address, 89 String endpointName, 90 String connectorName, 91 String transformers, 92 String responseTransformers, 93 int createConnector, 94 Properties properties, 95 URI uri) 96 { 97 this.address = address; 98 this.endpointName = endpointName; 99 this.connectorName = connectorName; 100 this.transformers = transformers; 101 this.responseTransformers = responseTransformers; 102 this.createConnector = createConnector; 103 this.params = properties; 104 this.uri = uri; 105 this.uriString = uri.toASCIIString(); 106 this.userInfo = uri.getUserInfo(); 107 if (properties != null) 108 { 109 resourceInfo = (String )properties.remove("resourceInfo"); 110 } 111 } 112 113 public MuleEndpointURI(UMOEndpointURI endpointUri) 114 { 115 initialise(endpointUri); 116 } 117 118 public MuleEndpointURI(UMOEndpointURI endpointUri, String filterAddress) 119 { 120 initialise(endpointUri); 121 this.filterAddress = filterAddress; 122 } 123 124 public MuleEndpointURI(String uri) throws MalformedEndpointException 125 { 126 String uriIdentifier = MuleManager.getInstance().lookupEndpointIdentifier(uri, uri); 127 if (!uriIdentifier.equals(uri)) 128 { 129 endpointName = uri; 130 uri = uriIdentifier; 131 } 132 133 uri = uri.trim().replaceAll(" ", "%20"); 134 135 if (!validateUrl(uri)) 136 { 137 throw new MalformedEndpointException(uri); 138 } 139 try 140 { 141 schemeMetaInfo = retrieveSchemeMetaInfo(uri); 142 if (schemeMetaInfo != null) 143 { 144 uri = uri.replaceFirst(schemeMetaInfo + ":", ""); 145 } 146 this.uri = new URI (uri); 147 this.userInfo = this.uri.getRawUserInfo(); 148 } 149 catch (URISyntaxException e) 150 { 151 throw new MalformedEndpointException(uri, e); 152 } 153 154 try 155 { 156 String scheme = (schemeMetaInfo == null ? this.uri.getScheme() : schemeMetaInfo); 157 ConnectorServiceDescriptor csd = ConnectorFactory.getServiceDescriptor(scheme); 158 EndpointBuilder builder = csd.createEndpointBuilder(); 159 UMOEndpointURI built = builder.build(this.uri); 160 initialise(built); 161 } 162 catch (ConnectorFactoryException e) 163 { 164 throw new MalformedEndpointException(e); 165 } 166 } 167 168 private String retrieveSchemeMetaInfo(String url) 169 { 170 int i = url.indexOf(':'); 171 if (i == -1) 172 { 173 return null; 174 } 175 if (url.charAt(i + 1) == '/') 176 { 177 return null; 178 } 179 else 180 { 181 return url.substring(0, i); 182 } 183 } 184 185 protected boolean validateUrl(String url) 186 { 187 return (url.indexOf(":/") > 0); 188 } 189 190 private void initialise(UMOEndpointURI endpointUri) 191 { 192 this.address = endpointUri.getAddress(); 193 if (this.endpointName == null) 194 { 195 this.endpointName = endpointUri.getEndpointName(); 196 } 197 this.connectorName = endpointUri.getConnectorName(); 198 this.transformers = endpointUri.getTransformers(); 199 this.responseTransformers = endpointUri.getResponseTransformers(); 200 this.createConnector = endpointUri.getCreateConnector(); 201 this.params = endpointUri.getParams(); 202 this.uri = endpointUri.getUri(); 203 this.uriString = this.uri.toASCIIString(); 204 this.resourceInfo = endpointUri.getResourceInfo(); 205 this.userInfo = endpointUri.getUserInfo(); 206 } 207 208 public String getAddress() 209 { 210 return address; 211 } 212 213 public String getEndpointName() 214 { 215 return (StringUtils.isEmpty(endpointName) ? null : endpointName); 216 } 217 218 public Properties getParams() 219 { 220 if (params.size() == 0 && getQuery() != null) 223 { 224 params = PropertiesUtils.getPropertiesFromQueryString(getQuery()); 225 } 226 return params; 227 } 228 229 public Properties getUserParams() 230 { 231 Properties p = new Properties (); 232 p.putAll(params); 233 p.remove(PROPERTY_ENDPOINT_NAME); 234 p.remove(PROPERTY_ENDPOINT_URI); 235 p.remove(PROPERTY_CREATE_CONNECTOR); 236 p.remove(PROPERTY_TRANSFORMERS); 237 return p; 238 } 239 240 public URI parseServerAuthority() throws URISyntaxException 241 { 242 return uri.parseServerAuthority(); 243 } 244 245 public URI normalize() 246 { 247 return uri.normalize(); 248 } 249 250 public URI resolve(URI uri) 251 { 252 return uri.resolve(uri); 253 } 254 255 public URI resolve(String str) 256 { 257 return uri.resolve(str); 258 } 259 260 public URI relativize(URI uri) 261 { 262 return uri.relativize(uri); 263 } 264 265 public String getScheme() 266 { 267 return uri.getScheme(); 268 } 269 270 public String getFullScheme() 271 { 272 return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo + ':' + uri.getScheme()); 273 274 } 275 276 public boolean isAbsolute() 277 { 278 return uri.isAbsolute(); 279 } 280 281 public boolean isOpaque() 282 { 283 return uri.isOpaque(); 284 } 285 286 public String getRawSchemeSpecificPart() 287 { 288 return uri.getRawSchemeSpecificPart(); 289 } 290 291 public String getSchemeSpecificPart() 292 { 293 return uri.getSchemeSpecificPart(); 294 } 295 296 public String getRawAuthority() 297 { 298 return uri.getRawAuthority(); 299 } 300 301 public String getAuthority() 302 { 303 return uri.getAuthority(); 304 } 305 306 public String getRawUserInfo() 307 { 308 return uri.getRawUserInfo(); 309 } 310 311 public String getUserInfo() 312 { 313 return userInfo; 314 } 315 316 public String getHost() 317 { 318 return uri.getHost(); 319 } 320 321 public int getPort() 322 { 323 return uri.getPort(); 324 } 325 326 public String getRawPath() 327 { 328 return uri.getRawPath(); 329 } 330 331 public String getPath() 332 { 333 return uri.getPath(); 334 } 335 336 public String getRawQuery() 337 { 338 return uri.getRawQuery(); 339 } 340 341 public String getQuery() 342 { 343 return uri.getQuery(); 344 } 345 346 public String getRawFragment() 347 { 348 return uri.getRawFragment(); 349 } 350 351 public String getFragment() 352 { 353 return uri.getFragment(); 354 } 355 356 public String toString() 357 { 358 return uriString; 359 } 360 361 public String getTransformers() 362 { 363 return transformers; 364 } 365 366 public int getCreateConnector() 367 { 368 return createConnector; 369 } 370 371 public URI getUri() 372 { 373 return uri; 374 } 375 376 public String getConnectorName() 377 { 378 return connectorName; 379 } 380 381 public String getSchemeMetaInfo() 382 { 383 return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo); 384 } 385 386 public String getResourceInfo() 387 { 388 return resourceInfo; 389 } 390 391 public String getFilterAddress() 392 { 393 return filterAddress; 394 } 395 396 public void setEndpointName(String name) 397 { 398 endpointName = name; 399 } 400 401 public String getUsername() 402 { 403 if (StringUtils.isNotBlank(userInfo)) 404 { 405 int i = userInfo.indexOf(':'); 406 if (i == -1) 407 { 408 return userInfo; 409 } 410 else 411 { 412 return userInfo.substring(0, i); 413 } 414 } 415 return null; 416 } 417 418 public String getResponseTransformers() 419 { 420 return responseTransformers; 421 } 422 423 public String getPassword() 424 { 425 if (StringUtils.isNotBlank(userInfo)) 426 { 427 int i = userInfo.indexOf(':'); 428 if (i > -1) 429 { 430 return userInfo.substring(i + 1); 431 } 432 } 433 return null; 434 } 435 436 public boolean equals(Object o) 437 { 438 if (this == o) 439 { 440 return true; 441 } 442 if (!(o instanceof MuleEndpointURI)) 443 { 444 return false; 445 } 446 447 final MuleEndpointURI muleEndpointURI = (MuleEndpointURI)o; 448 449 if (createConnector != muleEndpointURI.createConnector) 450 { 451 return false; 452 } 453 if (address != null ? !address.equals(muleEndpointURI.address) : muleEndpointURI.address != null) 454 { 455 return false; 456 } 457 if (connectorName != null 458 ? !connectorName.equals(muleEndpointURI.connectorName) 459 : muleEndpointURI.connectorName != null) 460 { 461 return false; 462 } 463 if (endpointName != null 464 ? !endpointName.equals(muleEndpointURI.endpointName) 465 : muleEndpointURI.endpointName != null) 466 { 467 return false; 468 } 469 if (filterAddress != null 470 ? !filterAddress.equals(muleEndpointURI.filterAddress) 471 : muleEndpointURI.filterAddress != null) 472 { 473 return false; 474 } 475 if (params != null ? !params.equals(muleEndpointURI.params) : muleEndpointURI.params != null) 476 { 477 return false; 478 } 479 if (resourceInfo != null 480 ? !resourceInfo.equals(muleEndpointURI.resourceInfo) 481 : muleEndpointURI.resourceInfo != null) 482 { 483 return false; 484 } 485 if (schemeMetaInfo != null 486 ? !schemeMetaInfo.equals(muleEndpointURI.schemeMetaInfo) 487 : muleEndpointURI.schemeMetaInfo != null) 488 { 489 return false; 490 } 491 if (transformers != null 492 ? !transformers.equals(muleEndpointURI.transformers) 493 : muleEndpointURI.transformers != null) 494 { 495 return false; 496 } 497 if (responseTransformers != null 498 ? !responseTransformers.equals(muleEndpointURI.responseTransformers) 499 : muleEndpointURI.responseTransformers != null) 500 { 501 return false; 502 } 503 if (uri != null ? !uri.equals(muleEndpointURI.uri) : muleEndpointURI.uri != null) 504 { 505 return false; 506 } 507 508 return true; 509 } 510 511 public int hashCode() 512 { 513 int result = (address != null ? address.hashCode() : 0); 514 result = 29 * result + (filterAddress != null ? filterAddress.hashCode() : 0); 515 result = 29 * result + (endpointName != null ? endpointName.hashCode() : 0); 516 result = 29 * result + (connectorName != null ? connectorName.hashCode() : 0); 517 result = 29 * result + (transformers != null ? transformers.hashCode() : 0); 518 result = 29 * result + (responseTransformers != null ? responseTransformers.hashCode() : 0); 519 result = 29 * result + createConnector; 520 result = 29 * result + (params != null ? params.hashCode() : 0); 521 result = 29 * result + (uri != null ? uri.hashCode() : 0); 522 result = 29 * result + (schemeMetaInfo != null ? schemeMetaInfo.hashCode() : 0); 523 return 29 * result + (resourceInfo != null ? resourceInfo.hashCode() : 0); 524 } 525 } 526 | Popular Tags |