001: /*
002: * $Id: AbstractEndpointURIBuilder.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.endpoint;
012:
013: import org.mule.api.endpoint.EndpointURI;
014: import org.mule.api.endpoint.EndpointURIBuilder;
015: import org.mule.api.endpoint.MalformedEndpointException;
016: import org.mule.util.PropertiesUtils;
017:
018: import java.io.UnsupportedEncodingException;
019: import java.net.URI;
020: import java.net.URLDecoder;
021: import java.util.Properties;
022:
023: /**
024: * {@link UrlEndpointURIBuilder} is the default endpointUri strategy suitable for
025: * most connectors
026: */
027:
028: public abstract class AbstractEndpointURIBuilder implements
029: EndpointURIBuilder {
030: protected String address;
031: protected String endpointName;
032: protected String connectorName;
033: protected String transformers;
034: protected String responseTransformers;
035: protected String userInfo;
036:
037: public EndpointURI build(URI uri) throws MalformedEndpointException {
038: Properties props = getPropertiesForURI(uri);
039: String replaceAddress = null;
040: //If the address has been set as a parameter on the URI, then we must ensure that that value is used
041: //for the address. We still call the setEndpoint() method so that other information on the URI
042: //is still processed
043: if (address != null) {
044: replaceAddress = address;
045: setEndpoint(uri, props);
046: address = replaceAddress;
047: } else {
048: setEndpoint(uri, props);
049: }
050:
051: EndpointURI ep = new MuleEndpointURI(address, endpointName,
052: connectorName, transformers, responseTransformers,
053: props, uri, userInfo);
054: address = null;
055: endpointName = null;
056: connectorName = null;
057: transformers = null;
058: responseTransformers = null;
059: return ep;
060: }
061:
062: protected abstract void setEndpoint(URI uri, Properties props)
063: throws MalformedEndpointException;
064:
065: protected Properties getPropertiesForURI(URI uri)
066: throws MalformedEndpointException {
067: Properties properties = PropertiesUtils
068: .getPropertiesFromQueryString(uri.getQuery());
069:
070: String tempEndpointName = (String) properties
071: .get(EndpointURI.PROPERTY_ENDPOINT_NAME);
072: if (tempEndpointName != null) {
073: this .endpointName = tempEndpointName;
074: }
075: // override the endpointUri if set
076: String endpoint = (String) properties
077: .get(EndpointURI.PROPERTY_ENDPOINT_URI);
078: if (endpoint != null) {
079: this .address = endpoint;
080: address = decode(address, uri);
081: }
082:
083: String cnnName = (String) properties
084: .get(EndpointURI.PROPERTY_CONNECTOR_NAME);
085: if (cnnName != null) {
086: this .connectorName = cnnName;
087: }
088:
089: transformers = (String) properties
090: .get(EndpointURI.PROPERTY_TRANSFORMERS);
091: if (transformers != null) {
092: transformers = transformers.replaceAll(" ", ",");
093: }
094: responseTransformers = (String) properties
095: .get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
096: if (responseTransformers != null) {
097: responseTransformers = responseTransformers.replaceAll(" ",
098: ",");
099: }
100: // If we have user info, decode it as it might contain '@' or other encodable
101: // characters
102: userInfo = uri.getUserInfo();
103: if (userInfo != null) {
104: userInfo = decode(userInfo, uri);
105: }
106: return properties;
107: }
108:
109: private String decode(String string, URI uri)
110: throws MalformedEndpointException {
111: try {
112: //TODO RM* URGENT:
113: return URLDecoder
114: .decode(string, "UTF-8" /*RegistryContext.getConfiguration().getDefaultEncoding()*/);
115: } catch (UnsupportedEncodingException e) {
116: throw new MalformedEndpointException(uri.toString(), e);
117: }
118: }
119: }
|