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 | 486 | 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 | 486 | Properties props = getPropertiesForURI(uri); |
40 | 486 | String replaceAddress = null; |
41 | |
|
42 | |
|
43 | |
|
44 | 486 | if (address != null) |
45 | |
{ |
46 | 0 | replaceAddress = address; |
47 | 0 | setEndpoint(uri, props); |
48 | 0 | address = replaceAddress; |
49 | |
} |
50 | |
else |
51 | |
{ |
52 | 486 | setEndpoint(uri, props); |
53 | |
} |
54 | |
|
55 | 486 | EndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers, |
56 | |
responseTransformers, props, uri, userInfo); |
57 | 486 | address = null; |
58 | 486 | endpointName = null; |
59 | 486 | connectorName = null; |
60 | 486 | transformers = null; |
61 | 486 | responseTransformers = null; |
62 | 486 | 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 | 486 | Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery()); |
70 | |
|
71 | 486 | String tempEndpointName = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_NAME); |
72 | 486 | if (tempEndpointName != null) |
73 | |
{ |
74 | 0 | this.endpointName = tempEndpointName; |
75 | |
} |
76 | |
|
77 | 486 | String endpoint = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_URI); |
78 | 486 | if (endpoint != null) |
79 | |
{ |
80 | 0 | this.address = endpoint; |
81 | 0 | address = decode(address, uri); |
82 | |
} |
83 | |
|
84 | 486 | String cnnName = (String) properties.get(EndpointURI.PROPERTY_CONNECTOR_NAME); |
85 | 486 | if (cnnName != null) |
86 | |
{ |
87 | 8 | this.connectorName = cnnName; |
88 | |
} |
89 | |
|
90 | 486 | transformers = (String) properties.get(EndpointURI.PROPERTY_TRANSFORMERS); |
91 | 486 | if (transformers != null) |
92 | |
{ |
93 | 0 | transformers = transformers.replaceAll(" ", ","); |
94 | |
} |
95 | 486 | responseTransformers = (String) properties.get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS); |
96 | 486 | if (responseTransformers != null) |
97 | |
{ |
98 | 0 | responseTransformers = responseTransformers.replaceAll(" ", ","); |
99 | |
} |
100 | 486 | userInfo = uri.getUserInfo(); |
101 | 486 | return properties; |
102 | |
} |
103 | |
|
104 | |
private String decode(String string, URI uri) throws MalformedEndpointException |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | |
|
109 | 0 | return URLDecoder.decode(string, "UTF-8" ); |
110 | |
} |
111 | 0 | catch (UnsupportedEncodingException e) |
112 | |
{ |
113 | 0 | throw new MalformedEndpointException(uri.toString(), e); |
114 | |
} |
115 | |
} |
116 | |
} |