xmlfaultutils.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 659 行 · 第 1/2 页
JAVA
659 行
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.axis2.jaxws.message.util;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPFaultCode;
import org.apache.axiom.soap.SOAPFaultDetail;
import org.apache.axiom.soap.SOAPFaultNode;
import org.apache.axiom.soap.SOAPFaultReason;
import org.apache.axiom.soap.SOAPFaultRole;
import org.apache.axiom.soap.SOAPFaultSubCode;
import org.apache.axiom.soap.SOAPFaultText;
import org.apache.axiom.soap.SOAPFaultValue;
import org.apache.axis2.jaxws.ExceptionFactory;
import org.apache.axis2.jaxws.message.Block;
import org.apache.axis2.jaxws.message.XMLFault;
import org.apache.axis2.jaxws.message.XMLFaultCode;
import org.apache.axis2.jaxws.message.XMLFaultReason;
import org.apache.axis2.jaxws.message.factory.OMBlockFactory;
import org.apache.axis2.jaxws.message.factory.SAAJConverterFactory;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import javax.xml.namespace.QName;
import javax.xml.soap.Detail;
import javax.xml.soap.DetailEntry;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
import javax.xml.stream.XMLStreamException;
import javax.xml.ws.WebServiceException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
/**
* Collection of utilities used by the Message implementation to process XMLFaults.
*
* @see XMLFault
*/
public class XMLFaultUtils {
/**
* @param envelope javax.xml.soap.SOAPEnvelope
* @return true if the SOAPEnvelope contains a SOAPFault
*/
public static boolean isFault(javax.xml.soap.SOAPEnvelope envelope) throws SOAPException {
javax.xml.soap.SOAPBody body = envelope.getBody();
if (body != null) {
return (body.getFault() != null);
}
return false;
}
/**
* @param envelope org.apache.axiom.soap.SOAPEnvelope
* @return true if the SOAPEnvelope contains a SOAPFault
*/
public static boolean isFault(SOAPEnvelope envelope) {
SOAPBody body = envelope.getBody();
if (body != null) {
return (body.hasFault() || body.getFault() != null);
}
return false;
}
/**
* Create an XMLFault object from a SOAPFault and detail Blocks
*
* @param soapFault
* @param detailBlocks
* @return
*/
public static XMLFault createXMLFault(SOAPFault soapFault, Block[] detailBlocks)
throws WebServiceException {
// Here is a sample comprehensive SOAP 1.2 fault which will help you
// understand the structure.
// <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
// xmlns:m="http://www.example.org/timeouts"
// xmlns:xml="http://www.w3.org/XML/1998/namespace">
// <env:Body>
// <env:Fault>
// <env:Code>
// <env:Value>env:Sender</env:Value>
// <env:Subcode>
// <env:Value>m:MessageTimeout</env:Value>
// </env:Subcode>
// </env:Code>
// <env:Reason>
// <env:Text xml:lang="en">Sender Timeout</env:Text>
// <env:Text xml:lang="de">Sender Timeout</env:Text>
// </env:Reason>
// <env:Node>http://my.example.org/Node</env:Node>
// <env:Role>http://my.example.org/Role</env:Role>
// <env:Detail>
// <m:MaxTime>P5M</m:MaxTime>
// </env:Detail>
// </env:Fault>
// </env:Body>
// </env:Envelope>
// Get the code
// TODO what if this fails ? Log a message and treat like a RECEIVER fault ?
//figureout the soap version
boolean isSoap11 = soapFault.getNamespace().getNamespaceURI().equals(
SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
SOAPFaultCode soapCode = soapFault.getCode();
QName codeQName = null;
if (isSoap11) {
codeQName = soapCode.getTextAsQName();
} else {
codeQName = soapCode.getValue().getTextAsQName();
}
XMLFaultCode code = XMLFaultCode.fromQName(codeQName);
// Get the primary reason text
// TODO what if this fails
SOAPFaultReason soapReason = soapFault.getReason();
String text = null;
String lang = null;
List soapTexts = null;
if (isSoap11) {
text = soapReason.getText();
} else {
soapTexts = soapReason.getAllSoapTexts();
SOAPFaultText reasonText = (SOAPFaultText)soapTexts.get(0);
text = reasonText.getText();
lang = reasonText.getLang();
}
XMLFaultReason reason = new XMLFaultReason(text, lang);
// Construct the XMLFault from the required information (code, reason, detail blocks)
XMLFault xmlFault = new XMLFault(code, reason, detailBlocks);
// Add the secondary fault information
// Get the SubCodes
SOAPFaultSubCode soapSubCode = soapCode.getSubCode();
if (soapSubCode != null) {
List<QName> list = new ArrayList<QName>();
// Walk the nested sub codes and collect the qnames
while (soapSubCode != null) {
SOAPFaultValue soapSubCodeValue = soapSubCode.getValue();
QName qName = soapSubCodeValue.getTextAsQName();
list.add(qName);
soapSubCode = soapSubCode.getSubCode();
}
// Put the collected sub code qnames onto the xmlFault
QName[] qNames = new QName[list.size()];
xmlFault.setSubCodes(list.toArray(qNames));
}
// Get the secondary Reasons...the first reason was already saved as the primary reason
if (soapTexts != null && soapTexts.size() > 1) {
XMLFaultReason[] secondaryReasons = new XMLFaultReason[soapTexts.size() - 1];
for (int i = 1; i < soapTexts.size(); i++) {
SOAPFaultText soapReasonText = (SOAPFaultText)soapTexts.get(i);
secondaryReasons[i - 1] = new XMLFaultReason(soapReasonText.getText(),
soapReasonText.getLang());
}
xmlFault.setSecondaryReasons(secondaryReasons);
}
// Get the Node
SOAPFaultNode soapNode = soapFault.getNode();
if (soapNode != null) {
xmlFault.setNode(soapNode.getText());
}
// Get the Role
SOAPFaultRole soapRole = soapFault.getRole();
if (soapRole != null) {
xmlFault.setRole(soapRole.getText());
}
return xmlFault;
}
/**
* Create XMLFault
*
* @param soapFault
* @return xmlFault
* @throws WebServiceException
*/
public static XMLFault createXMLFault(javax.xml.soap.SOAPFault soapFault)
throws WebServiceException {
Block[] detailBlocks = getDetailBlocks(soapFault);
return createXMLFault(soapFault, detailBlocks);
}
/**
* Create an XMLFault object from a SOAPFault and detail Blocks
*
* @param soapFault
* @param detailBlocks
* @return
*/
public static XMLFault createXMLFault(javax.xml.soap.SOAPFault soapFault, Block[] detailBlocks)
throws WebServiceException {
// The SOAPFault structure is modeled after SOAP 1.2.
// Here is a sample comprehensive SOAP 1.2 fault which will help you understand the
// structure.
// <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
// xmlns:m="http://www.example.org/timeouts"
// xmlns:xml="http://www.w3.org/XML/1998/namespace">
// <env:Body>
// <env:Fault>
// <env:Code>
// <env:Value>env:Sender</env:Value>
// <env:Subcode>
// <env:Value>m:MessageTimeout</env:Value>
// </env:Subcode>
// </env:Code>
// <env:Reason>
// <env:Text xml:lang="en">Sender Timeout</env:Text>
// <env:Text xml:lang="de">Sender Timeout</env:Text>
// </env:Reason>
// <env:Node>http://my.example.org/Node</env:Node>
// <env:Role>http://my.example.org/Role</env:Role>
// <env:Detail>
// <m:MaxTime>P5M</m:MaxTime>
// </env:Detail>
// </env:Fault>
// </env:Body>
// </env:Envelope>
// Get the code or default code
QName codeQName = soapFault.getFaultCodeAsQName();
XMLFaultCode code = XMLFaultCode.fromQName(codeQName);
// Get the primary reason text
// TODO what if this fails
String text = soapFault.getFaultString();
Locale locale = soapFault.getFaultStringLocale();
XMLFaultReason reason = new XMLFaultReason(text, localeToXmlLang(locale));
// Construct the XMLFault from the required information (code, reason, detail blocks)
XMLFault xmlFault = new XMLFault(code, reason, detailBlocks);
boolean isSOAP12 =
soapFault.getNamespaceURI().equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE);
// Add the secondary fault information
// Get the SubCodes
if (isSOAP12) {
Iterator it = soapFault.getFaultSubcodes();
List<QName> list = new ArrayList<QName>();
while (it.hasNext()) {
QName qName = (QName)it.next();
list.add(qName);
}
if (list.size() > 0) {
QName[] subCodes = new QName[list.size()];
subCodes = list.toArray(subCodes);
xmlFault.setSubCodes(subCodes);
}
}
// Get the secondary Reasons...the first reason was already saved as the primary reason
if (isSOAP12) {
try {
Iterator it = soapFault.getFaultReasonLocales();
boolean first = true;
List<XMLFaultReason> list = new ArrayList<XMLFaultReason>();
while (it.hasNext()) {
locale = (Locale)it.next();
if (first) {
first = false;
} else {
text = soapFault.getFaultReasonText(locale);
list.add(new XMLFaultReason(text, localeToXmlLang(locale)));
}
}
if (list.size() > 0) {
XMLFaultReason[] secondaryReasons = new XMLFaultReason[list.size()];
secondaryReasons = list.toArray(secondaryReasons);
xmlFault.setSecondaryReasons(secondaryReasons);
}
} catch (SOAPException se) {
throw ExceptionFactory.makeWebServiceException(se);
}
}
// Get the Node
if (isSOAP12) {
String soapNode = soapFault.getFaultNode();
if (soapNode != null) {
xmlFault.setNode(soapNode);
}
}
// Get the Role
String soapRole = soapFault
.getFaultActor(); // getFaultActor works for both SOAP 1.1 and SOAP 1.2 per spec
if (soapRole != null) {
xmlFault.setRole(soapRole);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?