clientwrapper.java

来自「提供ESB 应用mule源代码 提供ESB 应用mule源代码」· Java 代码 · 共 471 行 · 第 1/2 页

JAVA
471
字号
/* * $Id: ClientWrapper.java 12786 2008-09-28 20:34:12Z dandiep $ * -------------------------------------------------------------------------------------- * 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.cxf;import org.mule.api.MuleEvent;import org.mule.api.config.MuleProperties;import org.mule.api.endpoint.EndpointURI;import org.mule.api.endpoint.ImmutableEndpoint;import org.mule.api.lifecycle.CreateException;import org.mule.api.transport.DispatchException;import org.mule.transport.cxf.i18n.CxfMessages;import org.mule.transport.cxf.support.MuleHeadersInInterceptor;import org.mule.transport.cxf.support.MuleHeadersOutInterceptor;import org.mule.transport.cxf.support.MuleProtocolHeadersOutInterceptor;import org.mule.transport.cxf.support.OutputPayloadInterceptor;import org.mule.transport.cxf.support.ProxyService;import org.mule.transport.soap.i18n.SoapMessages;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.net.URL;import java.util.HashMap;import java.util.List;import javax.xml.namespace.QName;import javax.xml.ws.BindingProvider;import javax.xml.ws.Service;import javax.xml.ws.WebEndpoint;import javax.xml.ws.WebServiceClient;import org.apache.commons.lang.BooleanUtils;import org.apache.cxf.Bus;import org.apache.cxf.binding.Binding;import org.apache.cxf.common.classloader.ClassLoaderUtils;import org.apache.cxf.databinding.stax.StaxDataBinding;import org.apache.cxf.databinding.stax.StaxDataBindingFeature;import org.apache.cxf.endpoint.Client;import org.apache.cxf.endpoint.ClientImpl;import org.apache.cxf.endpoint.Endpoint;import org.apache.cxf.endpoint.EndpointImpl;import org.apache.cxf.feature.AbstractFeature;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.frontend.ClientProxyFactoryBean;import org.apache.cxf.frontend.MethodDispatcher;import org.apache.cxf.interceptor.Interceptor;import org.apache.cxf.interceptor.WrappedOutInterceptor;import org.apache.cxf.message.Message;import org.apache.cxf.phase.PhaseInterceptor;import org.apache.cxf.resource.ResourceManager;import org.apache.cxf.resource.URIResolver;import org.apache.cxf.service.model.BindingOperationInfo;import org.apache.cxf.service.model.EndpointInfo;import org.apache.cxf.transport.ChainInitiationObserver;import org.apache.cxf.transport.Destination;import org.apache.cxf.transport.DestinationFactory;import org.apache.cxf.transport.DestinationFactoryManager;import org.apache.cxf.transport.MessageObserver;public class ClientWrapper{    protected ImmutableEndpoint endpoint;    protected Bus bus;    protected Client client;    protected String defaultMethodName;    // If we have a proxy we're going to invoke it directly    // Since the JAX-WS proxy does extra special things for us.    protected BindingProvider clientProxy;    protected Method defaultMethod;    protected boolean proxy;        public Client getClient()    {        return client;    }    public BindingProvider getClientProxy()    {        return clientProxy;    }    @SuppressWarnings("unchecked")    public void initialize() throws Exception    {        String clientClass = (String) endpoint.getProperty(CxfConstants.CLIENT_CLASS);        proxy = BooleanUtils.toBoolean((String) endpoint.getProperty(CxfConstants.PROXY));                if (clientClass != null)        {            createClientFromClass(bus, clientClass);        }        else if (proxy)        {            createClientProxy(bus);        }        else        {            createClientFromLocalServer(bus);        }                addInterceptors(client.getInInterceptors(), (List<Interceptor>) endpoint.getProperty(CxfConstants.IN_INTERCEPTORS));        addInterceptors(client.getInFaultInterceptors(), (List<Interceptor>) endpoint.getProperty(CxfConstants.IN_FAULT_INTERCEPTORS));        addInterceptors(client.getOutInterceptors(), (List<Interceptor>) endpoint.getProperty(CxfConstants.OUT_INTERCEPTORS));        addInterceptors(client.getOutFaultInterceptors(), (List<Interceptor>) endpoint.getProperty(CxfConstants.OUT_FAULT_INTERCEPTORS));        if (proxy)        {            client.getOutInterceptors().add(new OutputPayloadInterceptor());        }                List<AbstractFeature> features = (List<AbstractFeature>) endpoint.getProperty(CxfConstants.OUT_FAULT_INTERCEPTORS);                if (features != null)        {            for (AbstractFeature f : features)            {                f.initialize(client, bus);            }        }        EndpointImpl ep = (EndpointImpl) client.getEndpoint();                Object mtomEnabled = endpoint.getProperty(CxfConstants.MTOM_ENABLED);        if (mtomEnabled != null)         {            HashMap<String, Object> props = new HashMap<String, Object>();            props.put(Message.MTOM_ENABLED, mtomEnabled);            ep.setProperties(props);        }                addMuleInterceptors();    }    @SuppressWarnings("unchecked")    private void addInterceptors(List<Interceptor> col, List<Interceptor> supplied)    {        if (supplied != null)         {            col.addAll(supplied);        }    }    protected Method findMethod(Class<?> clientCls) throws Exception    {        if (defaultMethod == null)        {            String op = (String) endpoint.getProperties().get(CxfConstants.OPERATION);            if (op == null)            {                op = (String) endpoint.getProperties().get(CxfConstants.OPERATION);            }            if (op != null)            {                return getMethodFromOperation(op);            }        }        return null;    }    protected BindingOperationInfo getOperation(String opName) throws Exception    {        // Normally its not this hard to invoke the CXF Client, but we're        // sending along some exchange properties, so we need to use a more advanced        // method        Endpoint ep = client.getEndpoint();        QName q = new QName(ep.getService().getName().getNamespaceURI(), opName);        BindingOperationInfo bop = ep.getBinding().getBindingInfo().getOperation(q);        if (bop == null)        {            throw new Exception("No such operation: " + defaultMethod);        }        if (bop.isUnwrappedCapable())        {            bop = bop.getUnwrappedOperation();        }        return bop;    }    private Method getMethodFromOperation(String op) throws Exception    {        BindingOperationInfo bop = getOperation(op);        MethodDispatcher md = (MethodDispatcher) client.getEndpoint().getService().get(            MethodDispatcher.class.getName());        return md.getMethod(bop);    }    protected void createClientProxy(Bus bus) throws Exception    {        // TODO: Specify WSDL        String wsdlLocation = (String) endpoint.getProperty(CxfConstants.WSDL_LOCATION);                ClientProxyFactoryBean cpf = new ClientProxyFactoryBean();        cpf.setServiceClass(ProxyService.class);        cpf.setDataBinding(new StaxDataBinding());        cpf.getFeatures().add(new StaxDataBindingFeature());        cpf.setAddress(endpoint.getEndpointURI().getAddress());        if (wsdlLocation != null)         {            cpf.setWsdlLocation(wsdlLocation);        }                this.client = ClientProxy.getClient(cpf.create());                Binding binding = this.client.getEndpoint().getBinding();                removeInterceptor(binding.getOutInterceptors(), WrappedOutInterceptor.class.getName());                proxy = true;    }    @SuppressWarnings("unchecked")    private void removeInterceptor(List<Interceptor> inInterceptors, String name) {        for (Interceptor<?> i : inInterceptors) {            if (i instanceof PhaseInterceptor) {                PhaseInterceptor<Message> p = (PhaseInterceptor<Message>)i;                if (p.getId().equals(name)) {                    inInterceptors.remove(p);                    return;                }            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?