View Javadoc

1   package org.codehaus.xfire.transport.http;
2   
3   import javax.servlet.http.HttpServletRequest;
4   import javax.wsdl.Binding;
5   import javax.wsdl.BindingInput;
6   import javax.wsdl.BindingOperation;
7   import javax.wsdl.BindingOutput;
8   import javax.wsdl.Operation;
9   import javax.wsdl.Port;
10  import javax.wsdl.PortType;
11  import javax.wsdl.extensions.soap.SOAPBinding;
12  import javax.wsdl.extensions.soap.SOAPBody;
13  import javax.xml.namespace.QName;
14  import org.codehaus.xfire.SOAPConstants;
15  import org.codehaus.xfire.service.Service;
16  import org.codehaus.xfire.transport.Transport;
17  import com.ibm.wsdl.BindingImpl;
18  import com.ibm.wsdl.BindingInputImpl;
19  import com.ibm.wsdl.BindingOperationImpl;
20  import com.ibm.wsdl.BindingOutputImpl;
21  import com.ibm.wsdl.PortImpl;
22  import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
23  import com.ibm.wsdl.extensions.soap.SOAPBindingImpl;
24  import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
25  import com.ibm.wsdl.extensions.soap.SOAPOperationImpl;
26  
27  /***
28   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
29   */
30  public class SoapHttpTransport
31  	implements Transport
32  {
33      public final static String ID = "http";
34  
35      public static final String HTTP_TRANSPORT_NS = "http://schemas.xmlsoap.org/soap/http";
36  
37      public SoapHttpTransport()
38      {
39      }
40      
41      /***
42       * @see org.codehaus.xfire.transport.Transport#getName()
43       */
44      public String getName()
45      {
46          return ID;
47      }
48  
49      /***
50       * @see org.codehaus.xfire.transport.Transport#createBinding(javax.wsdl.PortType)
51       */
52      public Binding createBinding(PortType portType, Service service)
53      {
54  		Binding binding = new BindingImpl();
55          binding.setQName( new QName( service.getDefaultNamespace(), service.getName()+"Binding" ) );
56          binding.setPortType( portType );
57          binding.setUndefined(false);
58          
59          binding.addExtensibilityElement( createSoapBinding(service) );
60  
61          return binding;
62      }
63  
64  	protected SOAPBinding createSoapBinding(Service service)
65  	{
66  	    SOAPBinding soapBind = new SOAPBindingImpl();
67  		
68  		String style = service.getStyle();
69          if ( style.equals( SOAPConstants.STYLE_WRAPPED ) )
70              style = SOAPConstants.STYLE_DOCUMENT;
71          
72          soapBind.setStyle( style );
73          soapBind.setTransportURI( HTTP_TRANSPORT_NS );
74  
75  		return soapBind;
76  	}
77  
78      /***
79       * @see org.codehaus.xfire.transport.Transport#createPort(javax.wsdl.Binding)
80       */
81      public Port createPort(Binding transportBinding, Service service)
82      {
83          SOAPAddressImpl add = new SOAPAddressImpl();
84          add.setLocationURI( getUrl( service.getName() ) );
85          
86          Port port = new PortImpl();
87          port.setBinding( transportBinding );
88          port.setName( service.getName()+"Port" );
89          port.addExtensibilityElement( add );
90         
91          return port;
92      }
93  
94      /***
95  	 * Get the URL for a particular service.
96  	 */
97  	protected String getUrl( String serviceName )
98  	{
99  		HttpServletRequest req = XFireServlet.getRequest();
100         
101         return getWebappBase(req) + "/services/" + serviceName;
102 	}
103 
104 	/***
105      * @see org.codehaus.xfire.transport.Transport#createBindingOperation(javax.wsdl.Message, javax.wsdl.Message, org.codehaus.xfire.java.JavaService)
106      */
107     public BindingOperation createBindingOperation(PortType portType, Operation wsdlOp, Service service)
108     {
109         // Create bindings
110         SOAPBody body = createSoapBody( service );
111 
112         SOAPOperationImpl soapOp = new SOAPOperationImpl();
113         soapOp.setSoapActionURI("");
114         
115         BindingInput bindIn = new BindingInputImpl();
116         bindIn.setName( wsdlOp.getInput().getName() );
117         bindIn.addExtensibilityElement( body );
118         
119         BindingOutput bindOut = new BindingOutputImpl();
120         bindOut.setName( wsdlOp.getOutput().getName() );
121         bindOut.addExtensibilityElement( body );
122         
123         BindingOperation bindOp = new BindingOperationImpl();
124         bindOp.setName( wsdlOp.getName() );
125         bindOp.setOperation( wsdlOp );
126         bindOp.setBindingInput( bindIn );
127         bindOp.setBindingOutput( bindOut );
128         bindOp.addExtensibilityElement( soapOp );
129         
130         return bindOp;
131     }
132     
133     public SOAPBody createSoapBody( Service service )
134     {
135         SOAPBody body = new SOAPBodyImpl();
136         body.setUse( service.getUse() ); 
137 
138         if ( service.getStyle().equals( SOAPConstants.STYLE_RPC ) )
139             body.setNamespaceURI( service.getDefaultNamespace() );
140         
141         return body;
142     }
143     
144     protected String getWebappBase(HttpServletRequest request)
145 	{
146 		StringBuffer baseURL = new StringBuffer(128);
147 		baseURL.append(request.getScheme());
148 		baseURL.append("://");
149 		baseURL.append(request.getServerName());
150 		if (request.getServerPort() != 80)
151 		{
152 			baseURL.append(":");
153 			baseURL.append(request.getServerPort());
154 		}
155 		baseURL.append(request.getContextPath());
156 		return baseURL.toString();
157 	}
158 }