Coverage Report - org.mule.endpoint.AbstractEndpointURIBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEndpointURIBuilder
0%
0/44
0%
0/14
3
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.endpoint;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.endpoint.EndpointURI;
 11  
 import org.mule.api.endpoint.EndpointURIBuilder;
 12  
 import org.mule.api.endpoint.MalformedEndpointException;
 13  
 import org.mule.util.PropertiesUtils;
 14  
 
 15  
 import java.io.UnsupportedEncodingException;
 16  
 import java.net.URI;
 17  
 import java.net.URLDecoder;
 18  
 import java.util.Properties;
 19  
 
 20  
 /**
 21  
  * {@link UrlEndpointURIBuilder} is the default endpointUri strategy suitable for
 22  
  * most connectors
 23  
  */
 24  
 
 25  0
 public abstract class AbstractEndpointURIBuilder implements EndpointURIBuilder
 26  
 {
 27  
     protected String address;
 28  
     protected String endpointName;
 29  
     protected String connectorName;
 30  
     protected String transformers;
 31  
     protected String responseTransformers;
 32  
     protected String userInfo;
 33  
     private URI uri;
 34  
 
 35  
     public EndpointURI build(URI uri, MuleContext muleContext) throws MalformedEndpointException
 36  
     {
 37  0
         this.uri = uri;
 38  0
         Properties props = getPropertiesForURI(uri, muleContext);
 39  0
         String replaceAddress = null;
 40  
         //If the address has been set as a parameter on the URI, then we must ensure that that value is used
 41  
         //for the address. We still call the setEndpoint() method so that other information on the URI
 42  
         //is still processed
 43  0
         if (address != null)
 44  
         {
 45  0
             replaceAddress = address;
 46  0
             setEndpoint(uri, props);
 47  0
             address = replaceAddress;
 48  
         }
 49  
         else
 50  
         {
 51  0
             setEndpoint(uri, props);
 52  
         }
 53  
 
 54  0
         EndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
 55  
             responseTransformers, props, this.uri, userInfo, muleContext);
 56  0
         address = null;
 57  0
         endpointName = null;
 58  0
         connectorName = null;
 59  0
         transformers = null;
 60  0
         responseTransformers = null;
 61  0
         uri = null;
 62  0
         return ep;
 63  
     }
 64  
 
 65  
     protected void rewriteURI(URI newURI)
 66  
     {
 67  0
         this.uri = newURI;
 68  0
     }
 69  
 
 70  
     protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException;
 71  
 
 72  
     protected Properties getPropertiesForURI(URI uri, MuleContext muleContext) throws MalformedEndpointException
 73  
     {
 74  0
         Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
 75  
 
 76  0
         String tempEndpointName = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_NAME);
 77  0
         if (tempEndpointName != null)
 78  
         {
 79  0
             this.endpointName = tempEndpointName;
 80  
         }
 81  
         // override the endpointUri if set
 82  0
         String endpoint = (String) properties.get(EndpointURI.PROPERTY_ENDPOINT_URI);
 83  0
         if (endpoint != null)
 84  
         {
 85  0
             this.address = endpoint;
 86  0
             address = decode(address, uri, muleContext);
 87  
         }
 88  
 
 89  0
         String cnnName = (String) properties.get(EndpointURI.PROPERTY_CONNECTOR_NAME);
 90  0
         if (cnnName != null)
 91  
         {
 92  0
             this.connectorName = cnnName;
 93  
         }
 94  
 
 95  0
         transformers = (String) properties.get(EndpointURI.PROPERTY_TRANSFORMERS);
 96  0
         if (transformers != null)
 97  
         {
 98  0
             transformers = transformers.replaceAll(" ", ",");
 99  
         }
 100  0
         responseTransformers = (String) properties.get(EndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
 101  0
         if (responseTransformers != null)
 102  
         {
 103  0
             responseTransformers = responseTransformers.replaceAll(" ", ",");
 104  
         }
 105  
         
 106  0
         userInfo = uri.getUserInfo();
 107  0
         return properties;
 108  
     }
 109  
 
 110  
     private String decode(String string, URI uri, MuleContext context) throws MalformedEndpointException
 111  
     {
 112  
         try
 113  
         {
 114  0
             String encoding = "UTF-8";
 115  0
             if(context!=null)
 116  
             {
 117  0
                 encoding = context.getConfiguration().getDefaultEncoding();
 118  
             }
 119  0
             return URLDecoder.decode(string, encoding);
 120  
         }
 121  0
         catch (UnsupportedEncodingException e)
 122  
         {
 123  0
             throw new MalformedEndpointException(uri.toString(), e);
 124  
         }
 125  
     }
 126  
 }