KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > soap > soaprmi > WSIFPort_SoapRMI


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.providers.soap.soaprmi;
59
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import javax.wsdl.Binding;
66 import javax.wsdl.BindingFault;
67 import javax.wsdl.BindingInput;
68 import javax.wsdl.BindingOperation;
69 import javax.wsdl.BindingOutput;
70 import javax.wsdl.Definition;
71 import javax.wsdl.Input;
72 import javax.wsdl.Operation;
73 import javax.wsdl.Output;
74 import javax.wsdl.Port;
75 import javax.wsdl.PortType;
76 import javax.wsdl.Service;
77 import javax.wsdl.extensions.soap.SOAPAddress;
78 import javax.wsdl.extensions.soap.SOAPBinding;
79 import javax.wsdl.extensions.soap.SOAPBody;
80 import javax.wsdl.extensions.soap.SOAPFault;
81 import javax.wsdl.extensions.soap.SOAPHeader;
82 import javax.wsdl.extensions.soap.SOAPOperation;
83 import org.apache.wsif.WSIFException;
84 import org.apache.wsif.WSIFMessage;
85 import org.apache.wsif.WSIFOperation;
86 import org.apache.wsif.base.WSIFDefaultPort;
87 import org.apache.wsif.logging.Trc;
88 import org.apache.wsif.providers.WSIFDynamicTypeMap;
89
90 /**
91  * This is SoapRMI dynamic WSIF port that is driven by WSDL.
92  *
93  * @author Alekander Slominski
94  */

