Coverage Report - org.mule.module.ibeans.spi.support.CallOutboundEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
CallOutboundEndpoint
0%
0/44
0%
0/14
0
 
 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.module.ibeans.spi.support;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.annotations.param.Payload;
 14  
 import org.mule.api.endpoint.EndpointBuilder;
 15  
 import org.mule.api.endpoint.EndpointURI;
 16  
 import org.mule.api.endpoint.MalformedEndpointException;
 17  
 import org.mule.api.transport.PropertyScope;
 18  
 import org.mule.config.endpoint.AnnotatedEndpointData;
 19  
 import org.mule.endpoint.MuleEndpointURI;
 20  
 import org.mule.transport.AbstractConnector;
 21  
 import org.mule.transport.service.TransportFactory;
 22  
 import org.mule.util.BeanUtils;
 23  
 import org.mule.util.TemplateParser;
 24  
 import org.mule.util.UriParamFilter;
 25  
 
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.ibeans.annotation.Call;
 32  
 import org.ibeans.annotation.param.HeaderParam;
 33  
 import org.ibeans.api.channel.CHANNEL;
 34  
 
 35  
 /**
 36  
  * <p>
 37  
  * A dynamic outbound endpoint defined when using the {@link Call} annotation. A
 38  
  * CallOutboundEndpoint is generated when the call method has a one or more payloads
 39  
  * defined using {@link Payload} annotation or one or more headers defined using the
 40  
  * {@link HeaderParam} annotation. annotations.
 41  
  * <p/>
 42  
  * <p>
 43  
  * The endpoint scheme is the only part of the URI that cannot be replaced at
 44  
  * runtime.
 45  
  * </p>
 46  
  *
 47  
  * @see CallRequestEndpoint
 48  
  */
 49  
 public class CallOutboundEndpoint extends org.mule.endpoint.DynamicOutboundEndpoint
 50  
 {
 51  
     public static final String NULL_PARAM = "null.param";
 52  
 
 53  
     /**
 54  
      * logger used by this class
 55  
      */
 56  0
     protected transient final Log logger = LogFactory.getLog(CallOutboundEndpoint.class);
 57  
 
 58  
     private static final long serialVersionUID = 1861985949279708638L;
 59  
 
 60  
     //The parser used to parse the @Call uri template
 61  0
     protected TemplateParser parser = TemplateParser.createCurlyBracesStyleParser();
 62  
 
 63  0
     private UriParamFilter filter = new UriParamFilter();
 64  
 
 65  
     public CallOutboundEndpoint(MuleContext context, AnnotatedEndpointData epData) throws MalformedEndpointException
 66  
     {
 67  0
         super(createBuilder(context, epData), epData.getAddress());
 68  0
     }
 69  
 
 70  
     private synchronized static EndpointBuilder createBuilder(MuleContext context, AnnotatedEndpointData epData)
 71  
     {
 72  
         try
 73  
         {
 74  0
             String address = epData.getAddress();
 75  0
             int i = address.indexOf(":/");
 76  
             String scheme;
 77  0
             if (i > -1)
 78  
             {
 79  0
                 scheme = address.substring(0, i);
 80  0
                 address = scheme + "://dynamic";
 81  
                 //This is used for creating the connector, since we don't know if the actual URI address is a vaild URI.
 82  0
                 EndpointURI tempUri = new MuleEndpointURI(address, context);
 83  0
                 AbstractConnector cnn = null;
 84  
 
 85  0
                 if (epData.getConnectorName() != null)
 86  
                 {
 87  0
                     cnn = (AbstractConnector) context.getRegistry().lookupConnector(epData.getConnectorName());
 88  
                 }
 89  0
                 if (cnn == null)
 90  
                 {
 91  0
                     cnn = (AbstractConnector) new TransportFactory(context).createConnector(tempUri);
 92  0
                     if (epData.getConnectorName() != null)
 93  
                     {
 94  0
                         cnn.setName(epData.getConnectorName());
 95  
                     }
 96  0
                     context.getRegistry().registerConnector(cnn);
 97  
                 }
 98  
 
 99  
                 //This allows connector properties to be set as properties on the endpoint
 100  0
                 Map props = epData.getProperties();
 101  0
                 if (props == null)
 102  
                 {
 103  0
                     props = new HashMap();
 104  
                 }
 105  
                 else
 106  
                 {
 107  0
                     BeanUtils.populateWithoutFail(cnn, props, false);
 108  
                 }
 109  0
                 EndpointBuilder builder = context.getEndpointFactory().getEndpointBuilder(address);
 110  0
                 builder.setConnector(cnn);
 111  0
                 builder.setName(epData.getName());
 112  0
                 builder.setProperties(props);
 113  
 
 114  0
                 return builder;
 115  
 
 116  
 
 117  
             }
 118  
             else
 119  
             {
 120  0
                 throw new IllegalArgumentException("When defining a dynamic endpoint the endpoint scheme must be set i.e. http://{dynamic}");
 121  
             }
 122  
         }
 123  0
         catch (Exception e)
 124  
         {
 125  0
             throw new RuntimeException(e);
 126  
         }
 127  
     }
 128  
 
 129  
     @Override
 130  
     protected void validateUriTemplate(String uri) throws MalformedEndpointException
 131  
     {
 132  
         //TODO
 133  0
     }
 134  
 
 135  
     @Override
 136  
     protected String parseURIString(String uri, MuleMessage message)
 137  
     {
 138  
         //We do additional processing here to parse the URI template
 139  0
         Map<String, Object> props = getPropertiesForUriTemplate(message);
 140  
 
 141  0
         String newUriString = parser.parse(props, uri);
 142  
         //Remove optional params completely if null
 143  0
         newUriString = filter.filterParamsByValue(newUriString, NULL_PARAM);
 144  
 
 145  0
         return super.parseURIString(newUriString, message);
 146  
     }
 147  
 
 148  
     protected Map<String, Object> getPropertiesForUriTemplate(MuleMessage message)
 149  
     {
 150  0
         Map<String, Object> props = (Map) message.getOutboundProperty(CHANNEL.URI_PARAM_PROPERTIES);
 151  0
         if (props == null)
 152  
         {
 153  0
             throw new IllegalStateException(CHANNEL.URI_PARAM_PROPERTIES + " not set on message");
 154  
         }
 155  0
         return props;
 156  
     }
 157  
 
 158  
     @Override
 159  
     public MuleEvent process(MuleEvent event) throws MuleException
 160  
     {
 161  0
         MuleEvent result = super.process(event);
 162  0
         if (result != null)
 163  
         {
 164  0
             result.getMessage().setProperty(CHANNEL.CALL_URI_PROPERTY, result.getEndpoint().getEndpointURI().toString(), PropertyScope.OUTBOUND);
 165  
         }
 166  0
         return result;
 167  
     }
 168  
 }