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.endpoint;
8   
9   import java.util.ArrayList;
10  import java.util.Arrays;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.endpoint.EndpointException;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.api.endpoint.OutboundEndpoint;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.processor.MessageProcessor;
18  import org.mule.endpoint.AbstractMetaEndpointBuilder;
19  import org.mule.endpoint.EndpointURIEndpointBuilder;
20  import org.mule.module.cxf.builder.WsdlClientMessageProcessorBuilder;
21  import org.mule.module.cxf.config.FlowConfiguringMessageProcessor;
22  
23  public class WsdlCxfEndpointBuilder extends AbstractMetaEndpointBuilder
24  {
25  
26      private final String wsdlAddress;
27  
28      public WsdlCxfEndpointBuilder(EndpointURIEndpointBuilder global) throws EndpointException
29      {
30          super(global);
31  
32          this.wsdlAddress = getEndpointAddressWithoutMetaScheme(global.getEndpointBuilder().toString());
33          this.uriBuilder = new EndpointURIEndpointBuilder(wsdlAddress, muleContext).getEndpointBuilder();
34      }
35  
36      public WsdlCxfEndpointBuilder(String address, MuleContext muleContext)
37      {
38          super(getAddressWithoutQuery(getEndpointAddressWithoutMetaScheme(address)), muleContext);
39          this.wsdlAddress = getEndpointAddressWithoutMetaScheme(address);
40      }
41  
42      @Override
43      public InboundEndpoint buildInboundEndpoint() throws EndpointException, InitialisationException
44      {
45          throw new UnsupportedOperationException("Inbound meta CXF endpoints not supported");
46      }
47  
48      @Override
49      public OutboundEndpoint buildOutboundEndpoint() throws EndpointException, InitialisationException
50      {
51          final WsdlClientMessageProcessorBuilder builder = new WsdlClientMessageProcessorBuilder();
52          builder.setMuleContext(muleContext);
53          builder.setWsdlLocation(getEndpointBuilder().toString() + "?wsdl");
54          builder.setOperation(getOperation());
55  
56          try
57          {
58              // List must be mutable as it gets cleared on Mule shutdown
59              messageProcessors = new ArrayList<MessageProcessor>(
60                  Arrays.asList(new FlowConfiguringMessageProcessor(builder)));
61          }
62          catch (final Exception e)
63          {
64              throw new EndpointException(e);
65          }
66  
67          return super.buildOutboundEndpoint();
68      }
69  
70      private String getOperation()
71      {
72          String query = wsdlAddress;
73          final int idx = wsdlAddress.lastIndexOf('?');
74          if (idx != -1)
75          {
76              query = wsdlAddress.substring(idx + 1);
77          }
78          else
79          {
80              return null;
81          }
82  
83          final String[] params = query.split("&");
84          for (final String p : params)
85          {
86              if (p.startsWith("method="))
87              {
88                  return p.substring(7);
89              }
90          }
91          return null;
92      }
93  
94      private static String getAddressWithoutQuery(String string)
95      {
96          final int idx = string.indexOf('?');
97          if (idx != -1)
98          {
99              string = string.substring(0, idx);
100         }
101         return string;
102     }
103 
104 }