View Javadoc

1   /*
2    * $Id: AbstractEndpointURIBuilder.java 11898 2008-06-02 19:51:48Z dfeist $
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.api.endpoint.EndpointURI;
14  import org.mule.api.endpoint.EndpointURIBuilder;
15  import org.mule.api.endpoint.MalformedEndpointException;
16  import org.mule.util.PropertiesUtils;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.net.URI;
20  import java.net.URLDecoder;
21  import java.util.Properties;
22  
23  /**
24   * {@link UrlEndpointURIBuilder} is the default endpointUri strategy suitable for
25   * most connectors
26   */
27  
28  public abstract class AbstractEndpointURIBuilder implements EndpointURIBuilder
29  {
30      protected String address;
31      protected String endpointName;
32      protected String connectorName;
33      protected String transformers;
34      protected String responseTransformers;
35      protected String userInfo;
36  
37      public EndpointURI build(URI uri) throws MalformedEndpointException
38      {
39          Properties props = getPropertiesForURI(uri);
40          String replaceAddress = null;
41          //If the address has been set as a parameter on the URI, then we must ensure that that value is used
42          //for the address. We still call the setEndpoint() method so that other information on the URI
43          //is still processed
44          if (address != null)
45          {
46              replaceAddress = address;
47              setEndpoint(uri, props);
48              address = replaceAddress;
49          }
50          else
51          {
52              setEndpoint(uri, props);
53          }
54  
55          EndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
56              responseTransformers, props, uri, userInfo);
57          address = null;
58          endpointName = null;
59          connectorName = null;
60          transformers = null;
61          responseTransformers = null;
62          return ep;
63      }
64  
65      protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException;
66  
67      protected Properties getPropertiesForURI(URI uri) throws MalformedEndpointException
68      {
69          Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
70  
71          String tempEndpointName = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_NAME);
72          if (tempEndpointName != null)
73          {
74              this.endpointName = tempEndpointName;
75          }
76          // override the endpointUri if set
77          String endpoint = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_URI);
78          if (endpoint != null)
79          {
80              this.address = endpoint;
81              address = decode(address, uri);
82          }
83  
84          String cnnName = (String) properties.get(EndpointURI.PROPERTY_CONNECTOR_NAME);
85          if (cnnName != null)
86          {
87              this.connectorName = cnnName;
88          }
89  
90          transformers = (String) properties.get(EndpointURI.PROPERTY_TRANSFORMERS);
91          if (transformers != null)
92          {
93              transformers = transformers.replaceAll(" ", ",");
94          }
95          responseTransformers = (String) properties.get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
96          if (responseTransformers != null)
97          {
98              responseTransformers = responseTransformers.replaceAll(" ", ",");
99          }
100         userInfo = uri.getUserInfo();
101         return properties;
102     }
103 
104     private String decode(String string, URI uri) throws MalformedEndpointException
105     {
106         try
107         {
108             //TODO RM* URGENT:
109             return URLDecoder.decode(string, "UTF-8" /*RegistryContext.getConfiguration().getDefaultEncoding()*/);
110         }
111         catch (UnsupportedEncodingException e)
112         {
113             throw new MalformedEndpointException(uri.toString(), e);
114         }
115     }
116 }