Coverage Report - org.mule.impl.endpoint.AbstractEndpointBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEndpointBuilder
0%
0/58
0%
0/15
5.5
 
 1  
 /*
 2  
  * $Id: AbstractEndpointBuilder.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.impl.endpoint;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.providers.service.TransportFactory;
 15  
 import org.mule.umo.endpoint.MalformedEndpointException;
 16  
 import org.mule.umo.endpoint.UMOEndpointURI;
 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  
  * <code>UrlEndpointBuilder</code> is the default endpointUri strategy suitable for
 26  
  * most connectors
 27  
  */
 28  
 
 29  0
 public abstract class AbstractEndpointBuilder implements EndpointBuilder
 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  
 
 38  0
     protected int createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
 39  
 
 40  
     public UMOEndpointURI build(URI uri) throws MalformedEndpointException
 41  
     {
 42  0
         Properties props = getPropertiesForURI(uri);
 43  0
         String replaceAddress = null;
 44  
         //If the address has been set as a parameter on the URI, then we must ensure that that value is used
 45  
         //for the address. We still call the setEndpoint() method so that other information on the URI
 46  
         //is still processed
 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
         UMOEndpointURI ep = new MuleEndpointURI(address, endpointName, connectorName, transformers,
 59  
             responseTransformers, createConnector, props, uri, userInfo);
 60  0
         address = null;
 61  0
         endpointName = null;
 62  0
         connectorName = null;
 63  0
         transformers = null;
 64  0
         responseTransformers = null;
 65  0
         createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
 66  0
         return ep;
 67  
     }
 68  
 
 69  
     protected abstract void setEndpoint(URI uri, Properties props) throws MalformedEndpointException;
 70  
 
 71  
     protected Properties getPropertiesForURI(URI uri) throws MalformedEndpointException
 72  
     {
 73  0
         Properties properties = PropertiesUtils.getPropertiesFromQueryString(uri.getQuery());
 74  
 
 75  0
         String tempEndpointName = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_NAME);
 76  0
         if (tempEndpointName != null)
 77  
         {
 78  0
             this.endpointName = tempEndpointName;
 79  
         }
 80  
         // override the endpointUri if set
 81  0
         String endpoint = (String) properties.get(UMOEndpointURI.PROPERTY_ENDPOINT_URI);
 82  0
         if (endpoint != null)
 83  
         {
 84  0
             this.address = endpoint;
 85  0
             address = decode(address, uri);
 86  
         }
 87  
 
 88  0
         String cnnName = (String) properties.get(UMOEndpointURI.PROPERTY_CONNECTOR_NAME);
 89  0
         if (cnnName != null)
 90  
         {
 91  0
             this.connectorName = cnnName;
 92  
         }
 93  
 
 94  0
         String create = (String) properties.get(UMOEndpointURI.PROPERTY_CREATE_CONNECTOR);
 95  0
         if (create != null)
 96  
         {
 97  0
             if ("0".equals(create))
 98  
             {
 99  0
                 this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
 100  
             }
 101  0
             else if ("1".equals(create))
 102  
             {
 103  0
                 this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR;
 104  
             }
 105  0
             else if ("2".equals(create))
 106  
             {
 107  0
                 this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR;
 108  
             }
 109  0
             else if ("IF_NEEDED".equals(create))
 110  
             {
 111  0
                 this.createConnector = TransportFactory.GET_OR_CREATE_CONNECTOR;
 112  
             }
 113  0
             else if ("ALWAYS".equals(create))
 114  
             {
 115  0
                 this.createConnector = TransportFactory.ALWAYS_CREATE_CONNECTOR;
 116  
             }
 117  0
             else if ("NEVER".equals(create))
 118  
             {
 119  0
                 this.createConnector = TransportFactory.NEVER_CREATE_CONNECTOR;
 120  
             }
 121  0
             else if (connectorName == null)
 122  
             {
 123  0
                 this.createConnector = TransportFactory.USE_CONNECTOR;
 124  0
                 connectorName = create;
 125  
             }
 126  
 
 127  
         }
 128  
 
 129  0
         transformers = (String) properties.get(UMOEndpointURI.PROPERTY_TRANSFORMERS);
 130  0
         if (transformers != null)
 131  
         {
 132  0
             transformers = transformers.replaceAll(" ", ",");
 133  
         }
 134  0
         responseTransformers = (String) properties.get(UMOEndpointURI.PROPERTY_RESPONSE_TRANSFORMERS);
 135  0
         if (responseTransformers != null)
 136  
         {
 137  0
             responseTransformers = responseTransformers.replaceAll(" ", ",");
 138  
         }
 139  
         // If we have user info, decode it as it might contain '@' or other encodable
 140  
         // characters
 141  0
         userInfo = uri.getUserInfo();
 142  0
         if (userInfo != null)
 143  
         {
 144  0
             userInfo = decode(userInfo, uri);
 145  
         }
 146  0
         return properties;
 147  
     }
 148  
 
 149  
     private String decode(String string, URI uri) throws MalformedEndpointException
 150  
     {
 151  
         try
 152  
         {
 153  0
             return URLDecoder.decode(string, MuleManager.getConfiguration().getEncoding());
 154  
         }
 155  0
         catch (UnsupportedEncodingException e)
 156  
         {
 157  0
             throw new MalformedEndpointException(uri.toString(), e);
 158  
         }
 159  
     }
 160  
 }