View Javadoc

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