Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: AxisMessageDispatcher.java 13051 2008-10-10 21:10:48Z dfeist $
   * --------------------------------------------------------------------------------------
   * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
 
 package org.mule.transport.soap.axis;
 
 
 import java.util.List;
 import java.util.Map;
 
 
 import  org.apache.axis.AxisProperties;
 import  org.apache.axis.EngineConfiguration;
 import  org.apache.axis.MessageContext;
 import  org.apache.axis.attachments.AttachmentPart;
 import  org.apache.axis.client.Call;
 import  org.apache.axis.client.Service;
 import  org.apache.axis.configuration.FileProvider;
 import  org.apache.axis.constants.Style;
 import  org.apache.axis.constants.Use;
 import  org.apache.axis.wsdl.fromJava.Namespaces;
 import  org.apache.axis.wsdl.fromJava.Types;

AxisMessageDispatcher is used to make soap requests via the Axis soap client.
 
 {
 
     protected EngineConfiguration clientConfig;
     protected AxisConnector connector;
     protected Service service;
     private Map callParameters;
 
     public AxisMessageDispatcher(OutboundEndpoint endpoint)
     {
         super(endpoint);
         this. = (AxisConnector)endpoint.getConnector();
         AxisProperties.setProperty("axis.doAutoTypes", Boolean.toString(.isDoAutoTypes()));
     }
 
     protected void doConnect() throws Exception
     {
         if ( == null)
         {
              = createService();
         }
     }
 
     protected void doDisconnect() throws Exception
     {
         if ( != null)
         {
              = null;
         }
     }
 
     protected void doDispose()
     {
         // template method
     }
 
     protected synchronized EngineConfiguration getClientConfig(ImmutableEndpoint endpoint)
     {
         if ( == null)
         {
             // Allow the client config to be set on the endpoint
            String config;
            config = (String)endpoint.getProperty(.);
            if (config != null)
            {
                 = new FileProvider(config);
            }
            else
            {
                 = .getClientProvider();
            }
        }
        return ;
    }
    protected Service createService(ImmutableEndpoint endpointthrows Exception
    {
        // Create a simple axis service without wsdl
        EngineConfiguration config = getClientConfig(endpoint);
        return new Service(config);
    }
    protected void doDispatch(MuleEvent eventthrows Exception
    {
        Object[] args = getArgs(event);
        Call call = getCall(eventargs);
        // dont use invokeOneWay here as we are already in a thread pool.
        // Axis creates a new thread for every invoke one way call. nasty!
        // Mule overides the default Axis HttpSender to return immediately if
        // the axis.one.way property is set
        call.setProperty("axis.one.way".);
        call.setProperty(.event);
        call.invoke(args);
    }
    protected MuleMessage doSend(MuleEvent eventthrows Exception
    {
        Call call;
        Object result;
        Object[] args = getArgs(event);
        call = getCall(eventargs);
        result = call.invoke(args);
        if (result == null)
        {
            return null;
        }
        else
        {
            MuleMessage resultMessage = new DefaultMuleMessage(resultevent.getMessage());
            setMessageContextProperties(resultMessagecall.getMessageContext());
            return resultMessage;
        }
    }
    protected Call getCall(MuleEvent eventObject[] argsthrows Exception
    {
        EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
        Object method = getInitialMethod(event); // changes object state
        Call call = (Call) .createCall();
        parseStyle(eventcall);
        parseUse(eventcall);
        // set properties on the call from the endpoint properties
        BeanUtils.populateWithoutFail(callevent.getEndpoint().getProperties(), false);
        call.setTargetEndpointAddress(endpointUri.getAddress());
        method = refineMethod(eventcallmethod);
        String methodNamespace = call.getOperationName().getNamespaceURI();
        // set Mule event here so that handlers can extract info
        call.setProperty(.event);
        call.setProperty(.event.getEndpoint());
        setCustomProperties(eventcall);
        call.setTimeout(new Integer(event.getTimeout()));
        setUserCredentials(endpointUricall);
        Map methodCalls = (Map)event.getMessage().getProperty(.);
        if (methodCalls == null && !(method instanceof SoapMethod))
        {
            buildSoapMethods(eventcallmethodmethodNamespaceargs);
        }
        setCallParams(calleventcall.getOperationName());
        setSoapAction(eventendpointUricall);
        addAttachments(eventcall);
        return call;
    }
    protected void addAttachments(MuleEvent event, Call call)
    {
        // Add any attachments to the call
        for (Iterator iterator = event.getMessage().getAttachmentNames().iterator(); iterator.hasNext();)
        {
            String name = (String)iterator.next();
            DataHandler dh = event.getMessage().getAttachment(name);
            AttachmentPart part = new AttachmentPart(dh);
            call.addAttachmentPart(part);
        }
    }
    
    protected void setSoapAction(MuleEvent eventEndpointURI endpointUri, Call call)
    {
        // Set custom soap action if set on the event or endpoint
        String soapAction = (String)event.getMessage().getProperty(.);
        if (soapAction != null)
        {
            soapAction = parseSoapAction(soapActioncall.getOperationName(), event);
            call.setSOAPActionURI(soapAction);
            call.setUseSOAPAction(..booleanValue());
        }
        else
        {
            call.setSOAPActionURI(endpointUri.getAddress());
        }
    }
    protected void buildSoapMethods(MuleEvent event, Call callObject methodString methodNamespaceObject[] args)
    {
        List params = new ArrayList();
        for (int i = 0; i < args.lengthi++)
        {
            if (args[i] == null)
            {
                QName qname = call.getTypeMapping().getTypeQName(Object.class);
                params.add("value" + i + ";qname{" + qname.getPrefix() + ":" + qname.getLocalPart() + ":"
                        + qname.getNamespaceURI() + "};in");
            }
            else if (args[iinstanceof DataHandler[])
            {
                params.add("attachments;qname{DataHandler:http://xml.apache.org/xml-soap};in");
                // Convert key/value pairs into the parameters
            }
            else if (args[iinstanceof Map && .isTreatMapAsNamedParams())
            {
                for (Iterator iterator = ((Map)args[i]).entrySet().iterator(); iterator.hasNext();)
                {
                    Map.Entry entry = (Map.Entry)iterator.next();
                    if (call.getTypeMapping().getTypeQName(entry.getValue().getClass()) != null)
                    {
                        QName type = call.getTypeMapping().getTypeQName(entry.getValue().getClass());
                        params.add("qname{" + entry.getKey().toString()
                                + (methodNamespace == null ? "" : ":" + methodNamespace) + "};qname{"
                                + type.getPrefix() + ":" + type.getLocalPart() + ":"
                                + type.getNamespaceURI() + "};in");
                    }
                    else
                    {
                        params.add("value" + i + ";qname{"
                                + Types.getLocalNameFromFullName(args[i].getClass().getName()) + ":"
                                + Namespaces.makeNamespace(args[i].getClass().getName()) + "};in");
                        params.add("qname{" + entry.getKey().toString()
                                + (methodNamespace == null ? "" : ":" + methodNamespace) + "};qname{"
                                + Types.getLocalNameFromFullName(args[i].getClass().getName()) + ":"
                                + Namespaces.makeNamespace(args[i].getClass().getName()) + "};in");
                    }
                }
            }
            else if (call.getTypeMapping().getTypeQName(args[i].getClass()) != null)
            {
                QName qname = call.getTypeMapping().getTypeQName(args[i].getClass());
                params.add("value" + i + ";qname{" + qname.getPrefix() + ":" + qname.getLocalPart() + ":"
                        + qname.getNamespaceURI() + "};in");
            }
            else
            {
                params.add("value" + i + ";qname{"
                        + Types.getLocalNameFromFullName(args[i].getClass().getName()) + ":"
                        + Namespaces.makeNamespace(args[i].getClass().getName()) + "};in");
            }
        }
        HashMap map = new HashMap();
        map.put(methodparams);
        event.getMessage().setProperty(.map);
    }
    protected void setUserCredentials(EndpointURI endpointUri, Call call)
    {
        if (endpointUri.getUserInfo() != null)
        {
            call.setUsername(endpointUri.getUser());
            call.setPassword(endpointUri.getPassword());
        }
    }
    protected void setCustomProperties(MuleEvent event, Call call)
    {
        for (Iterator iter = event.getMessage().getPropertyNames().iterator(); iter.hasNext();)
        {
            String key = (String)iter.next();
            if (!(key.startsWith(.)))
            {
                Object value = event.getMessage().getProperty(key);
                if (value != null)
                {
                    call.setProperty(keyvalue);
                }
            }
        }
    }
    protected Object refineMethod(MuleEvent event, Call callObject method)
    {
        if (method instanceof String)
        {
            // Set a custome method namespace if one is set. This will be used forthe
            // parameters too
            String methodNamespace = (String)event.getMessage().getProperty(.);
            if (methodNamespace != null)
            {
                call.setOperationName(new QName(methodNamespacemethod.toString()));
            }
            else
            {
                call.setOperationName(new QName(method.toString()));
            }
        }
        else if (method instanceof QName)
        {
            call.setOperationName((QName)method);
            method = ((QName)method).getLocalPart();
        }
        else
        {
            call.setOperationName(((SoapMethod)method).getName());
        }
        return method;
    }
    protected void parseUse(MuleEvent event, Call call)
    {
        // Set use: Endcoded/Literal
        String use = event.getMessage().getStringProperty(.null);
        if (use != null)
        {
            Use u = Use.getUse(use);
            if (u == null)
            {
                throw new IllegalArgumentException(
                        CoreMessages.valueIsInvalidFor(use.).toString());
            }
            else
            {
                call.setOperationUse(u);
            }
        }
    }
    protected void parseStyle(MuleEvent event, Call call)
    {
        // Note that Axis has specific rules to how these two variables are
        // combined. This is handled for us
        // Set style: RPC/wrapped/Doc/Message
        String style = event.getMessage().getStringProperty(.null);
        if (style != null)
        {
            Style s = Style.getStyle(style);
            if (s == null)
            {
                throw new IllegalArgumentException(
                        CoreMessages.valueIsInvalidFor(style.).toString());
            }
            else
            {
                call.setOperationStyle(s);
            }
        }
    }
    protected Object getInitialMethod(MuleEvent eventthrows DispatchException
    {
        if (method == null)
        {
        }
        if (method == null)
        {
            throw new DispatchException(
                    SoapMessages.cannotInvokeCallWithoutOperation(), event.getMessage(),
                    event.getEndpoint());
        }
        else if (method instanceof SoapMethod)
        {
            synchronized (this)
            {
                if ( == null)
                {
                     = new HashMap();
                }
                .put(((SoapMethod)method).getName().getLocalPart(), method);
            }
        }
        return method;
    }
    private Object[] getArgs(MuleEvent eventthrows TransformerException
    {
        Object payload = event.transformMessage();
        Object[] args;
        if (payload instanceof Object[])
        {
            args = (Object[])payload;
        }
        else
        {
            args = new Object[]{payload};
        }
        if (event.getMessage().getAttachmentNames() != null
            && event.getMessage().getAttachmentNames().size() > 0)
        {
            ArrayList attachments = new ArrayList();
            Iterator i = event.getMessage().getAttachmentNames().iterator();
            while (i.hasNext())
            {
                attachments.add(event.getMessage().getAttachment((String)i.next()));
            }
            ArrayList temp = new ArrayList(Arrays.asList(args));
            temp.add(attachments.toArray(new DataHandler[0]));
            args = temp.toArray();
        }
        return args;
    }
    protected void setMessageContextProperties(MuleMessage message, MessageContext ctx)
    {
        String temp = ctx.getStrProp(.);
        if (StringUtils.isNotBlank(temp))
        {
            message.setCorrelationId(temp);
        }
        temp = ctx.getStrProp(.);
        if (StringUtils.isNotBlank(temp))
        {
            message.setCorrelationGroupSize(Integer.parseInt(temp));
        }
        temp = ctx.getStrProp(.);
        if (StringUtils.isNotBlank(temp))
        {
            message.setCorrelationSequence(Integer.parseInt(temp));
        }
        temp = ctx.getStrProp(.);
        if (StringUtils.isNotBlank(temp))
        {
            message.setReplyTo(temp);
        }
    }
    protected void setMessageContextAttachments(MuleMessage message, MessageContext ctxthrows Exception
    {
        int x = 0;
        for (Iterator iterator = ctx.getMessage().getAttachments(); iterator.hasNext(); x++)
        {
            message.addAttachment(String.valueOf(x),
                ((AttachmentPart)iterator.next()).getActivationDataHandler());
        }
    }
    protected static MuleMessage createMessage(Object result, Call call)
    {
        if (result == null)
        {
            result = NullPayload.getInstance();
        }
        Map props = new HashMap();
        Iterator iter = call.getMessageContext().getPropertyNames();
        Object key;
        while (iter.hasNext())
        {
            key = iter.next();
            props.put(keycall.getMessageContext().getProperty(key.toString()));
        }
        props.put("soap.message"call.getMessageContext().getMessage());
        call.clearHeaders();
        call.clearOperation();
        return new DefaultMuleMessage(resultprops);
    }
    public String parseSoapAction(String soapActionQName methodMuleEvent event)
    {
        EndpointURI endpointURI = event.getEndpoint().getEndpointURI();
        Map properties = new HashMap();
        MuleMessage msg = event.getMessage();
        for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
        {
            String propertyKey = (String)iterator.next();
            properties.put(propertyKeymsg.getProperty(propertyKey));
        }
        properties.put(.method.getLocalPart());
        properties.put("methodNamespace"method.getNamespaceURI());
        properties.put("address"endpointURI.getAddress());
        properties.put("scheme"endpointURI.getScheme());
        properties.put("host"endpointURI.getHost());
        properties.put("port", String.valueOf(endpointURI.getPort()));
        properties.put("path"endpointURI.getPath());
        properties.put("hostInfo"endpointURI.getScheme()
                                   + "://"
                                   + endpointURI.getHost()
                                   + (endpointURI.getPort() > -1
                                                   ? ":" + String.valueOf(endpointURI.getPort()) : ""));
        if (event.getService() != null)
        {
            properties.put("serviceName"event.getService().getName());
        }
        TemplateParser tp = TemplateParser.createMuleStyleParser();
        soapAction = tp.parse(propertiessoapAction);
        if (.isDebugEnabled())
        {
            .debug("SoapAction for this call is: " + soapAction);
        }
        return soapAction;
    }
    private void setCallParams(Call callMuleEvent eventQName methodthrows ClassNotFoundException
    {
        synchronized (this)
        {
            if ( == null)
            {
                loadCallParams(eventmethod.getNamespaceURI());
            }
        }
        SoapMethod soapMethod = (SoapMethod)event.getMessage()
        if (soapMethod == null)
        {
            soapMethod = (SoapMethod).get(method.getLocalPart());
        }
        if (soapMethod != null)
        {
            for (Iterator iterator = soapMethod.getNamedParameters().iterator(); iterator.hasNext();)
            {
                NamedParameter parameter = (NamedParameter)iterator.next();
                call.addParameter(parameter.getName(), parameter.getType(), parameter.getMode());
            }
            if (soapMethod.getReturnType() != null)
            {
                call.setReturnType(soapMethod.getReturnType());
            }
            else if (soapMethod.getReturnClass() != null)
            {
                call.setReturnClass(soapMethod.getReturnClass());
            }
            call.setOperationName(soapMethod.getName());
        }
    }
    private void loadCallParams(MuleEvent eventString namespacethrows ClassNotFoundException
    {
        Map methodCalls = (Map)event.getMessage().getProperty(.);
        if (methodCalls == null)
        {
            return;
        }
        Map.Entry entry;
        SoapMethod soapMethod;
         = new HashMap();
        for (Iterator iterator = methodCalls.entrySet().iterator(); iterator.hasNext();)
        {
            entry = (Map.Entry)iterator.next();
            if (StringUtils.isEmpty(namespace))
            {
                if (entry.getValue() instanceof List)
                {
                    soapMethod = new SoapMethod(entry.getKey().toString(), (List)entry.getValue());
                }
                else
                {
                    soapMethod = new SoapMethod(entry.getKey().toString(), entry.getValue().toString());
                }
            }
            else
            {
                if (entry.getValue() instanceof List)
                {
                    soapMethod = new SoapMethod(new QName(namespaceentry.getKey().toString()),
                        (List)entry.getValue());
                }
                else
                {
                    soapMethod = new SoapMethod(new QName(namespaceentry.getKey().toString()),
                        entry.getValue().toString());
                }
            }
            .put(soapMethod.getName().getLocalPart(), soapMethod);
        }
    }
New to GrepCode? Check out our FAQ X