1
2
3
4
5
6
7
8
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
26
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
45
46
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
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 userInfo = uri.getUserInfo();
110 return properties;
111 }
112
113 private String decode(String string, URI uri, MuleContext context) throws MalformedEndpointException
114 {
115 try
116 {
117 String encoding = "UTF-8";
118 if(context!=null)
119 {
120 encoding = context.getConfiguration().getDefaultEncoding();
121 }
122 return URLDecoder.decode(string, encoding);
123 }
124 catch (UnsupportedEncodingException e)
125 {
126 throw new MalformedEndpointException(uri.toString(), e);
127 }
128 }
129 }