View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.endpoint;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.endpoint.EndpointURI;
11  import org.mule.api.endpoint.EndpointURIBuilder;
12  import org.mule.api.endpoint.MalformedEndpointException;
13  import org.mule.util.PropertiesUtils;
14  
15  import java.io.UnsupportedEncodingException;
16  import java.net.URI;
17  import java.net.URLDecoder;
18  import java.util.Properties;
19  
20  /**
21   * {@link UrlEndpointURIBuilder} is the default endpointUri strategy suitable for
22   * most connectors
23   */
24  
25  public abstract class AbstractEndpointURIBuilder implements EndpointURIBuilder
26  {
27      protected String address;
28      protected String endpointName;
29      protected String connectorName;
30      protected String transformers;
31      protected String responseTransformers;
32      protected String userInfo;
33      private URI uri;
34  
35      public EndpointURI build(URI uri, MuleContext muleContext) throws MalformedEndpointException
36      {
37          this.uri = uri;
38          Properties props = getPropertiesForURI(uri, muleContext);
39          String replaceAddress = null;
40          //If the address has been set as a parameter on the URI, then we must ensure that that value is used
41          //for the address. We still call the setEndpoint() method so that other information on the URI
42          //is still processed
43          if (address != null)
44          {
45              replaceAddress = address;
46              setEndpoint(uri, props);
47              address = replaceAddress;
48          }
49          else
50          {
51              setEndpoint(uri, props);
52          }
53  
54          EndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
55              responseTransformers, props, this.uri, userInfo, muleContext);
56          address = null;
57          endpointName = null;
58          connectorName = null;
59          transformers = null;
60          responseTransformers = null;
61          uri = null;
62          return ep;
63      }
64  
65      protected void rewriteURI(URI newURI)
66      {
67          this.uri = newURI;
68      }
69  
70      protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException;
71  
72      protected Properties getPropertiesForURI(URI uri, MuleContext muleContext) throws MalformedEndpointException
73      {
74          Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
75  
76          String tempEndpointName = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_NAME);
77          if (tempEndpointName != null)
78          {
79              this.endpointName = tempEndpointName;
80          }
81          // override the endpointUri if set
82          String endpoint = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_URI);
83          if (endpoint != null)
84          {
85              this.address = endpoint;
86              address = decode(address, uri, muleContext);
87          }
88  
89          String cnnName = (String) properties.get(EndpointURI.PROPERTY_CONNECTOR_NAME);
90          if (cnnName != null)
91          {
92              this.connectorName = cnnName;
93          }
94  
95          transformers = (String) properties.get(EndpointURI.PROPERTY_TRANSFORMERS);
96          if (transformers != null)
97          {
98              transformers = transformers.replaceAll(" ", ",");
99          }
100         responseTransformers = (String) properties.get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
101         if (responseTransformers != null)
102         {
103             responseTransformers = responseTransformers.replaceAll(" ", ",");
104         }
105         
106         userInfo = uri.getUserInfo();
107         return properties;
108     }
109 
110     private String decode(String string, URI uri, MuleContext context) throws MalformedEndpointException
111     {
112         try
113         {
114             String encoding = "UTF-8";
115             if(context!=null)
116             {
117                 encoding = context.getConfiguration().getDefaultEncoding();
118             }
119             return URLDecoder.decode(string, encoding);
120         }
121         catch (UnsupportedEncodingException e)
122         {
123             throw new MalformedEndpointException(uri.toString(), e);
124         }
125     }
126 }