View Javadoc

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