View Javadoc

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