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