View Javadoc

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