1
2
3
4
5
6
7
8
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
25
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
42
43
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
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
109 return URLDecoder.decode(string, "UTF-8"
110 }
111 catch (UnsupportedEncodingException e)
112 {
113 throw new MalformedEndpointException(uri.toString(), e);
114 }
115 }
116 }