Coverage Report - org.mule.endpoint.AbstractEndpointURIBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEndpointURIBuilder
70%
26/37
58%
7/12
3.25
 
 1  
 /*
 2  
  * $Id: AbstractEndpointURIBuilder.java 11898 2008-06-02 19:51:48Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 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  
  * {@link UrlEndpointURIBuilder} is the default endpointUri strategy suitable for
 25  
  * most connectors
 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  
         //If the address has been set as a parameter on the URI, then we must ensure that that value is used
 42  
         //for the address. We still call the setEndpoint() method so that other information on the URI
 43  
         //is still processed
 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  
         // override the endpointUri if set
 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  
             //TODO RM* URGENT:
 109  0
             return URLDecoder.decode(string, "UTF-8" /*RegistryContext.getConfiguration().getDefaultEncoding()*/);
 110  
         }
 111  0
         catch (UnsupportedEncodingException e)
 112  
         {
 113  0
             throw new MalformedEndpointException(uri.toString(), e);
 114  
         }
 115  
     }
 116  
 }