View Javadoc
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.cxf.builder;
8   
9   import org.mule.api.lifecycle.CreateException;
10  import org.mule.module.cxf.CxfOutboundMessageProcessor;
11  import org.mule.module.cxf.endpoint.CxfEndpointBuilder;
12  
13  import org.apache.cxf.endpoint.Client;
14  import org.apache.cxf.endpoint.ClientImpl;
15  import org.apache.cxf.endpoint.Endpoint;
16  import org.apache.cxf.service.model.EndpointInfo;
17  import org.apache.cxf.transport.ChainInitiationObserver;
18  import org.apache.cxf.transport.Destination;
19  import org.apache.cxf.transport.DestinationFactory;
20  import org.apache.cxf.transport.DestinationFactoryManager;
21  import org.apache.cxf.transport.MessageObserver;
22  
23  /**
24   * This builder uses a service that is already configured to build a CXF
25   * client and it's corresponding MessageProcessor. Given the specified
26   * <code>address</code> property, it will lookup the corresponding 
27   * inbound MessageProcessor. It will then use this processor's service model
28   * to configure a CXF client. 
29   * <p>
30   * This can be used via CXF meta endpoints. For instance, with MuleClient you can do:
31   * <code>
32   * MuleClient client = ...
33   * client.send("cxf:http://host/yourService?method=remoteOperation", message);
34   * </code>
35   * This will find the remote service, configure the client appropriately, and 
36   * invoke the remote service. 
37   * <p>
38   * This only works if the server and client are in the same Mule instance.
39   * 
40   * @see CxfEndpointBuilder
41   */
42  public class LocalClientMessageProcessorBuilder extends AbstractOutboundMessageProcessorBuilder
43  {
44      @Override
45      protected void configureMessageProcessor(CxfOutboundMessageProcessor processor)
46      {
47      }
48  
49      @Override
50      protected Client createClient() throws CreateException, Exception
51      {
52          String uri = getAddress();
53          int idx = uri.indexOf('?');
54          if (idx != -1)
55          {
56              uri = uri.substring(0, idx);
57          }
58          
59          // remove username/password
60          idx = uri.indexOf('@');
61          int slashIdx = uri.indexOf("//");
62          if (idx != -1 && slashIdx != -1)
63          {
64              uri = uri.substring(0, slashIdx + 2) + uri.substring(idx + 1);
65          }
66          
67          EndpointInfo ei = new EndpointInfo();
68          ei.setAddress(uri);
69  
70          DestinationFactoryManager dfm = getBus().getExtension(DestinationFactoryManager.class);
71          DestinationFactory df = dfm.getDestinationFactoryForUri(uri);
72          if (df == null)
73          {
74              throw new Exception("Could not find a destination factory for uri " + uri);
75          }
76  
77          Destination dest = df.getDestination(ei);
78          MessageObserver mo = dest.getMessageObserver();
79          if (mo instanceof ChainInitiationObserver)
80          {
81              ChainInitiationObserver cMo = (ChainInitiationObserver) mo;
82              Endpoint cxfEP = cMo.getEndpoint();
83  
84              return new ClientImpl(getBus(), cxfEP);
85          }
86          else
87          {
88              throw new Exception("Could not create client! No Server was found directly on the endpoint: "
89                                  + uri);
90          }
91      }
92      
93  }