use of com.damnhandy.uri.template.impl.ExpressionParseException in project wso2-synapse by wso2.
the class HTTPEndpoint method processUrlTemplate.
private void processUrlTemplate(MessageContext synCtx) throws ExpressionParseException {
Map<String, Object> variables = new HashMap<String, Object>();
/*The properties with uri.var.* are only considered for Outbound REST Endpoints*/
Set propertySet = synCtx.getPropertyKeySet();
// We have to create a local UriTemplate object, or else the UriTemplate.set(variables) call will fill up a list of variables and since uriTemplate
// is not thread safe, this list won't be clearing.
UriTemplate template = null;
String evaluatedUri = "";
// legacySupport for backward compatibility where URI Template decoding handled via HTTPEndpoint
if (legacySupport) {
for (Object propertyKey : propertySet) {
if (propertyKey.toString() != null && (propertyKey.toString().startsWith(RESTConstants.REST_URI_VARIABLE_PREFIX) || propertyKey.toString().startsWith(RESTConstants.REST_QUERY_PARAM_PREFIX))) {
Object objProperty = synCtx.getProperty(propertyKey.toString());
if (objProperty != null) {
if (objProperty instanceof String) {
variables.put(propertyKey.toString(), decodeString((String) synCtx.getProperty(propertyKey.toString())));
} else {
variables.put(propertyKey.toString(), decodeString(String.valueOf(synCtx.getProperty(propertyKey.toString()))));
}
}
}
}
// Include properties defined at endpoint.
Iterator endpointProperties = getProperties().iterator();
while (endpointProperties.hasNext()) {
MediatorProperty property = (MediatorProperty) endpointProperties.next();
if (property.getName().toString() != null && (property.getName().toString().startsWith(RESTConstants.REST_URI_VARIABLE_PREFIX) || property.getName().toString().startsWith(RESTConstants.REST_QUERY_PARAM_PREFIX))) {
variables.put(property.getName(), decodeString((String) property.getValue()));
}
}
template = UriTemplate.fromTemplate(uriTemplate.getTemplate());
if (template != null) {
template.set(variables);
}
if (variables.isEmpty()) {
evaluatedUri = template.getTemplate();
} else {
try {
// Decode needs to avoid replacing special characters(e.g %20 -> %2520) when creating URL.
String decodedString = URLDecoder.decode(template.expand(), "UTF-8");
URL url = new URL(decodedString);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), // this to avoid url.toURI which causes exceptions
url.getRef());
evaluatedUri = uri.toURL().toString();
if (log.isDebugEnabled()) {
log.debug("Expanded URL : " + evaluatedUri);
}
} catch (URISyntaxException e) {
if (log.isDebugEnabled()) {
log.debug("Invalid URL syntax for HTTP Endpoint: " + this.getName(), e);
}
evaluatedUri = template.getTemplate();
} catch (ExpressionParseException e) {
log.debug("No URI Template variables defined in HTTP Endpoint: " + this.getName());
evaluatedUri = template.getTemplate();
} catch (MalformedURLException e) {
log.debug("Invalid URL for HTTP Endpoint: " + this.getName());
evaluatedUri = template.getTemplate();
} catch (UnsupportedEncodingException e) {
log.debug("Exception while decoding the URL in HTTP Endpoint: " + this.getName());
evaluatedUri = template.getTemplate();
}
}
} else {
// URI Template encoding not handled by HTTP Endpoint, compliant with RFC6570
for (Object propertyKey : propertySet) {
if (propertyKey.toString() != null && (propertyKey.toString().startsWith(RESTConstants.REST_URI_VARIABLE_PREFIX) || propertyKey.toString().startsWith(RESTConstants.REST_QUERY_PARAM_PREFIX))) {
Object objProperty = synCtx.getProperty(propertyKey.toString());
if (objProperty != null) {
if (objProperty instanceof String) {
variables.put(propertyKey.toString(), (String) synCtx.getProperty(propertyKey.toString()));
} else {
variables.put(propertyKey.toString(), (String) String.valueOf(synCtx.getProperty(propertyKey.toString())));
}
}
}
}
// Include properties defined at endpoint.
Iterator endpointProperties = getProperties().iterator();
while (endpointProperties.hasNext()) {
MediatorProperty property = (MediatorProperty) endpointProperties.next();
if (property.getName().toString() != null && (property.getName().toString().startsWith(RESTConstants.REST_URI_VARIABLE_PREFIX) || property.getName().toString().startsWith(RESTConstants.REST_QUERY_PARAM_PREFIX))) {
variables.put(property.getName(), (String) property.getValue());
}
}
String tmpl;
// this was used in connectors Eg:- uri-template="{uri.var.variable}"
if (uriTemplate.getTemplate().charAt(0) == '{' && uriTemplate.getTemplate().charAt(1) != '+') {
tmpl = "{+" + uriTemplate.getTemplate().substring(1);
} else {
tmpl = uriTemplate.getTemplate();
}
template = UriTemplate.fromTemplate(tmpl);
if (template != null) {
template.set(variables);
}
if (variables.isEmpty()) {
evaluatedUri = template.getTemplate();
} else {
try {
URI uri = new URI(template.expand());
evaluatedUri = uri.toString();
if (log.isDebugEnabled()) {
log.debug("Expanded URL : " + evaluatedUri);
}
} catch (URISyntaxException e) {
if (log.isDebugEnabled()) {
log.debug("Invalid URL syntax for HTTP Endpoint: " + this.getName(), e);
}
evaluatedUri = template.getTemplate();
} catch (ExpressionParseException e) {
log.debug("No URI Template variables defined in HTTP Endpoint: " + this.getName());
evaluatedUri = template.getTemplate();
}
}
}
if (evaluatedUri != null) {
synCtx.setTo(new EndpointReference(evaluatedUri));
if (super.getDefinition() != null) {
synCtx.setProperty(EndpointDefinition.DYNAMIC_URL_VALUE, evaluatedUri);
}
}
}
Aggregations