View Javadoc

1   /*
2    * $Id: MuleUniversalTransport.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.transport;
12  
13  import org.mule.module.cxf.CxfConfiguration;
14  
15  import java.io.IOException;
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.HashSet;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import javax.wsdl.Port;
25  import javax.wsdl.extensions.http.HTTPAddress;
26  import javax.wsdl.extensions.soap.SOAPAddress;
27  import javax.xml.namespace.QName;
28  
29  import org.apache.cxf.Bus;
30  import org.apache.cxf.service.Service;
31  import org.apache.cxf.service.model.BindingInfo;
32  import org.apache.cxf.service.model.EndpointInfo;
33  import org.apache.cxf.service.model.ServiceInfo;
34  import org.apache.cxf.transport.AbstractTransportFactory;
35  import org.apache.cxf.transport.Conduit;
36  import org.apache.cxf.transport.ConduitInitiator;
37  import org.apache.cxf.transport.Destination;
38  import org.apache.cxf.transport.DestinationFactory;
39  import org.apache.cxf.ws.addressing.AttributedURIType;
40  import org.apache.cxf.ws.addressing.EndpointReferenceType;
41  import org.apache.cxf.wsdl.http.AddressType;
42  import org.apache.cxf.wsdl11.WSDLEndpointFactory;
43  
44  public class MuleUniversalTransport extends AbstractTransportFactory
45      implements ConduitInitiator, DestinationFactory, WSDLEndpointFactory
46  {
47  
48      public static final String TRANSPORT_ID = "http://mule.codehaus.org/cxf";
49  
50      private static Set<String> PREFIXES = new HashSet<String>();
51      static
52      {
53          PREFIXES.add("http://");
54          PREFIXES.add("https://");
55          PREFIXES.add("jms://");
56          PREFIXES.add("vm://");
57          PREFIXES.add("xmpp://");
58          PREFIXES.add("smtp://");
59          PREFIXES.add("tcp://");
60      }
61  
62      private Map<String, Destination> destinations = new HashMap<String, Destination>();
63  
64      private Bus bus;
65  
66      private CxfConfiguration connector;
67  
68      public MuleUniversalTransport(CxfConfiguration connector)
69      {
70          super();
71  
72          ArrayList<String> tids = new ArrayList<String>();
73          tids.add("http://schemas.xmlsoap.org/soap/http");
74          setTransportIds(tids);
75  
76          this.connector = connector;
77      }
78  
79      public Destination getDestination(EndpointInfo ei) throws IOException
80      {
81          return getDestination(ei, createReference(ei));
82      }
83  
84      protected Destination getDestination(EndpointInfo ei, EndpointReferenceType reference) throws IOException
85      {
86          String uri = reference.getAddress().getValue();
87          int idx = uri.indexOf('?');
88          if (idx != -1)
89          {
90              uri = uri.substring(0, idx);
91          }
92  
93          synchronized (this)
94          {
95              Destination d = destinations.get(uri);
96              if (d == null)
97              {
98                  d = createDestination(ei, reference);
99                  destinations.put(uri, d);
100             }
101             return d;
102         }
103     }
104 
105     private Destination createDestination(EndpointInfo ei, EndpointReferenceType reference)
106     {
107         return new MuleUniversalDestination(this, reference, ei);
108     }
109 
110     public Conduit getConduit(EndpointInfo ei) throws IOException
111     {
112         return new MuleUniversalConduit(this, connector, ei, null);
113     }
114 
115     public Conduit getConduit(EndpointInfo ei, EndpointReferenceType target) throws IOException
116     {
117         return new MuleUniversalConduit(this, connector, ei, target);
118     }
119 
120     EndpointReferenceType createReference(EndpointInfo ei)
121     {
122         EndpointReferenceType epr = new EndpointReferenceType();
123         AttributedURIType address = new AttributedURIType();
124         address.setValue(ei.getAddress());
125         epr.setAddress(address);
126         return epr;
127     }
128 
129     @Override
130     public Set<String> getUriPrefixes()
131     {
132         return PREFIXES;
133     }
134 
135     public Bus getBus()
136     {
137         return bus;
138     }
139 
140     public void setBus(Bus bus)
141     {
142         this.bus = bus;
143     }
144 
145     void remove(MuleUniversalDestination destination)
146     {
147         destinations.remove(destination.getAddress().getAddress().getValue());
148     }
149 
150     public CxfConfiguration getConnector()
151     {
152         return connector;
153     }
154 
155     // Stuff relating to building of the <soap:address/> -
156     // I have no idea how this really works, but it does
157 
158     public void createPortExtensors(EndpointInfo ei, Service service)
159     {
160         // TODO
161     }
162 
163     @SuppressWarnings("unchecked")
164     public EndpointInfo createEndpointInfo(ServiceInfo serviceInfo, BindingInfo b, Port port)
165     {
166         if (port != null)
167         {
168             List ees = port.getExtensibilityElements();
169             for (Iterator itr = ees.iterator(); itr.hasNext();)
170             {
171                 Object extensor = itr.next();
172 
173                 if (extensor instanceof HTTPAddress)
174                 {
175                     final HTTPAddress httpAdd = (HTTPAddress) extensor;
176 
177                     EndpointInfo info = new HttpEndpointInfo(serviceInfo,
178                         "http://schemas.xmlsoap.org/wsdl/http/");
179                     info.setAddress(httpAdd.getLocationURI());
180                     info.addExtensor(httpAdd);
181                     return info;
182                 }
183                 else if (extensor instanceof AddressType)
184                 {
185                     final AddressType httpAdd = (AddressType) extensor;
186 
187                     EndpointInfo info = new HttpEndpointInfo(serviceInfo,
188                         "http://schemas.xmlsoap.org/wsdl/http/");
189                     info.setAddress(httpAdd.getLocation());
190                     info.addExtensor(httpAdd);
191                     return info;
192                 }
193             }
194         }
195         HttpEndpointInfo hei = new HttpEndpointInfo(serviceInfo, "http://schemas.xmlsoap.org/wsdl/http/");
196         AddressType at = new HttpAddressType();
197         hei.addExtensor(at);
198 
199         return hei;
200     }
201 
202     private static class HttpEndpointInfo extends EndpointInfo
203     {
204         AddressType saddress;
205 
206         HttpEndpointInfo(ServiceInfo serv, String trans)
207         {
208             super(serv, trans);
209         }
210 
211         public void setAddress(String s)
212         {
213             super.setAddress(s);
214             if (saddress != null)
215             {
216                 saddress.setLocation(s);
217             }
218         }
219 
220         public void addExtensor(Object el)
221         {
222             super.addExtensor(el);
223             if (el instanceof AddressType)
224             {
225                 saddress = (AddressType) el;
226             }
227         }
228     }
229 
230     private static class HttpAddressType extends AddressType implements HTTPAddress, SOAPAddress
231     {
232         public HttpAddressType()
233         {
234             super();
235             setElementType(new QName("http://schemas.xmlsoap.org/wsdl/soap/", "address"));
236         }
237 
238         public String getLocationURI()
239         {
240             return getLocation();
241         }
242 
243         public void setLocationURI(String locationURI)
244         {
245             setLocation(locationURI);
246         }
247 
248     }
249 }