95 public class WSIFPort_SoapRMI extends WSIFDefaultPort {
96     
97     private static final long serialVersionUID = 1L;
98     
99     protected Map JavaDoc operationInstances = new HashMap JavaDoc();
100
101     protected Port port;
102     protected Definition definition;
103     protected String JavaDoc location;
104
105     /**
106      * Create dynamic port instance from WDL model defintion and port.
107      * <p><b>NOTE:</b> this constructor is doing full initialization
108      * therefore after dynamic port is created overhead of executing
109      * operation should be as small as possible for dynamic case...
110      */

111     public WSIFPort_SoapRMI(
112         Definition def,
113         Service service,
114         Port port,
115         WSIFDynamicTypeMap typeMap)
116         throws WSIFException {
117         setDefinition(def);
118         setPort(port);
119
120         // find Port soap:address bindng element
121
SOAPAddress sa =
122             (SOAPAddress) getExtElem(port,
123                 SOAPAddress.class,
124                 port.getExtensibilityElements());
125         if (sa != null) {
126             location = sa.getLocationURI();
127         }
128         if (location == null) {
129             throw new WSIFException(
130                 "soap:address with location URI is required for " + port);
131         }
132
133         // check soap:binding element
134
String JavaDoc style = null;
135         Binding binding = port.getBinding();
136         SOAPBinding soapBinding =
137             (SOAPBinding) getExtElem(binding,
138                 SOAPBinding.class,
139                 binding.getExtensibilityElements());
140         if (soapBinding != null) {
141             style = soapBinding.getStyle();
142             if (!"rpc".equals(style)) {
143                 throw new WSIFException("unsupported style " + style + " for " + soapBinding);
144             }
145             String JavaDoc transport = soapBinding.getTransportURI();
146             if (!"http://schemas.xmlsoap.org/soap/http".equals(transport)) {
147                 throw new WSIFException(
148                     "unsupported transport " + transport + " for " + soapBinding);
149             }
150         }
151         if (style == null) {
152             style = "document"; //it is default style value as of WSDL 1.1
153
}
154
155         // check required SOAP bindings for all portType operations
156
PortType portType = binding.getPortType();
157
158         List JavaDoc operationList = portType.getOperations();
159
160         // process each operation to create dynamic operation instance
161
for (Iterator JavaDoc i = operationList.iterator(); i.hasNext();) {
162             Operation op = (Operation) i.next();
163             String JavaDoc name = op.getName();
164             //System.err.println("op = "+op);
165
Input input = op.getInput();
166             Output output = op.getOutput();
167             if (input == null) {
168                 throw new WSIFException("missing input message for operation " + name);
169
170             }
171             if (output == null) {
172                 throw new WSIFException("missing output message for operation " + name);
173             }
174
175             WSIFOperation_SoapRMI opInst = new WSIFOperation_SoapRMI(this, op, typeMap);
176
177             BindingOperation bop =
178                 binding.getBindingOperation(name, input.getName(), output.getName());
179
180             if (bop == null) {
181                 throw new WSIFException(
182                     "mising required in WSDL 1.1 binding operation for " + name);
183             }
184
185             // get soapActionURI and style from soap:operation
186
SOAPOperation soapOperation =
187                 (SOAPOperation) getExtElem(bop,
188                     SOAPOperation.class,
189                     bop.getExtensibilityElements());
190             if (soapOperation == null) {
191                 throw new WSIFException(
192                     "soapAction must be specified in "
193                         + " required by WSDL 1.1 soap:operation binding for "
194                         + bop);
195             }
196             String JavaDoc soapActionURI = soapOperation.getSoapActionURI();
197             opInst.setSoapActionURI(soapActionURI);
198
199             if (Trc.ON)
200                 Trc.event(
201                     this,
202                     "setting actionURI "
203                         + soapActionURI
204                         + " for op "
205                         + opInst.getName());
206             String JavaDoc opStyle = soapOperation.getStyle();
207             if (opStyle != null && !"rpc".equals(opStyle)) {
208                 throw new WSIFException(
209                     "unsupported style " + style + " for operation " + name);
210             } else if (!"rpc".equals(style)) {
211                 throw new WSIFException(
212                     "default soap style must be rpc if operation "
213                         + name
214                         + " binding has not style attribute");
215             }
216
217             // try to get soap:body for input message
218
BindingInput binpt = bop.getBindingInput();
219             SOAPBody soapInputBody =
220                 (SOAPBody) getExtElem(binpt, SOAPBody.class, binpt.getExtensibilityElements());
221             if (soapInputBody != null) {
222                 String JavaDoc namespaceURI = soapInputBody.getNamespaceURI();
223                 
224                 if (Trc.ON)
225                     Trc.event(
226                         this,
227                         "setting namespace "
228                             + namespaceURI
229                             + " for op "
230                             + opInst.getName());
231                 opInst.setInputNamespace(namespaceURI);
232                 String JavaDoc use = soapInputBody.getUse();
233                 if (!"encoded".equals(use)) {
234                     throw new WSIFException("unsupported use " + use + " in " + soapOperation);
235                 }
236                 List JavaDoc encodingStyles = soapInputBody.getEncodingStyles();
237                 if (encodingStyles != null) {
238                     if (encodingStyles.size() == 0) {
239                     }
240                     opInst.setInputEncodingStyle((String JavaDoc) encodingStyles.get(0));
241                     // quietly ignore if encodingStyles.size() > 1 ...
242
}
243                 List JavaDoc parts = soapInputBody.getParts();
244                 if (parts != null) {
245                     opInst.setPartNames(parts);
246                 }
247             }
248             SOAPHeader soapHeader =
249                 (SOAPHeader) getExtElem(binpt,
250                     SOAPHeader.class,
251                     binpt.getExtensibilityElements());
252             if (soapHeader != null) {
253                 throw new WSIFException("not supported input soap:header " + soapHeader);
254             }
255
256             // try to get soap:body for output message
257
BindingOutput boutpt = bop.getBindingOutput();
258             SOAPBody soapOutputBody =
259                 (SOAPBody) getExtElem(boutpt,
260                     SOAPBody.class,
261                     boutpt.getExtensibilityElements());
262             if (soapOutputBody != null) {
263                 // NOTE: element ignored
264
//String namespaceURI = soapOutputBody.getNamespaceURI();
265
String JavaDoc use = soapInputBody.getUse();
266                 if (!"encoded".equals(use)) {
267                     throw new WSIFException("unsupported use " + use + " in " + soapOperation);
268                 }
269                 // NOTE: element ignored
270
//List encodingStyles = soapInputBody.getEncodingStyles();
271
List JavaDoc parts = soapInputBody.getParts();
272                 if (parts != null && parts.size() > 0) {
273                     opInst.setReturnName((String JavaDoc) parts.get(0));
274                 }
275             }
276             soapHeader =
277                 (SOAPHeader) getExtElem(boutpt,
278                     SOAPHeader.class,
279                     boutpt.getExtensibilityElements());
280             if (soapHeader != null) {
281                 throw new WSIFException("not supported output soap:header " + soapHeader);
282             }
283
284             for (Iterator JavaDoc bfaults = bop.getBindingFaults().values().iterator();
285                 bfaults.hasNext();
286                 ) {
287                 BindingFault bfault = (BindingFault) bfaults.next();
288                 SOAPFault soapFault =
289                     (SOAPFault) getExtElem(bfault,
290                         SOAPFault.class,
291                         bfault.getExtensibilityElements());
292                 // NOTE: element ignored
293
//if(soapFault != null) {
294
// throw new WSIFException(
295
// "soap:fault not supported in "+bfault);
296
//}
297
}
298
299             // make this operation instance accessible
300
setDynamicWSIFOperation(
301                 op.getName(),
302                 op.getInput().getName(),
303                 op.getOutput().getName(),
304                 opInst);
305
306         } // for
307
}
308
309     public String JavaDoc getLocation() {
310         return location;
311     }
312
313     public void setLocation(String JavaDoc location) {
314         this.location = location;
315     }
316
317     // where is WSDL defining this abstract mesage
318
public Definition getDefinition() {
319         return definition;
320     }
321     
322     public void setDefinition(Definition value) {
323         definition = value;
324     }
325     
326     public Port getPort() {
327         return port;
328     }
329     
330     public void setPort(Port value) {
331         port = value;
332     }
333
334     // WSIF: keep list of operations available in this port
335
public void setDynamicWSIFOperation(
336         String JavaDoc name,
337         String JavaDoc inputName,
338         String JavaDoc outputName,
339         WSIFOperation_SoapRMI value) {
340         operationInstances.put(getKey(name, inputName, outputName), value);
341     }
342
343     public WSIFOperation createOperation(String JavaDoc operationName)
344         throws WSIFException {
345         return createOperation(operationName, null, null);
346     }
347
348     public WSIFOperation createOperation(
349         String JavaDoc operationName,
350         String JavaDoc inputName,
351         String JavaDoc outputName)
352         throws WSIFException {
353         WSIFOperation_SoapRMI op =
354             getDynamicWSIFOperation(operationName, inputName, outputName);
355         if (op == null) {
356             throw new WSIFException(
357                 "Could not create operation: "
358                     + operationName
359                     + ":"
360                     + inputName
361                     + ":"
362                     + outputName);
363         }
364         return op.copy();
365     }
366
367     public WSIFOperation_SoapRMI getDynamicWSIFOperation(
368         String JavaDoc name,
369         String JavaDoc inputName,
370         String JavaDoc outputName)
371         throws WSIFException {
372         WSIFOperation_SoapRMI tempOp =
373             (WSIFOperation_SoapRMI) operationInstances.get(
374                 getKey(name, inputName, outputName));
375                 
376         WSIFOperation_SoapRMI operation = null;
377         if (tempOp != null) {
378             operation = tempOp.copy();
379         }
380                         
381         if (operation == null) {
382             BindingOperation bindingOperationModel =
383                 port.getBinding().getBindingOperation(name, inputName, outputName);
384
385             if (bindingOperationModel != null) {
386                 // Only one operation matched in binding so find it in porttype
387
// from all the information that is available to us
388
Iterator JavaDoc i = operationInstances.keySet().iterator();
389                 while (i.hasNext()) {
390                     String JavaDoc key = (String JavaDoc) i.next();
391                     if ((outputName != null && key.endsWith(outputName)) || outputName == null) {
392                         String JavaDoc start = (inputName == null) ? name : name + ":" + inputName;
393                         if (key.startsWith(start)) {
394                             if (operation != null) {
395                                 // Duplicate operation found based on names!
396
operation = null;
397                                 break;
398                             }
399                             operation = (WSIFOperation_SoapRMI) operationInstances.get(key);
400                         }
401                     }
402                 }
403             }
404         }
405
406         return operation;
407     }
408 }
Popular Tags