1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | 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 | 0 | protected int createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR; |
39 | |
|
40 | |
public UMOEndpointURI build(URI uri) throws MalformedEndpointException |
41 | |
{ |
42 | 0 | Properties props = getPropertiesForURI(uri); |
43 | 0 | String replaceAddress = null; |
44 | |
|
45 | |
|
46 | |
|
47 | 0 | if (address != null) |
48 | |
{ |
49 | 0 | replaceAddress = address; |
50 | 0 | setEndpoint(uri, props); |
51 | 0 | address = replaceAddress; |
52 | |
} |
53 | |
else |
54 | |
{ |
55 | 0 | setEndpoint(uri, props); |
56 | |
} |
57 | |
|
58 | 0 | UMOEndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers, |
59 | |
responseTransformers, createConnector, props, uri, userInfo); |
60 | 0 | address = null; |
61 | 0 | endpointName = null; |
62 | 0 | connectorName = null; |
63 | 0 | transformers = null; |
64 | 0 | responseTransformers = null; |
65 | 0 | createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR; |
66 | 0 | 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 | 0 | Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery()); |
74 | |
|
75 | 0 | String tempEndpointName = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_NAME); |
76 | 0 | if (tempEndpointName != null) |
77 | |
{ |
78 | 0 | this.endpointName = tempEndpointName; |
79 | |
} |
80 | |
|
81 | 0 | String endpoint = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_URI); |
82 | 0 | if (endpoint != null) |
83 | |
{ |
84 | 0 | this.address = endpoint; |
85 | 0 | address = decode(address, uri); |
86 | |
} |
87 | |
|
88 | 0 | String cnnName = (String) properties.get(UMOEndpointURI.PROPERTY_CONNECTOR_NAME); |
89 | 0 | if (cnnName != null) |
90 | |
{ |
91 | 0 | this.connectorName = cnnName; |
92 | |
} |
93 | |
|
94 | 0 | String create = (String) properties.get(UMOEndpointURI.PROPERTY_CREATE_CONNECTOR); |
95 | 0 | if (create != null) |
96 | |
{ |
97 | 0 | if ("0".equals(create)) |
98 | |
{ |
99 | 0 | this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR; |
100 | |
} |
101 | 0 | else if ("1".equals(create)) |
102 | |
{ |
103 | 0 | this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR; |
104 | |
} |
105 | 0 | else if ("2".equals(create)) |
106 | |
{ |
107 | 0 | this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR; |
108 | |
} |
109 | 0 | else if ("IF_NEEDED".equals(create)) |
110 | |
{ |
111 | 0 | this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR; |
112 | |
} |
113 | 0 | else if ("ALWAYS".equals(create)) |
114 | |
{ |
115 | 0 | this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR; |
116 | |
} |
117 | 0 | else if ("NEVER".equals(create)) |
118 | |
{ |
119 | 0 | this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR; |
120 | |
} |
121 | 0 | else if (connectorName == null) |
122 | |
{ |
123 | 0 | this.createConnector = TransportFactory.USE_CONNECTOR; |
124 | 0 | connectorName = create; |
125 | |
} |
126 | |
|
127 | |
} |
128 | |
|
129 | 0 | transformers = (String) properties.get(UMOEndpointURI.PROPERTY_TRANSFORMERS); |
130 | 0 | if (transformers != null) |
131 | |
{ |
132 | 0 | transformers = transformers.replaceAll(" ", ","); |
133 | |
} |
134 | 0 | responseTransformers = (String) properties.get(UMOEndpointURI.PROPERTY_RESPONSE_TRANSFORMERS); |
135 | 0 | if (responseTransformers != null) |
136 | |
{ |
137 | 0 | responseTransformers = responseTransformers.replaceAll(" ", ","); |
138 | |
} |
139 | |
|
140 | |
|
141 | 0 | userInfo = uri.getUserInfo(); |
142 | 0 | if (userInfo != null) |
143 | |
{ |
144 | 0 | userInfo = decode(userInfo, uri); |
145 | |
} |
146 | 0 | return properties; |
147 | |
} |
148 | |
|
149 | |
private String decode(String string, URI uri) throws MalformedEndpointException |
150 | |
{ |
151 | |
try |
152 | |
{ |
153 | 0 | return URLDecoder.decode(string, MuleManager.getConfiguration().getEncoding()); |
154 | |
} |
155 | 0 | catch (UnsupportedEncodingException e) |
156 | |
{ |
157 | 0 | throw new MalformedEndpointException(uri.toString(), e); |
158 | |
} |
159 | |
} |
160 | |
} |