Source Code Cross Referenced for XMLFaultUtils.java in  » Web-Services-AXIS2 » jax-ws » org » apache » axis2 » jaxws » message » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services AXIS2 » jax ws » org.apache.axis2.jaxws.message.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:        package org.apache.axis2.jaxws.message.util;
020:
021:        import org.apache.axiom.om.OMElement;
022:        import org.apache.axiom.om.OMNamespace;
023:        import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
024:        import org.apache.axiom.soap.SOAP11Constants;
025:        import org.apache.axiom.soap.SOAPBody;
026:        import org.apache.axiom.soap.SOAPEnvelope;
027:        import org.apache.axiom.soap.SOAPFactory;
028:        import org.apache.axiom.soap.SOAPFault;
029:        import org.apache.axiom.soap.SOAPFaultCode;
030:        import org.apache.axiom.soap.SOAPFaultDetail;
031:        import org.apache.axiom.soap.SOAPFaultNode;
032:        import org.apache.axiom.soap.SOAPFaultReason;
033:        import org.apache.axiom.soap.SOAPFaultRole;
034:        import org.apache.axiom.soap.SOAPFaultSubCode;
035:        import org.apache.axiom.soap.SOAPFaultText;
036:        import org.apache.axiom.soap.SOAPFaultValue;
037:        import org.apache.axis2.jaxws.ExceptionFactory;
038:        import org.apache.axis2.jaxws.message.Block;
039:        import org.apache.axis2.jaxws.message.XMLFault;
040:        import org.apache.axis2.jaxws.message.XMLFaultCode;
041:        import org.apache.axis2.jaxws.message.XMLFaultReason;
042:        import org.apache.axis2.jaxws.message.factory.OMBlockFactory;
043:        import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
044:        import org.apache.axis2.jaxws.registry.FactoryRegistry;
045:
046:        import javax.xml.namespace.QName;
047:        import javax.xml.soap.Detail;
048:        import javax.xml.soap.DetailEntry;
049:        import javax.xml.soap.SOAPConstants;
050:        import javax.xml.soap.SOAPException;
051:        import javax.xml.stream.XMLStreamException;
052:        import javax.xml.ws.WebServiceException;
053:        import java.util.ArrayList;
054:        import java.util.Iterator;
055:        import java.util.List;
056:        import java.util.Locale;
057:
058:        /**
059:         * Collection of utilities used by the Message implementation to process XMLFaults.
060:         *
061:         * @see XMLFault
062:         */
063:        public class XMLFaultUtils {
064:
065:            /**
066:             * @param envelope javax.xml.soap.SOAPEnvelope
067:             * @return true if the SOAPEnvelope contains a SOAPFault
068:             */
069:            public static boolean isFault(javax.xml.soap.SOAPEnvelope envelope)
070:                    throws SOAPException {
071:                javax.xml.soap.SOAPBody body = envelope.getBody();
072:                if (body != null) {
073:                    return (body.getFault() != null);
074:                }
075:                return false;
076:            }
077:
078:            /**
079:             * @param envelope org.apache.axiom.soap.SOAPEnvelope
080:             * @return true if the SOAPEnvelope contains a SOAPFault
081:             */
082:            public static boolean isFault(SOAPEnvelope envelope) {
083:                SOAPBody body = envelope.getBody();
084:                if (body != null) {
085:                    return (body.hasFault() || body.getFault() != null);
086:                }
087:                return false;
088:            }
089:
090:            /**
091:             * Create an XMLFault object from a SOAPFault and detail Blocks
092:             *
093:             * @param soapFault
094:             * @param detailBlocks
095:             * @return
096:             */
097:            public static XMLFault createXMLFault(SOAPFault soapFault,
098:                    Block[] detailBlocks) throws WebServiceException {
099:
100:                // Here is a sample comprehensive SOAP 1.2 fault which will help you 
101:                // understand the structure.
102:                // <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
103:                //               xmlns:m="http://www.example.org/timeouts"
104:                //               xmlns:xml="http://www.w3.org/XML/1998/namespace">
105:                //   <env:Body>
106:                //      <env:Fault>
107:                //        <env:Code>
108:                //          <env:Value>env:Sender</env:Value>
109:                //          <env:Subcode>
110:                //            <env:Value>m:MessageTimeout</env:Value>
111:                //          </env:Subcode>
112:                //        </env:Code>
113:                //        <env:Reason>
114:                //          <env:Text xml:lang="en">Sender Timeout</env:Text>
115:                //          <env:Text xml:lang="de">Sender Timeout</env:Text>
116:                //        </env:Reason>
117:                //        <env:Node>http://my.example.org/Node</env:Node>
118:                //        <env:Role>http://my.example.org/Role</env:Role>
119:                //        <env:Detail>
120:                //          <m:MaxTime>P5M</m:MaxTime>
121:                //        </env:Detail>    
122:                //      </env:Fault>
123:                //   </env:Body>
124:                // </env:Envelope>
125:
126:                // Get the code
127:                // TODO what if this fails ?  Log a message and treat like a RECEIVER fault ?
128:
129:                //figureout the soap version
130:                boolean isSoap11 = soapFault.getNamespace().getNamespaceURI()
131:                        .equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
132:
133:                SOAPFaultCode soapCode = soapFault.getCode();
134:                QName codeQName = null;
135:                if (isSoap11) {
136:                    codeQName = soapCode.getTextAsQName();
137:                } else {
138:                    codeQName = soapCode.getValue().getTextAsQName();
139:                }
140:                XMLFaultCode code = XMLFaultCode.fromQName(codeQName);
141:
142:                // Get the primary reason text
143:                // TODO what if this fails
144:                SOAPFaultReason soapReason = soapFault.getReason();
145:                String text = null;
146:                String lang = null;
147:                List soapTexts = null;
148:                if (isSoap11) {
149:                    text = soapReason.getText();
150:                } else {
151:                    soapTexts = soapReason.getAllSoapTexts();
152:                    SOAPFaultText reasonText = (SOAPFaultText) soapTexts.get(0);
153:                    text = reasonText.getText();
154:                    lang = reasonText.getLang();
155:                }
156:                XMLFaultReason reason = new XMLFaultReason(text, lang);
157:
158:                // Construct the XMLFault from the required information (code, reason, detail blocks)
159:                XMLFault xmlFault = new XMLFault(code, reason, detailBlocks);
160:
161:                // Add the secondary fault information
162:
163:                // Get the SubCodes
164:                SOAPFaultSubCode soapSubCode = soapCode.getSubCode();
165:                if (soapSubCode != null) {
166:                    List<QName> list = new ArrayList<QName>();
167:
168:                    // Walk the nested sub codes and collect the qnames
169:                    while (soapSubCode != null) {
170:                        SOAPFaultValue soapSubCodeValue = soapSubCode
171:                                .getValue();
172:                        QName qName = soapSubCodeValue.getTextAsQName();
173:                        list.add(qName);
174:                        soapSubCode = soapSubCode.getSubCode();
175:                    }
176:
177:                    // Put the collected sub code qnames onto the xmlFault
178:                    QName[] qNames = new QName[list.size()];
179:                    xmlFault.setSubCodes(list.toArray(qNames));
180:                }
181:
182:                // Get the secondary Reasons...the first reason was already saved as the primary reason
183:                if (soapTexts != null && soapTexts.size() > 1) {
184:                    XMLFaultReason[] secondaryReasons = new XMLFaultReason[soapTexts
185:                            .size() - 1];
186:                    for (int i = 1; i < soapTexts.size(); i++) {
187:                        SOAPFaultText soapReasonText = (SOAPFaultText) soapTexts
188:                                .get(i);
189:                        secondaryReasons[i - 1] = new XMLFaultReason(
190:                                soapReasonText.getText(), soapReasonText
191:                                        .getLang());
192:                    }
193:                    xmlFault.setSecondaryReasons(secondaryReasons);
194:                }
195:
196:                // Get the Node
197:                SOAPFaultNode soapNode = soapFault.getNode();
198:                if (soapNode != null) {
199:                    xmlFault.setNode(soapNode.getText());
200:                }
201:
202:                // Get the Role
203:                SOAPFaultRole soapRole = soapFault.getRole();
204:                if (soapRole != null) {
205:                    xmlFault.setRole(soapRole.getText());
206:                }
207:                return xmlFault;
208:            }
209:
210:            /**
211:             * Create XMLFault
212:             *
213:             * @param soapFault
214:             * @return xmlFault
215:             * @throws WebServiceException
216:             */
217:            public static XMLFault createXMLFault(
218:                    javax.xml.soap.SOAPFault soapFault)
219:                    throws WebServiceException {
220:                Block[] detailBlocks = getDetailBlocks(soapFault);
221:                return createXMLFault(soapFault, detailBlocks);
222:            }
223:
224:            /**
225:             * Create an XMLFault object from a SOAPFault and detail Blocks
226:             *
227:             * @param soapFault
228:             * @param detailBlocks
229:             * @return
230:             */
231:            public static XMLFault createXMLFault(
232:                    javax.xml.soap.SOAPFault soapFault, Block[] detailBlocks)
233:                    throws WebServiceException {
234:
235:                // The SOAPFault structure is modeled after SOAP 1.2.  
236:                // Here is a sample comprehensive SOAP 1.2 fault which will help you understand the
237:                // structure.
238:                // <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
239:                //               xmlns:m="http://www.example.org/timeouts"
240:                //               xmlns:xml="http://www.w3.org/XML/1998/namespace">
241:                //   <env:Body>
242:                //      <env:Fault>
243:                //        <env:Code>
244:                //          <env:Value>env:Sender</env:Value>
245:                //          <env:Subcode>
246:                //            <env:Value>m:MessageTimeout</env:Value>
247:                //          </env:Subcode>
248:                //        </env:Code>
249:                //        <env:Reason>
250:                //          <env:Text xml:lang="en">Sender Timeout</env:Text>
251:                //          <env:Text xml:lang="de">Sender Timeout</env:Text>
252:                //        </env:Reason>
253:                //        <env:Node>http://my.example.org/Node</env:Node>
254:                //        <env:Role>http://my.example.org/Role</env:Role>
255:                //        <env:Detail>
256:                //          <m:MaxTime>P5M</m:MaxTime>
257:                //        </env:Detail>    
258:                //      </env:Fault>
259:                //   </env:Body>
260:                // </env:Envelope>
261:
262:                // Get the code or default code
263:                QName codeQName = soapFault.getFaultCodeAsQName();
264:                XMLFaultCode code = XMLFaultCode.fromQName(codeQName);
265:
266:                // Get the primary reason text
267:                // TODO what if this fails
268:                String text = soapFault.getFaultString();
269:                Locale locale = soapFault.getFaultStringLocale();
270:                XMLFaultReason reason = new XMLFaultReason(text,
271:                        localeToXmlLang(locale));
272:
273:                // Construct the XMLFault from the required information (code, reason, detail blocks)
274:                XMLFault xmlFault = new XMLFault(code, reason, detailBlocks);
275:
276:                boolean isSOAP12 = soapFault.getNamespaceURI().equals(
277:                        SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE);
278:
279:                // Add the secondary fault information
280:
281:                // Get the SubCodes
282:                if (isSOAP12) {
283:                    Iterator it = soapFault.getFaultSubcodes();
284:                    List<QName> list = new ArrayList<QName>();
285:                    while (it.hasNext()) {
286:                        QName qName = (QName) it.next();
287:                        list.add(qName);
288:                    }
289:                    if (list.size() > 0) {
290:                        QName[] subCodes = new QName[list.size()];
291:                        subCodes = list.toArray(subCodes);
292:                        xmlFault.setSubCodes(subCodes);
293:                    }
294:                }
295:
296:                // Get the secondary Reasons...the first reason was already saved as the primary reason
297:                if (isSOAP12) {
298:                    try {
299:                        Iterator it = soapFault.getFaultReasonLocales();
300:                        boolean first = true;
301:                        List<XMLFaultReason> list = new ArrayList<XMLFaultReason>();
302:                        while (it.hasNext()) {
303:                            locale = (Locale) it.next();
304:                            if (first) {
305:                                first = false;
306:                            } else {
307:                                text = soapFault.getFaultReasonText(locale);
308:                                list.add(new XMLFaultReason(text,
309:                                        localeToXmlLang(locale)));
310:                            }
311:                        }
312:                        if (list.size() > 0) {
313:                            XMLFaultReason[] secondaryReasons = new XMLFaultReason[list
314:                                    .size()];
315:                            secondaryReasons = list.toArray(secondaryReasons);
316:                            xmlFault.setSecondaryReasons(secondaryReasons);
317:                        }
318:                    } catch (SOAPException se) {
319:                        throw ExceptionFactory.makeWebServiceException(se);
320:                    }
321:                }
322:
323:                // Get the Node
324:                if (isSOAP12) {
325:                    String soapNode = soapFault.getFaultNode();
326:                    if (soapNode != null) {
327:                        xmlFault.setNode(soapNode);
328:                    }
329:                }
330:
331:                // Get the Role
332:                String soapRole = soapFault.getFaultActor(); // getFaultActor works for both SOAP 1.1 and SOAP 1.2 per spec
333:                if (soapRole != null) {
334:                    xmlFault.setRole(soapRole);
335:                }
336:
337:                return xmlFault;
338:            }
339:
340:            private static Block[] getDetailBlocks(SOAPFault soapFault)
341:                    throws WebServiceException {
342:                try {
343:                    Block[] blocks = null;
344:                    SOAPFaultDetail detail = soapFault.getDetail();
345:                    if (detail != null) {
346:                        // Create a block for each element
347:                        OMBlockFactory bf = (OMBlockFactory) FactoryRegistry
348:                                .getFactory(OMBlockFactory.class);
349:                        ArrayList<Block> list = new ArrayList<Block>();
350:                        Iterator it = detail.getChildElements();
351:                        while (it.hasNext()) {
352:                            OMElement om = (OMElement) it.next();
353:                            Block b = bf.createFrom(om, null, om.getQName());
354:                            list.add(b);
355:                        }
356:                        blocks = new Block[list.size()];
357:                        blocks = list.toArray(blocks);
358:                    }
359:                    return blocks;
360:                } catch (Exception e) {
361:                    throw ExceptionFactory.makeWebServiceException(e);
362:                }
363:            }
364:
365:            private static Block[] getDetailBlocks(
366:                    javax.xml.soap.SOAPFault soapFault)
367:                    throws WebServiceException {
368:                try {
369:                    Block[] blocks = null;
370:                    Detail detail = soapFault.getDetail();
371:                    if (detail != null) {
372:                        // Get a SAAJ->OM converter
373:                        SAAJConverterFactory converterFactory = (SAAJConverterFactory) FactoryRegistry
374:                                .getFactory(SAAJConverterFactory.class);
375:                        SAAJConverter converter = converterFactory
376:                                .getSAAJConverter();
377:
378:                        // Create a block for each element
379:                        OMBlockFactory bf = (OMBlockFactory) FactoryRegistry
380:                                .getFactory(OMBlockFactory.class);
381:                        ArrayList<Block> list = new ArrayList<Block>();
382:                        Iterator it = detail.getChildElements();
383:                        while (it.hasNext()) {
384:                            DetailEntry de = (DetailEntry) it.next();
385:                            OMElement om = converter.toOM(de);
386:                            Block b = bf.createFrom(om, null, om.getQName());
387:                            list.add(b);
388:                        }
389:                        blocks = new Block[list.size()];
390:                        blocks = list.toArray(blocks);
391:                    }
392:                    return blocks;
393:                } catch (Exception e) {
394:                    throw ExceptionFactory.makeWebServiceException(e);
395:                }
396:            }
397:
398:            /**
399:             * Create a SOAPFault representing the XMLFault and attach it to body. If there are 1 or more
400:             * detail Blocks on the XMLFault, a SOAPFaultDetail is attached. If ignoreDetailBlocks=false,
401:             * then OMElements are added to the SOAPFaultDetail. If ignoreDetailBlocks=true, then the Detail
402:             * Blocks are ignored (this is necessary for XMLSpine processing)
403:             *
404:             * @param xmlFault
405:             * @param body               - Assumes that the body is empty
406:             * @param ignoreDetailBlocks true or fals
407:             * @return SOAPFault (which is attached to body)
408:             */
409:            public static SOAPFault createSOAPFault(XMLFault xmlFault,
410:                    SOAPBody body, boolean ignoreDetailBlocks)
411:                    throws WebServiceException {
412:
413:                // Get the factory and create the soapFault
414:                SOAPFactory factory = MessageUtils.getSOAPFactory(body);
415:                SOAPFault soapFault = factory.createSOAPFault(body);
416:                OMNamespace ns = body.getNamespace();
417:
418:                // The SOAPFault structure is modeled after SOAP 1.2.  
419:                // Here is a sample comprehensive SOAP 1.2 fault which will help you understand the
420:                // structure.
421:                // <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
422:                //               xmlns:m="http://www.example.org/timeouts"
423:                //               xmlns:xml="http://www.w3.org/XML/1998/namespace">
424:                //   <env:Body>
425:                //      <env:Fault>
426:                //        <env:Code>
427:                //          <env:Value>env:Sender</env:Value>
428:                //          <env:Subcode>
429:                //            <env:Value>m:MessageTimeout</env:Value>
430:                //          </env:Subcode>
431:                //        </env:Code>
432:                //        <env:Reason>
433:                //          <env:Text xml:lang="en">Sender Timeout</env:Text>
434:                //          <env:Text xml:lang="de">Sender Timeout</env:Text>
435:                //        </env:Reason>
436:                //        <env:Node>http://my.example.org/Node</env:Node>
437:                //        <env:Role>http://my.example.org/Role</env:Role>
438:                //        <env:Detail>
439:                //          <m:MaxTime>P5M</m:MaxTime>
440:                //        </env:Detail>    
441:                //      </env:Fault>
442:                //   </env:Body>
443:                // </env:Envelope>
444:
445:                boolean isSoap11 = soapFault.getNamespace().getNamespaceURI()
446:                        .equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
447:
448:                // Set the primary Code Value
449:                SOAPFaultCode soapCode = factory.createSOAPFaultCode(soapFault);
450:                QName soapValueQName = xmlFault.getCode().toQName(
451:                        ns.getNamespaceURI());
452:                if (isSoap11) {
453:                    soapCode.setText(soapValueQName);
454:                } else {
455:                    SOAPFaultValue soapValue = factory
456:                            .createSOAPFaultValue(soapCode);
457:                    soapValue.setText(soapValueQName);
458:                }
459:
460:                // Set the primary Reason Text
461:                SOAPFaultReason soapReason = factory
462:                        .createSOAPFaultReason(soapFault);
463:                if (isSoap11) {
464:                    soapReason.setText(xmlFault.getReason().getText());
465:                } else {
466:                    SOAPFaultText soapText = factory
467:                            .createSOAPFaultText(soapReason);
468:                    soapText.setText(xmlFault.getReason().getText());
469:                    soapText.setLang(xmlFault.getReason().getLang());
470:                }
471:
472:                // Set the Detail and contents of Detail
473:                Block[] blocks = xmlFault.getDetailBlocks();
474:                if (blocks != null && blocks.length > 0) {
475:                    SOAPFaultDetail detail = factory
476:                            .createSOAPFaultDetail(soapFault);
477:                    if (!ignoreDetailBlocks) {
478:                        for (int i = 0; i < blocks.length; i++) {
479:                            // A Block implements OMDataSource.  So create OMSourcedElements
480:                            // for each of the Blocks.
481:                            OMSourcedElementImpl element = new OMSourcedElementImpl(
482:                                    blocks[i].getQName(), factory, blocks[i]);
483:                            detail.addChild(element);
484:                        }
485:                    }
486:                }
487:
488:                // Now set all of the secondary fault information
489:                // Set the SubCodes
490:                QName[] subCodes = xmlFault.getSubCodes();
491:                if (subCodes != null && subCodes.length > 0) {
492:                    OMElement curr = soapCode;
493:                    for (int i = 0; i < subCodes.length; i++) {
494:                        SOAPFaultSubCode subCode = (i == 0) ? factory
495:                                .createSOAPFaultSubCode((SOAPFaultCode) curr)
496:                                : factory
497:                                        .createSOAPFaultSubCode((SOAPFaultSubCode) curr);
498:                        SOAPFaultValue soapSubCodeValue = factory
499:                                .createSOAPFaultValue(subCode);
500:                        soapSubCodeValue.setText(subCodes[i]);
501:                        curr = subCode;
502:                    }
503:                }
504:
505:                // Set the secondary reasons and languages
506:                XMLFaultReason reasons[] = xmlFault.getSecondaryReasons();
507:                if (reasons != null && reasons.length > 0) {
508:                    for (int i = 0; i < reasons.length; i++) {
509:                        SOAPFaultText soapReasonText = factory
510:                                .createSOAPFaultText(soapReason);
511:                        soapReasonText.setText(reasons[i].getText());
512:                        soapReasonText.setLang(reasons[i].getLang());
513:                    }
514:                }
515:
516:                // Set the Role
517:                if (xmlFault.getRole() != null) {
518:                    SOAPFaultRole soapRole = factory.createSOAPFaultRole();
519:                    soapRole.setText(xmlFault.getRole());
520:                    soapFault.setRole(soapRole);
521:                }
522:
523:                // Set the Node
524:                if (xmlFault.getNode() != null) {
525:                    SOAPFaultNode soapNode = factory.createSOAPFaultNode();
526:                    soapNode.setText(xmlFault.getNode());
527:                    soapFault.setNode(soapNode);
528:                }
529:
530:                return soapFault;
531:
532:            }
533:
534:            /**
535:             * Create a SOAPFault representing the XMLFault. If there are 1 or more detail Blocks on the
536:             * XMLFault, a SOAPFaultDetail is attached.
537:             *
538:             * @param xmlFault
539:             * @param body
540:             * @return SOAPFault (which is attached to body)
541:             */
542:            public static javax.xml.soap.SOAPFault createSAAJFault(
543:                    XMLFault xmlFault, javax.xml.soap.SOAPBody body)
544:                    throws SOAPException, WebServiceException {
545:
546:                // Get the factory and create the soapFault
547:                String protocolNS = body.getNamespaceURI();
548:
549:                javax.xml.soap.SOAPFault soapFault = body.addFault();
550:
551:                // The SOAPFault structure is modeled after SOAP 1.2.  
552:                // Here is a sample comprehensive SOAP 1.2 fault which will help you understand the
553:                // structure.
554:                // <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
555:                //               xmlns:m="http://www.example.org/timeouts"
556:                //               xmlns:xml="http://www.w3.org/XML/1998/namespace">
557:                //   <env:Body>
558:                //      <env:Fault>
559:                //        <env:Code>
560:                //          <env:Value>env:Sender</env:Value>
561:                //          <env:Subcode>
562:                //            <env:Value>m:MessageTimeout</env:Value>
563:                //          </env:Subcode>
564:                //        </env:Code>
565:                //        <env:Reason>
566:                //          <env:Text xml:lang="en">Sender Timeout</env:Text>
567:                //          <env:Text xml:lang="de">Sender Timeout</env:Text>
568:                //        </env:Reason>
569:                //        <env:Node>http://my.example.org/Node</env:Node>
570:                //        <env:Role>http://my.example.org/Role</env:Role>
571:                //        <env:Detail>
572:                //          <m:MaxTime>P5M</m:MaxTime>
573:                //        </env:Detail>    
574:                //      </env:Fault>
575:                //   </env:Body>
576:                // </env:Envelope>
577:
578:                // Set the primary Code Value
579:                QName soapValueQName = xmlFault.getCode().toQName(protocolNS);
580:                String prefix = soapFault.getPrefix();
581:                String soapValue = null;
582:                if (prefix == null || prefix.length() == 0) {
583:                    soapValue = soapValueQName.getLocalPart();
584:                } else {
585:                    soapValue = prefix + ":" + soapValueQName.getLocalPart();
586:                }
587:                soapFault.setFaultCode(soapValue);
588:
589:                // Set the primary Reason Text
590:                String reasonText = xmlFault.getReason().getText();
591:                String reasonLang = xmlFault.getReason().getLang();
592:                Locale locale = (reasonLang != null && reasonLang.length() > 0) ? new Locale(
593:                        reasonLang)
594:                        : Locale.getDefault();
595:                soapFault.setFaultString(reasonText, locale);
596:
597:                // Set the Detail and contents of Detail
598:                Block[] blocks = xmlFault.getDetailBlocks();
599:                if (blocks != null && blocks.length > 0) {
600:                    Detail detail = soapFault.addDetail();
601:                    // Get a OM->SAAJ converter
602:                    SAAJConverterFactory converterFactory = (SAAJConverterFactory) FactoryRegistry
603:                            .getFactory(SAAJConverterFactory.class);
604:                    SAAJConverter converter = converterFactory
605:                            .getSAAJConverter();
606:                    for (int i = 0; i < blocks.length; i++) {
607:                        try {
608:                            converter.toSAAJ(blocks[i].getOMElement(), detail);
609:                        } catch (XMLStreamException xse) {
610:                            ExceptionFactory.makeWebServiceException(xse);
611:                        }
612:                    }
613:
614:                }
615:
616:                // Now set all of the secondary fault information
617:                // Set the SubCodes
618:                QName[] subCodes = xmlFault.getSubCodes();
619:                if (subCodes != null
620:                        && subCodes.length > 0
621:                        && protocolNS
622:                                .equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE)) {
623:                    for (int i = 0; i < subCodes.length; i++) {
624:                        soapFault.appendFaultSubcode(subCodes[i]);
625:                    }
626:                }
627:
628:                // Set the secondary reasons and languages
629:                XMLFaultReason reasons[] = xmlFault.getSecondaryReasons();
630:                if (reasons != null
631:                        && reasons.length > 0
632:                        && protocolNS
633:                                .equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE)) {
634:                    for (int i = 0; i < reasons.length; i++) {
635:                        if (reasons[i].getLang() == null
636:                                || reasons[i].getLang().length() == 0) {
637:                            locale = Locale.getDefault();
638:                        } else {
639:                            locale = new Locale(reasons[i].getLang());
640:                        }
641:                        soapFault.addFaultReasonText(reasons[i].getText(),
642:                                locale);
643:                    }
644:                }
645:
646:                // Set the Role
647:                if (xmlFault.getRole() != null) {
648:                    soapFault.setFaultActor(xmlFault.getRole()); // Use Fault actor because it is applicable for SOAP 1.1 and SOAP 1.2
649:                }
650:
651:                // Set the Node...only applicable for SOAP 1.2
652:                if (xmlFault.getNode() != null
653:                        && protocolNS
654:                                .equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE)) {
655:                    soapFault.setFaultRole(xmlFault.getNode());
656:                }
657:
658:                return soapFault;
659:
660:            }
661:
662:            /**
663:             * Converte a Locale object to an xmlLang String
664:             * @param locale
665:             * @return String of the form <locale.getLanguage()>-<locale.getCountry()>
666:             */
667:            private static String localeToXmlLang(Locale locale) {
668:                if (locale == null) {
669:                    return Locale.getDefault().getLanguage() + "-"
670:                            + Locale.getDefault().getCountry();
671:                }
672:                String lang = locale.getLanguage();
673:                String countryCode = locale.getCountry();
674:                if (countryCode == null || countryCode.length() == 0) {
675:                    return lang;
676:                } else {
677:                    return new String(lang + "-" + countryCode);
678:                }
679:
680:            }
681:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.