Coverage Report - org.mule.endpoint.MuleEndpointURI
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleEndpointURI
0%
0/167
0%
0/86
0
 
 1  
 /*
 2  
  * $Id: MuleEndpointURI.java 20109 2010-11-07 05:20:30Z mike.schilling $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.endpoint;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.endpoint.EndpointException;
 15  
 import org.mule.api.endpoint.EndpointURI;
 16  
 import org.mule.api.endpoint.EndpointURIBuilder;
 17  
 import org.mule.api.endpoint.MalformedEndpointException;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.api.registry.ServiceException;
 20  
 import org.mule.api.registry.ServiceType;
 21  
 import org.mule.config.i18n.CoreMessages;
 22  
 import org.mule.transport.service.TransportServiceDescriptor;
 23  
 import org.mule.util.ClassUtils;
 24  
 import org.mule.util.PropertiesUtils;
 25  
 import org.mule.util.StringUtils;
 26  
 
 27  
 import java.net.URI;
 28  
 import java.net.URISyntaxException;
 29  
 import java.util.Properties;
 30  
 
 31  
 import org.apache.commons.logging.Log;
 32  
 import org.apache.commons.logging.LogFactory;
 33  
 
 34  
 /**
 35  
  * <code>MuleEndpointURI</code> is used to determine how a message is sent or received. The url 
 36  
  * defines the protocol, the endpointUri destination of the message and optionally the endpoint to 
 37  
  * use when dispatching the event. Mule urls take the form of - 
 38  
  * protocol://[host]:[port]/[provider]/endpointUri or
 39  
  * protocol://[host]:[port]/endpointUri i.e. vm:///my.object 
 40  
  * <br/>
 41  
  * The protocol can be any of any connector registered with Mule. The endpoint name if specified 
 42  
  * must be the name of a registered global endpoint. The endpointUri can be any endpointUri
 43  
  * recognised by the endpoint type.
 44  
  */
 45  
 public class MuleEndpointURI implements EndpointURI
 46  
 {
 47  
     /**
 48  
      * Serial version
 49  
      */
 50  
     private static final long serialVersionUID = 3906735768171252877L;
 51  
 
 52  
     /**
 53  
      * logger used by this class
 54  
      */
 55  0
     protected static final Log logger = LogFactory.getLog(MuleEndpointURI.class);
 56  
 
 57  
     public static boolean isMuleUri(String url)
 58  
     {
 59  0
         return url.indexOf(":/") != -1;
 60  
     }
 61  
 
 62  
     private String address;
 63  
     private String filterAddress;
 64  
     private String endpointName;
 65  
     private String connectorName;
 66  
     private String transformers;
 67  
     private String responseTransformers;
 68  0
     private Properties params = new Properties();
 69  
     private URI uri;
 70  
     private String userInfo;
 71  
     private String schemeMetaInfo;
 72  
     private String resourceInfo;
 73  
     private boolean dynamic;
 74  
     private transient MuleContext muleContext;
 75  
 
 76  
     MuleEndpointURI(String address,
 77  
                     String endpointName,
 78  
                     String connectorName,
 79  
                     String transformers,
 80  
                     String responseTransformers,
 81  
                     Properties properties,
 82  
                     URI uri,
 83  
                     String userInfo, MuleContext muleContext)
 84  
     {
 85  0
         this(address, endpointName, connectorName, transformers, responseTransformers,
 86  
                 properties, uri, muleContext);
 87  0
         if (userInfo != null)
 88  
         {
 89  0
             this.userInfo = userInfo;
 90  
         }
 91  0
     }
 92  
 
 93  
     public MuleEndpointURI(String address,
 94  
                            String endpointName,
 95  
                            String connectorName,
 96  
                            String transformers,
 97  
                            String responseTransformers,
 98  
                            Properties properties,
 99  
                            URI uri, MuleContext muleContext)
 100  0
     {
 101  0
         this.address = address;
 102  0
         this.endpointName = endpointName;
 103  0
         this.connectorName = connectorName;
 104  0
         this.transformers = transformers;
 105  0
         this.responseTransformers = responseTransformers;
 106  0
         this.params = properties;
 107  0
         this.uri = uri;
 108  0
         this.userInfo = uri.getUserInfo();
 109  0
         this.muleContext = muleContext;
 110  0
         if (properties != null)
 111  
         {
 112  0
             resourceInfo = (String) properties.remove("resourceInfo");
 113  
         }
 114  0
     }
 115  
 
 116  
     public MuleEndpointURI(EndpointURI endpointUri)
 117  0
     {
 118  0
         initialise(endpointUri);
 119  0
     }
 120  
 
 121  
     public MuleEndpointURI(EndpointURI endpointUri, String filterAddress)
 122  0
     {
 123  0
         initialise(endpointUri);
 124  0
         this.filterAddress = filterAddress;
 125  0
     }
 126  
 
 127  
     public MuleEndpointURI(String uri, MuleContext muleContext) throws EndpointException
 128  
     {
 129  0
         this(uri, null, muleContext);       
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Creates but does not initialize the endpoint URI.  It is up to the caller
 134  
      * to call initialise() at some point.
 135  
      */
 136  
     public MuleEndpointURI(String uri, String encodedUri, MuleContext muleContext) throws EndpointException
 137  0
     {
 138  0
         this.muleContext = muleContext;
 139  0
         uri = preprocessUri(uri);
 140  0
         String startUri = uri;
 141  0
         uri = convertExpressionDelimiters(uri, "#");
 142  0
         uri = convertExpressionDelimiters(uri, "$");
 143  
 
 144  0
         if (uri.indexOf("#[") >= 0)
 145  
         {
 146  0
             address = uri;
 147  0
             dynamic = true;
 148  
         }
 149  
         else
 150  
         {
 151  
             try
 152  
             {
 153  0
                 this.uri = new URI((encodedUri != null && uri.equals(startUri)) ? preprocessUri(encodedUri) : uri);
 154  
             }
 155  0
             catch (URISyntaxException e)
 156  
             {
 157  0
                 throw new MalformedEndpointException(uri, e);
 158  0
             }
 159  0
             this.userInfo = this.uri.getRawUserInfo();
 160  
         }
 161  0
     }
 162  
 
 163  
     private String convertExpressionDelimiters(String uri, String startChar)
 164  
     {
 165  
         //Allow Expressions to be embedded
 166  0
         int uriLength = uri.length();
 167  0
         for (int index = 0; index < uriLength; )
 168  
         {
 169  0
             index = uri.indexOf(startChar + "{", index);
 170  0
             if (index < 0)
 171  
             {
 172  0
                 break;
 173  
             }
 174  0
             int braceCount = 1;
 175  0
             for (int seek = index + 2; seek < uriLength; seek++)
 176  
             {
 177  0
                 char c = uri.charAt(seek);
 178  0
                 if (c == '{')
 179  
                 {
 180  0
                     braceCount++;
 181  
                 }
 182  0
                 else if (c == '}')
 183  
                 {
 184  0
                     if (--braceCount == 0)
 185  
                     {
 186  0
                         uri = uri.substring(0, index) + startChar + "[" + uri.substring(index + 2, seek) + "]" + uri.substring(seek+1);
 187  0
                         break;
 188  
                     }
 189  
                 }
 190  
             }
 191  0
             index += 2;
 192  0
         }
 193  0
         return uri;
 194  
     }
 195  
 
 196  
     protected String preprocessUri(String uri) throws MalformedEndpointException
 197  
     {
 198  0
         uri = uri.trim().replaceAll(" ", "%20");
 199  0
         if (!validateUrl(uri))
 200  
         {
 201  0
             throw new MalformedEndpointException(uri);
 202  
         }
 203  0
         schemeMetaInfo = retrieveSchemeMetaInfo(uri);
 204  0
         if (schemeMetaInfo != null)
 205  
         {
 206  0
             uri = uri.replaceFirst(schemeMetaInfo + ":", "");
 207  
         }
 208  0
         return uri;
 209  
     }
 210  
 
 211  
     public void initialise() throws InitialisationException
 212  
     {
 213  
         try
 214  
         {
 215  0
             String scheme = getFullScheme();
 216  
             TransportServiceDescriptor sd;
 217  0
             sd = (TransportServiceDescriptor) muleContext.getRegistry().lookupServiceDescriptor(ServiceType.TRANSPORT, scheme, null);
 218  0
             if (sd == null)
 219  
             {
 220  0
                 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
 221  
             }
 222  0
             EndpointURIBuilder builder = sd.createEndpointURIBuilder();
 223  0
             EndpointURI built = builder.build(this.uri, muleContext);
 224  0
             initialise(built);
 225  
         }
 226  0
         catch (Exception e)
 227  
         {
 228  0
             throw new InitialisationException(e, this);
 229  0
         }
 230  0
     }
 231  
 
 232  
     private String retrieveSchemeMetaInfo(String url)
 233  
     {
 234  0
         int i = url.indexOf(':');
 235  0
         if (i == -1)
 236  
         {
 237  0
             return null;
 238  
         }
 239  0
         if (url.charAt(i + 1) == '/')
 240  
         {
 241  0
             return null;
 242  
         }
 243  
         else
 244  
         {
 245  0
             return url.substring(0, i);
 246  
         }
 247  
     }
 248  
 
 249  
     protected boolean validateUrl(String url)
 250  
     {
 251  0
         return (url.indexOf(":/") > 0);
 252  
     }
 253  
 
 254  
     private void initialise(EndpointURI endpointUri)
 255  
     {
 256  0
         this.address = endpointUri.getAddress();
 257  0
         if (this.endpointName == null)
 258  
         {
 259  0
             this.endpointName = endpointUri.getEndpointName();
 260  
         }
 261  0
         this.connectorName = endpointUri.getConnectorName();
 262  0
         this.transformers = endpointUri.getTransformers();
 263  0
         this.responseTransformers = endpointUri.getResponseTransformers();
 264  0
         this.params = endpointUri.getParams();
 265  0
         this.uri = endpointUri.getUri();
 266  0
         this.resourceInfo = endpointUri.getResourceInfo();
 267  0
         this.userInfo = endpointUri.getUserInfo();
 268  0
     }
 269  
 
 270  
     public String getAddress()
 271  
     {
 272  0
         return address;
 273  
     }
 274  
 
 275  
     public String getEndpointName()
 276  
     {
 277  0
         return (StringUtils.isEmpty(endpointName) ? null : endpointName);
 278  
     }
 279  
 
 280  
     public Properties getParams()
 281  
     {
 282  
         // TODO fix this so that the query string properties are not lost.
 283  
         // not sure whats causing this at the moment
 284  0
         if (params.size() == 0 && getQuery() != null)
 285  
         {
 286  0
             params = PropertiesUtils.getPropertiesFromQueryString(getQuery());
 287  
         }
 288  0
         return params;
 289  
     }
 290  
 
 291  
     public Properties getUserParams()
 292  
     {
 293  0
         Properties p = new Properties();
 294  0
         p.putAll(getParams());
 295  0
         p.remove(PROPERTY_ENDPOINT_NAME);
 296  0
         p.remove(PROPERTY_ENDPOINT_URI);
 297  0
         p.remove(PROPERTY_TRANSFORMERS);
 298  0
         return p;
 299  
     }
 300  
 
 301  
     public URI parseServerAuthority() throws URISyntaxException
 302  
     {
 303  0
         return uri.parseServerAuthority();
 304  
     }
 305  
 
 306  
     public URI normalize()
 307  
     {
 308  0
         return uri.normalize();
 309  
     }
 310  
 
 311  
     public URI resolve(URI uri)
 312  
     {
 313  0
         return uri.resolve(uri);
 314  
     }
 315  
 
 316  
     public URI resolve(String str)
 317  
     {
 318  0
         return uri.resolve(str);
 319  
     }
 320  
 
 321  
     public URI relativize(URI uri)
 322  
     {
 323  0
         return uri.relativize(uri);
 324  
     }
 325  
 
 326  
     public String getScheme()
 327  
     {
 328  0
         return uri.getScheme();
 329  
     }
 330  
 
 331  
     public String getFullScheme()
 332  
     {
 333  
         String scheme;
 334  0
         if (dynamic)
 335  
         {
 336  0
             int colon = address.indexOf(':');
 337  0
             scheme = address.substring(0, colon);
 338  0
         }
 339  
         else
 340  
         {
 341  0
             scheme = uri.getScheme();
 342  
         }
 343  0
         return (schemeMetaInfo == null ? scheme : schemeMetaInfo + ':' + scheme);
 344  
     }
 345  
 
 346  
     public boolean isAbsolute()
 347  
     {
 348  0
         return uri.isAbsolute();
 349  
     }
 350  
 
 351  
     public boolean isOpaque()
 352  
     {
 353  0
         return uri.isOpaque();
 354  
     }
 355  
 
 356  
     public String getRawSchemeSpecificPart()
 357  
     {
 358  0
         return uri.getRawSchemeSpecificPart();
 359  
     }
 360  
 
 361  
     public String getSchemeSpecificPart()
 362  
     {
 363  0
         return uri.getSchemeSpecificPart();
 364  
     }
 365  
 
 366  
     public String getRawAuthority()
 367  
     {
 368  0
         return uri.getRawAuthority();
 369  
     }
 370  
 
 371  
     public String getAuthority()
 372  
     {
 373  0
         return uri.getAuthority();
 374  
     }
 375  
 
 376  
     public String getRawUserInfo()
 377  
     {
 378  0
         return uri.getRawUserInfo();
 379  
     }
 380  
 
 381  
     public String getUserInfo()
 382  
     {
 383  0
         return userInfo;
 384  
     }
 385  
 
 386  
     public String getHost()
 387  
     {
 388  0
         return uri.getHost();
 389  
     }
 390  
 
 391  
     public int getPort()
 392  
     {
 393  0
         return uri.getPort();
 394  
     }
 395  
 
 396  
     public String getRawPath()
 397  
     {
 398  0
         return uri.getRawPath();
 399  
     }
 400  
 
 401  
     public String getPath()
 402  
     {
 403  0
         return uri.getPath();
 404  
     }
 405  
 
 406  
     public String getRawQuery()
 407  
     {
 408  0
         return uri.getRawQuery();
 409  
     }
 410  
 
 411  
     public String getQuery()
 412  
     {
 413  0
         return uri.getQuery();
 414  
     }
 415  
 
 416  
     public String getRawFragment()
 417  
     {
 418  0
         return uri.getRawFragment();
 419  
     }
 420  
 
 421  
     public String getFragment()
 422  
     {
 423  0
         return uri.getFragment();
 424  
     }
 425  
 
 426  
     @Override
 427  
     public String toString()
 428  
     {
 429  0
         if (StringUtils.isNotEmpty(userInfo) && (userInfo.indexOf(":") > 0))
 430  
         {
 431  
             // Mask passwords in the logs
 432  0
             String maskinfo = userInfo.substring(0, userInfo.indexOf(":")) + ":****";
 433  0
             return uri.toASCIIString().replace(userInfo, maskinfo);
 434  
         }
 435  0
         return uri.toASCIIString();
 436  
     }
 437  
 
 438  
     public String getTransformers()
 439  
     {
 440  0
         return transformers;
 441  
     }
 442  
 
 443  
     public URI getUri()
 444  
     {
 445  0
         return uri;
 446  
     }
 447  
 
 448  
     public String getConnectorName()
 449  
     {
 450  0
         return connectorName;
 451  
     }
 452  
 
 453  
     public String getSchemeMetaInfo()
 454  
     {
 455  0
         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo);
 456  
     }
 457  
 
 458  
     public String getResourceInfo()
 459  
     {
 460  0
         return resourceInfo;
 461  
     }
 462  
 
 463  
     public String getFilterAddress()
 464  
     {
 465  0
         return filterAddress;
 466  
     }
 467  
 
 468  
     public String getUser()
 469  
     {
 470  0
         if (StringUtils.isNotBlank(userInfo))
 471  
         {
 472  0
             int i = userInfo.indexOf(':');
 473  0
             if (i == -1)
 474  
             {
 475  0
                 return userInfo;
 476  
             }
 477  
             else
 478  
             {
 479  0
                 return userInfo.substring(0, i);
 480  
             }
 481  
         }
 482  0
         return null;
 483  
     }
 484  
 
 485  
     public String getResponseTransformers()
 486  
     {
 487  0
         return responseTransformers;
 488  
     }
 489  
 
 490  
     public String getPassword()
 491  
     {
 492  0
         if (StringUtils.isNotBlank(userInfo))
 493  
         {
 494  0
             int i = userInfo.indexOf(':');
 495  0
             if (i > -1)
 496  
             {
 497  0
                 return userInfo.substring(i + 1);
 498  
             }
 499  
         }
 500  0
         return null;
 501  
     }
 502  
 
 503  
     public MuleContext getMuleContext()
 504  
     {
 505  0
         return muleContext;
 506  
     }
 507  
 
 508  
     public boolean isDynamic()
 509  
     {
 510  0
         return dynamic;
 511  
     }
 512  
 
 513  
     @Override
 514  
     public boolean equals(Object o)
 515  
     {
 516  0
         if (this == o)
 517  
         {
 518  0
             return true;
 519  
         }
 520  0
         if (!(o instanceof MuleEndpointURI))
 521  
         {
 522  0
             return false;
 523  
         }
 524  0
         MuleEndpointURI muleEndpointURI = (MuleEndpointURI) o;
 525  0
         return ClassUtils.equal(address, muleEndpointURI.address) &&
 526  
                 ClassUtils.equal(connectorName, muleEndpointURI.connectorName) &&
 527  
                 ClassUtils.equal(endpointName, muleEndpointURI.endpointName) &&
 528  
                 ClassUtils.equal(filterAddress, muleEndpointURI.filterAddress) &&
 529  
                 ClassUtils.equal(params, muleEndpointURI.params) &&
 530  
                 ClassUtils.equal(resourceInfo, muleEndpointURI.resourceInfo) &&
 531  
                 ClassUtils.equal(schemeMetaInfo, muleEndpointURI.schemeMetaInfo) &&
 532  
                 ClassUtils.equal(transformers, muleEndpointURI.transformers) &&
 533  
                 ClassUtils.equal(responseTransformers, muleEndpointURI.responseTransformers) &&
 534  
                 ClassUtils.equal(uri, muleEndpointURI.uri);
 535  
     }
 536  
 
 537  
     @Override
 538  
     public int hashCode()
 539  
     {
 540  0
         return ClassUtils.hash(new Object[]{
 541  
                 address,
 542  
                 filterAddress,
 543  
                 endpointName,
 544  
                 connectorName,
 545  
                 transformers,
 546  
                 responseTransformers,
 547  
                 params,
 548  
                 uri,
 549  
                 schemeMetaInfo,
 550  
                 resourceInfo
 551  
         });
 552  
     }
 553  
 }