View Javadoc

1   /*
2    * $Id: AbstractEndpointBuilder.java 7976 2007-08-21 14:26:13Z 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.umo.endpoint.MalformedEndpointException;
16  import org.mule.umo.endpoint.UMOEndpointURI;
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   * <code>UrlEndpointBuilder</code> is the default endpointUri strategy suitable for
26   * most connectors
27   */
28  
29  public abstract class AbstractEndpointBuilder implements EndpointBuilder
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  
38      protected int createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
39  
40      public UMOEndpointURI build(URI uri) throws MalformedEndpointException
41      {
42          Properties props = getPropertiesForURI(uri);
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          UMOEndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
59              responseTransformers, createConnector, props, uri, userInfo);
60          address = null;
61          endpointName = null;
62          connectorName = null;
63          transformers = null;
64          responseTransformers = null;
65          createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
66          return ep;
67      }
68  
69      protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException;
70  
71      protected Properties getPropertiesForURI(URI uri) throws MalformedEndpointException
72      {
73          Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
74  
75          String tempEndpointName = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_NAME);
76          if (tempEndpointName != null)
77          {
78              this.endpointName = tempEndpointName;
79          }
80          // override the endpointUri if set
81          String endpoint = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_URI);
82          if (endpoint != null)
83          {
84              this.address = endpoint;
85              address = decode(address, uri);
86          }
87  
88          String cnnName = (String) properties.get(UMOEndpointURI.PROPERTY_CONNECTOR_NAME);
89          if (cnnName != null)
90          {
91              this.connectorName = cnnName;
92          }
93  
94          String create = (String) properties.get(UMOEndpointURI.PROPERTY_CREATE_CONNECTOR);
95          if (create != null)
96          {
97              if ("0".equals(create))
98              {
99                  this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
100             }
101             else if ("1".equals(create))
102             {
103                 this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR;
104             }
105             else if ("2".equals(create))
106             {
107                 this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR;
108             }
109             else if ("IF_NEEDED".equals(create))
110             {
111                 this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
112             }
113             else if ("ALWAYS".equals(create))
114             {
115                 this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR;
116             }
117             else if ("NEVER".equals(create))
118             {
119                 this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR;
120             }
121             else if (connectorName == null)
122             {
123                 this.createConnector = TransportFactory.USE_CONNECTOR;
124                 connectorName = create;
125             }
126 
127         }
128 
129         transformers = (String) properties.get(UMOEndpointURI.PROPERTY_TRANSFORMERS);
130         if (transformers != null)
131         {
132             transformers = transformers.replaceAll(" ", ",");
133         }
134         responseTransformers = (String) properties.get(UMOEndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
135         if (responseTransformers != null)
136         {
137             responseTransformers = responseTransformers.replaceAll(" ", ",");
138         }
139         // If we have user info, decode it as it might contain '@' or other encodable
140         // characters
141         userInfo = uri.getUserInfo();
142         if (userInfo != null)
143         {
144             userInfo = decode(userInfo, uri);
145         }
146         return properties;
147     }
148 
149     private String decode(String string, URI uri) throws MalformedEndpointException
150     {
151         try
152         {
153             return URLDecoder.decode(string, MuleManager.getConfiguration().getEncoding());
154         }
155         catch (UnsupportedEncodingException e)
156         {
157             throw new MalformedEndpointException(uri.toString(), e);
158         }
159     }
160 }