View Javadoc

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      protected static final Log logger = LogFactory.getLog(MuleEndpointURI.class);
56  
57      public static boolean isMuleUri(String url)
58      {
59          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      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          this(address, endpointName, connectorName, transformers, responseTransformers, 
84              properties, uri);
85          if (userInfo != null)
86          {
87              this.userInfo = userInfo;
88          }
89      }
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      {
99          this.address = address;
100         this.endpointName = endpointName;
101         this.connectorName = connectorName;
102         this.transformers = transformers;
103         this.responseTransformers = responseTransformers;
104         this.params = properties;
105         this.uri = uri;
106         this.userInfo = uri.getUserInfo();
107         if (properties != null)
108         {
109             resourceInfo = (String) properties.remove("resourceInfo");
110         }
111     }
112 
113     public MuleEndpointURI(EndpointURI endpointUri)
114     {
115         initialise(endpointUri);
116     }
117 
118     public MuleEndpointURI(EndpointURI endpointUri, String filterAddress)
119     {
120         initialise(endpointUri);
121         this.filterAddress = filterAddress;
122     }
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     {
130         uri = uri.trim().replaceAll(" ", "%20");
131         //Allow Expressions to be embedded
132         uri = uri.replaceAll("\\{", "\\[");
133         uri = uri.replaceAll("\\}", "\\]");
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 
155 
156     public void initialise() throws InitialisationException
157     {
158         try
159         {
160             String scheme = (schemeMetaInfo == null ? this.uri.getScheme() : schemeMetaInfo);
161             TransportServiceDescriptor sd;
162             sd = (TransportServiceDescriptor)RegistryContext.getRegistry().lookupServiceDescriptor(ServiceDescriptorFactory.PROVIDER_SERVICE_TYPE, scheme, null);
163             if (sd == null)
164             {
165                 throw new ServiceException(CoreMessages.noServiceTransportDescriptor(scheme));
166             }
167             EndpointURIBuilder builder = sd.createEndpointBuilder();
168             EndpointURI built = builder.build(this.uri);
169             initialise(built);
170         }
171         catch (Exception e)
172         {
173             throw new InitialisationException(e, this);
174         }
175     }
176 
177     private String retrieveSchemeMetaInfo(String url)
178     {
179         int i = url.indexOf(':');
180         if (i == -1)
181         {
182             return null;
183         }
184         if (url.charAt(i + 1) == '/')
185         {
186             return null;
187         }
188         else
189         {
190             return url.substring(0, i);
191         }
192     }
193 
194     protected boolean validateUrl(String url)
195     {
196         return (url.indexOf(":/") > 0);
197     }
198 
199     private void initialise(EndpointURI endpointUri)
200     {
201         this.address = endpointUri.getAddress();
202         if (this.endpointName == null)
203         {
204             this.endpointName = endpointUri.getEndpointName();
205         }
206         this.connectorName = endpointUri.getConnectorName();
207         this.transformers = endpointUri.getTransformers();
208         this.responseTransformers = endpointUri.getResponseTransformers();
209         this.params = endpointUri.getParams();
210         this.uri = endpointUri.getUri();
211         this.resourceInfo = endpointUri.getResourceInfo();
212         this.userInfo = endpointUri.getUserInfo();
213     }
214 
215     public String getAddress()
216     {
217         return address;
218     }
219 
220     public String getEndpointName()
221     {
222         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         if (params.size() == 0 && getQuery() != null)
230         {
231             params = PropertiesUtils.getPropertiesFromQueryString(getQuery());
232         }
233         return params;
234     }
235 
236     public Properties getUserParams()
237     {
238         Properties p = new Properties();
239         p.putAll(getParams());
240         p.remove(PROPERTY_ENDPOINT_NAME);
241         p.remove(PROPERTY_ENDPOINT_URI);
242         p.remove(PROPERTY_TRANSFORMERS);
243         return p;
244     }
245 
246     public URI parseServerAuthority() throws URISyntaxException
247     {
248         return uri.parseServerAuthority();
249     }
250 
251     public URI normalize()
252     {
253         return uri.normalize();
254     }
255 
256     public URI resolve(URI uri)
257     {
258         return uri.resolve(uri);
259     }
260 
261     public URI resolve(String str)
262     {
263         return uri.resolve(str);
264     }
265 
266     public URI relativize(URI uri)
267     {
268         return uri.relativize(uri);
269     }
270 
271     public String getScheme()
272     {
273         return uri.getScheme();
274     }
275 
276     public String getFullScheme()
277     {
278         return (schemeMetaInfo == null ? uri.getScheme() : schemeMetaInfo + ':' + uri.getScheme());
279 
280     }
281 
282     public boolean isAbsolute()
283     {
284         return uri.isAbsolute();
285     }
286 
287     public boolean isOpaque()
288     {
289         return uri.isOpaque();
290     }
291 
292     public String getRawSchemeSpecificPart()
293     {
294         return uri.getRawSchemeSpecificPart();
295     }
296 
297     public String getSchemeSpecificPart()
298     {
299         return uri.getSchemeSpecificPart();
300     }
301 
302     public String getRawAuthority()
303     {
304         return uri.getRawAuthority();
305     }
306 
307     public String getAuthority()
308     {
309         return uri.getAuthority();
310     }
311 
312     public String getRawUserInfo()
313     {
314         return uri.getRawUserInfo();
315     }
316 
317     public String getUserInfo()
318     {
319         return userInfo;
320     }
321 
322     public String getHost()
323     {
324         return uri.getHost();
325     }
326 
327     public int getPort()
328     {
329         return uri.getPort();
330     }
331 
332     public String getRawPath()
333     {
334         return uri.getRawPath();
335     }
336 
337     public String getPath()
338     {
339         return uri.getPath();
340     }
341 
342     public String getRawQuery()
343     {
344         return uri.getRawQuery();
345     }
346 
347     public String getQuery()
348     {
349         return uri.getQuery();
350     }
351 
352     public String getRawFragment()
353     {
354         return uri.getRawFragment();
355     }
356 
357     public String getFragment()
358     {
359         return uri.getFragment();
360     }
361 
362     public String toString()
363     {
364         return uri.toASCIIString();
365     }
366 
367     public String getTransformers()
368     {
369         return transformers;
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 getUser()
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         MuleEndpointURI muleEndpointURI = (MuleEndpointURI) o;
448         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         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 }