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 | 0 | 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 | 0 | this.uri = uri; |
42 | 0 | Properties props = getPropertiesForURI(uri, muleContext); |
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 | EndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers, |
59 | |
responseTransformers, props, this.uri, userInfo, muleContext); |
60 | 0 | address = null; |
61 | 0 | endpointName = null; |
62 | 0 | connectorName = null; |
63 | 0 | transformers = null; |
64 | 0 | responseTransformers = null; |
65 | 0 | uri = null; |
66 | 0 | return ep; |
67 | |
} |
68 | |
|
69 | |
protected void rewriteURI(URI newURI) |
70 | |
{ |
71 | 0 | this.uri = newURI; |
72 | 0 | } |
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 | 0 | Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery()); |
79 | |
|
80 | 0 | String tempEndpointName = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_NAME); |
81 | 0 | if (tempEndpointName != null) |
82 | |
{ |
83 | 0 | this.endpointName = tempEndpointName; |
84 | |
} |
85 | |
|
86 | 0 | String endpoint = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_URI); |
87 | 0 | if (endpoint != null) |
88 | |
{ |
89 | 0 | this.address = endpoint; |
90 | 0 | address = decode(address, uri, muleContext); |
91 | |
} |
92 | |
|
93 | 0 | String cnnName = (String) properties.get(EndpointURI.PROPERTY_CONNECTOR_NAME); |
94 | 0 | if (cnnName != null) |
95 | |
{ |
96 | 0 | this.connectorName = cnnName; |
97 | |
} |
98 | |
|
99 | 0 | transformers = (String) properties.get(EndpointURI.PROPERTY_TRANSFORMERS); |
100 | 0 | if (transformers != null) |
101 | |
{ |
102 | 0 | transformers = transformers.replaceAll(" ", ","); |
103 | |
} |
104 | 0 | responseTransformers = (String) properties.get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS); |
105 | 0 | if (responseTransformers != null) |
106 | |
{ |
107 | 0 | responseTransformers = responseTransformers.replaceAll(" ", ","); |
108 | |
} |
109 | 0 | userInfo = uri.getUserInfo(); |
110 | 0 | return properties; |
111 | |
} |
112 | |
|
113 | |
private String decode(String string, URI uri, MuleContext context) throws MalformedEndpointException |
114 | |
{ |
115 | |
try |
116 | |
{ |
117 | 0 | String encoding = "UTF-8"; |
118 | 0 | if(context!=null) |
119 | |
{ |
120 | 0 | encoding = context.getConfiguration().getDefaultEncoding(); |
121 | |
} |
122 | 0 | return URLDecoder.decode(string, encoding); |
123 | |
} |
124 | 0 | catch (UnsupportedEncodingException e) |
125 | |
{ |
126 | 0 | throw new MalformedEndpointException(uri.toString(), e); |
127 | |
} |
128 | |
} |
129 | |
} |