Coverage Report - org.mule.endpoint.MuleEndpointURI
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleEndpointURI
62%
82/132
53%
34/64
1.56
 
 1  
 /*
 2  
  * $Id: MuleEndpointURI.java 12100 2008-06-19 08:58:48Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.RegistryContext;
 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.ServiceDescriptorFactory;
 20  
 import org.mule.api.registry.ServiceException;
 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 of
 36  
  * received. The url defines the protocol, the endpointUri destination of the message
 37  
  * and optionally the endpoint to use when dispatching the event. Mule urls take the
 38  
  * form of - protocol://[host]:[port]/[provider]/endpointUri or
 39  
  * protocol://[host]:[port]/endpointUri i.e. vm:///my.object or The protocol can be
 40  
  * any of any connector registered with Mule. The endpoint name if specified must be
 41  
  * the name of a register global endpoint The endpointUri can be any endpointUri
 42  
  * recognised by the endpoint type.
 43  
  */
 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  2
     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  1432
     private Properties params = new Properties();
 69  
     private URI uri;
 70  
     private String userInfo;
 71  
     private String schemeMetaInfo;
 72  
     private String resourceInfo;
 73  
 
 74  
     MuleEndpointURI(String address,
 75  
                     String endpointName,
 76  
                     String connectorName,
 77  
                     String transformers,
 78  
                     String responseTransformers,
 79  
                     Properties properties,
 80  
                     URI uri,
 81  
                     String userInfo)
 82  
     {
 83  486
         this(address, endpointName, connectorName, transformers, responseTransformers, 
 84  
             properties, uri);
 85  486
         if (userInfo != null)
 86  
         {
 87  0
             this.userInfo = userInfo;
 88  
         }
 89  486
     }
 90  
 
 91  
     public MuleEndpointURI(String address,
 92  
                            String endpointName,
 93  
                            String connectorName,
 94  
                            String transformers,
 95  
                            String responseTransformers,
 96  
                            Properties properties,
 97  
                            URI uri)
 98  486
     {
 99  486
         this.address = address;
 100  486
         this.endpointName = endpointName;
 101  486
         this.connectorName = connectorName;
 102  486
         this.transformers = transformers;
 103  486
         this.responseTransformers = responseTransformers;
 104  486
         this.params = properties;
 105  486
         this.uri = uri;
 106  486
         this.userInfo = uri.getUserInfo();
 107  486
         if (properties != null)
 108  
         {
 109  486
             resourceInfo = (String) properties.remove("resourceInfo");
 110  
         }
 111  486
     }
 112  
 
 113  
     public MuleEndpointURI(EndpointURI endpointUri)
 114  2
     {
 115  2
         initialise(endpointUri);
 116  2
     }
 117  
 
 118  
     public MuleEndpointURI(EndpointURI endpointUri, String filterAddress)
 119  0
     {
 120  0
         initialise(endpointUri);
 121  0
         this.filterAddress = filterAddress;
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Creates but does not initialize the endpoint URI.  It is up to the caller
 126  
      * to call initialise() at some point.
 127  
      */
 128  
     public MuleEndpointURI(String uri) throws EndpointException
 129  944
     {
 130  944
         uri = uri.trim().replaceAll(" ", "%20");
 131  
         //Allow Expressions to be embedded
 132  944
         uri = uri.replaceAll("\\{", "\\[");
 133  944
         uri = uri.replaceAll("\\}", "\\]");
 134  
 
 135  944
         if (!validateUrl(uri))
 136  
         {
 137  2
             throw new MalformedEndpointException(uri);
 138  
         }
 139  
         try
 140  
         {
 141  942
             schemeMetaInfo = retrieveSchemeMetaInfo(uri);
 142  942
             if (schemeMetaInfo != null)
 143  
             {
 144  2
                 uri = uri.replaceFirst(schemeMetaInfo + ":", "");
 145  
             }
 146  942
             this.uri = new URI(uri);
 147  942
             this.userInfo = this.uri.getRawUserInfo();
 148  
         }
 149  0
         catch (URISyntaxException e)
 150  
         {
 151  0
             throw new MalformedEndpointException(uri, e);
 152  942
         }
 153  942
     }
 154  
 
 155  
 
 156  
     public void initialise() throws InitialisationException
 157  
     {
 158  
         try
 159  
         {
 160  486
             String scheme = (schemeMetaInfo == null ? this.uri.getScheme() : schemeMetaInfo);
 161  
             TransportServiceDescriptor sd;
 162  486
             sd = (TransportServiceDescriptor)RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null);
 163  486
             if (sd == null)
 164  
             {
 165  0
                 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
 166  
             }
 167  486
             EndpointURIBuilder builder = sd.createEndpointBuilder();
 168  486
             EndpointURI built = builder.build(this.uri);
 169  486
             initialise(built);
 170  
         }
 171  0
         catch (Exception e)
 172  
         {
 173  0
             throw new InitialisationException(e, this);
 174  486
         }
 175  486
     }
 176  
 
 177  
     private String retrieveSchemeMetaInfo(String url)
 178  
     {
 179  942
         int i = url.indexOf(':');
 180  942
         if (i == -1)
 181  
         {
 182  0
             return null;
 183  
         }
 184  942
         if (url.charAt(i + 1) == '/')
 185  
         {
 186  940
             return null;
 187  
         }
 188  
         else
 189  
         {
 190  2
             return url.substring(0, i);
 191  
         }
 192  
     }
 193  
 
 194  
     protected boolean validateUrl(String url)
 195  
     {
 196  944
         return (url.indexOf(":/") > 0);
 197  
     }
 198  
 
 199  
     private void initialise(EndpointURI endpointUri)
 200  
     {
 201  488
         this.address = endpointUri.getAddress();
 202  488
         if (this.endpointName == null)
 203  
         {
 204  488
             this.endpointName = endpointUri.getEndpointName();
 205  
         }
 206  488
         this.connectorName = endpointUri.getConnectorName();
 207  488
         this.transformers = endpointUri.getTransformers();
 208  488
         this.responseTransformers = endpointUri.getResponseTransformers();
 209  488
         this.params = endpointUri.getParams();
 210  488
         this.uri = endpointUri.getUri();
 211  488
         this.resourceInfo = endpointUri.getResourceInfo();
 212  488
         this.userInfo = endpointUri.getUserInfo();
 213  488
     }
 214  
 
 215  
     public String getAddress()
 216  
     {
 217  614
         return address;
 218  
     }
 219  
 
 220  
     public String getEndpointName()
 221  
     {
 222  522
         return (StringUtils.isEmpty(endpointName) ? null : endpointName);
 223  
     }
 224  
 
 225  
     public Properties getParams()
 226  
     {
 227  
         // TODO fix this so that the query string properties are not lost.
 228  
         // not sure whats causing this at the moment
 229  1494
         if (params.size() == 0 && getQuery() != null)
 230  
         {
 231  12
             params = PropertiesUtils.getPropertiesFromQueryString(getQuery());
 232  
         }
 233  1494
         return params;
 234  
     }
 235  
 
 236  
     public Properties getUserParams()
 237  
     {
 238  0
         Properties p = new Properties();
 239  0
         p.putAll(getParams());
 240  0
         p.remove(PROPERTY_ENDPOINT_NAME);
 241  0
         p.remove(PROPERTY_ENDPOINT_URI);
 242  0
         p.remove(PROPERTY_TRANSFORMERS);
 243  0
         return p;
 244  
     }
 245  
 
 246  
     public URI parseServerAuthority() throws URISyntaxException
 247  
     {
 248  0
         return uri.parseServerAuthority();
 249  
     }
 250  
 
 251  
     public URI normalize()
 252  
     {
 253  0
         return uri.normalize();
 254  
     }
 255  
 
 256  
     public URI resolve(URI uri)
 257  
     {
 258  0
         return uri.resolve(uri);
 259  
     }
 260  
 
 261  
     public URI resolve(String str)
 262  
     {
 263  0
         return uri.resolve(str);
 264  
     }
 265  
 
 266  
     public URI relativize(URI uri)
 267  
     {
 268  0
         return uri.relativize(uri);
 269  
     }
 270  
 
 271  
     public String getScheme()
 272  
     {
 273  562
         return uri.getScheme();
 274  
     }
 275  
 
 276  
     public String getFullScheme()
 277  
     {
 278  572
         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo + ':' + uri.getScheme());
 279  
 
 280  
     }
 281  
 
 282  
     public boolean isAbsolute()
 283  
     {
 284  0
         return uri.isAbsolute();
 285  
     }
 286  
 
 287  
     public boolean isOpaque()
 288  
     {
 289  0
         return uri.isOpaque();
 290  
     }
 291  
 
 292  
     public String getRawSchemeSpecificPart()
 293  
     {
 294  0
         return uri.getRawSchemeSpecificPart();
 295  
     }
 296  
 
 297  
     public String getSchemeSpecificPart()
 298  
     {
 299  0
         return uri.getSchemeSpecificPart();
 300  
     }
 301  
 
 302  
     public String getRawAuthority()
 303  
     {
 304  0
         return uri.getRawAuthority();
 305  
     }
 306  
 
 307  
     public String getAuthority()
 308  
     {
 309  0
         return uri.getAuthority();
 310  
     }
 311  
 
 312  
     public String getRawUserInfo()
 313  
     {
 314  0
         return uri.getRawUserInfo();
 315  
     }
 316  
 
 317  
     public String getUserInfo()
 318  
     {
 319  1140
         return userInfo;
 320  
     }
 321  
 
 322  
     public String getHost()
 323  
     {
 324  34
         return uri.getHost();
 325  
     }
 326  
 
 327  
     public int getPort()
 328  
     {
 329  34
         return uri.getPort();
 330  
     }
 331  
 
 332  
     public String getRawPath()
 333  
     {
 334  0
         return uri.getRawPath();
 335  
     }
 336  
 
 337  
     public String getPath()
 338  
     {
 339  0
         return uri.getPath();
 340  
     }
 341  
 
 342  
     public String getRawQuery()
 343  
     {
 344  0
         return uri.getRawQuery();
 345  
     }
 346  
 
 347  
     public String getQuery()
 348  
     {
 349  1482
         return uri.getQuery();
 350  
     }
 351  
 
 352  
     public String getRawFragment()
 353  
     {
 354  0
         return uri.getRawFragment();
 355  
     }
 356  
 
 357  
     public String getFragment()
 358  
     {
 359  0
         return uri.getFragment();
 360  
     }
 361  
 
 362  
     public String toString()
 363  
     {
 364  1174
         return uri.toASCIIString();
 365  
     }
 366  
 
 367  
     public String getTransformers()
 368  
     {
 369  970
         return transformers;
 370  
     }
 371  
 
 372  
     public URI getUri()
 373  
     {
 374  1526
         return uri;
 375  
     }
 376  
 
 377  
     public String getConnectorName()
 378  
     {
 379  548
         return connectorName;
 380  
     }
 381  
 
 382  
     public String getSchemeMetaInfo()
 383  
     {
 384  588
         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo);
 385  
     }
 386  
 
 387  
     public String getResourceInfo()
 388  
     {
 389  488
         return resourceInfo;
 390  
     }
 391  
 
 392  
     public String getFilterAddress()
 393  
     {
 394  40
         return filterAddress;
 395  
     }
 396  
 
 397  
     public void setEndpointName(String name)
 398  
     {
 399  0
         endpointName = name;
 400  0
     }
 401  
 
 402  
     public String getUser()
 403  
     {
 404  0
         if (StringUtils.isNotBlank(userInfo))
 405  
         {
 406  0
             int i = userInfo.indexOf(':');
 407  0
             if (i == -1)
 408  
             {
 409  0
                 return userInfo;
 410  
             }
 411  
             else
 412  
             {
 413  0
                 return userInfo.substring(0, i);
 414  
             }
 415  
         }
 416  0
         return null;
 417  
     }
 418  
 
 419  
     public String getResponseTransformers()
 420  
     {
 421  974
         return responseTransformers;
 422  
     }
 423  
 
 424  
     public String getPassword()
 425  
     {
 426  0
         if (StringUtils.isNotBlank(userInfo))
 427  
         {
 428  0
             int i = userInfo.indexOf(':');
 429  0
             if (i > -1)
 430  
             {
 431  0
                 return userInfo.substring(i + 1);
 432  
             }
 433  
         }
 434  0
         return null;
 435  
     }
 436  
 
 437  
     public boolean equals(Object o)
 438  
     {
 439  18
         if (this == o)
 440  
         {
 441  0
             return true;
 442  
         }
 443  18
         if (!(o instanceof MuleEndpointURI))
 444  
         {
 445  0
             return false;
 446  
         }
 447  18
         MuleEndpointURI muleEndpointURI = (MuleEndpointURI) o;
 448  18
         return ClassUtils.equal(address, muleEndpointURI.address) &&
 449  
                 ClassUtils.equal(connectorName, muleEndpointURI.connectorName) &&
 450  
                 ClassUtils.equal(endpointName, muleEndpointURI.endpointName) &&
 451  
                 ClassUtils.equal(filterAddress, muleEndpointURI.filterAddress) &&
 452  
                 ClassUtils.equal(params, muleEndpointURI.params) &&
 453  
                 ClassUtils.equal(resourceInfo, muleEndpointURI.resourceInfo) &&
 454  
                 ClassUtils.equal(schemeMetaInfo, muleEndpointURI.schemeMetaInfo) &&
 455  
                 ClassUtils.equal(transformers, muleEndpointURI.transformers) &&
 456  
                 ClassUtils.equal(responseTransformers, muleEndpointURI.responseTransformers) &&
 457  
                 ClassUtils.equal(uri, muleEndpointURI.uri);
 458  
     }
 459  
 
 460  
     public int hashCode()
 461  
     {
 462  996
         return ClassUtils.hash(new Object[]{
 463  
                 address,
 464  
                 filterAddress,
 465  
                 endpointName,
 466  
                 connectorName,
 467  
                 transformers,
 468  
                 responseTransformers,
 469  
                 params,
 470  
                 uri,
 471  
                 schemeMetaInfo,
 472  
                 resourceInfo
 473  
         });
 474  
     }
 475  
 }