xmlfaultutils.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 659 行 · 第 1/2 页
JAVA
659 行
return xmlFault;
}
private static Block[] getDetailBlocks(SOAPFault soapFault) throws WebServiceException {
try {
Block[] blocks = null;
SOAPFaultDetail detail = soapFault.getDetail();
if (detail != null) {
// Create a block for each element
OMBlockFactory bf =
(OMBlockFactory)FactoryRegistry.getFactory(OMBlockFactory.class);
ArrayList<Block> list = new ArrayList<Block>();
Iterator it = detail.getChildElements();
while (it.hasNext()) {
OMElement om = (OMElement)it.next();
Block b = bf.createFrom(om, null, om.getQName());
list.add(b);
}
blocks = new Block[list.size()];
blocks = list.toArray(blocks);
}
return blocks;
} catch (Exception e) {
throw ExceptionFactory.makeWebServiceException(e);
}
}
private static Block[] getDetailBlocks(javax.xml.soap.SOAPFault soapFault)
throws WebServiceException {
try {
Block[] blocks = null;
Detail detail = soapFault.getDetail();
if (detail != null) {
// Get a SAAJ->OM converter
SAAJConverterFactory converterFactory = (SAAJConverterFactory)FactoryRegistry
.getFactory(SAAJConverterFactory.class);
SAAJConverter converter = converterFactory.getSAAJConverter();
// Create a block for each element
OMBlockFactory bf =
(OMBlockFactory)FactoryRegistry.getFactory(OMBlockFactory.class);
ArrayList<Block> list = new ArrayList<Block>();
Iterator it = detail.getChildElements();
while (it.hasNext()) {
DetailEntry de = (DetailEntry)it.next();
OMElement om = converter.toOM(de);
Block b = bf.createFrom(om, null, om.getQName());
list.add(b);
}
blocks = new Block[list.size()];
blocks = list.toArray(blocks);
}
return blocks;
} catch (Exception e) {
throw ExceptionFactory.makeWebServiceException(e);
}
}
/**
* Create a SOAPFault representing the XMLFault and attach it to body. If there are 1 or more
* detail Blocks on the XMLFault, a SOAPFaultDetail is attached. If ignoreDetailBlocks=false,
* then OMElements are added to the SOAPFaultDetail. If ignoreDetailBlocks=true, then the Detail
* Blocks are ignored (this is necessary for XMLSpine processing)
*
* @param xmlFault
* @param body - Assumes that the body is empty
* @param ignoreDetailBlocks true or fals
* @return SOAPFault (which is attached to body)
*/
public static SOAPFault createSOAPFault(XMLFault xmlFault,
SOAPBody body,
boolean ignoreDetailBlocks) throws WebServiceException {
// Get the factory and create the soapFault
SOAPFactory factory = MessageUtils.getSOAPFactory(body);
SOAPFault soapFault = factory.createSOAPFault(body);
OMNamespace ns = body.getNamespace();
// 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>
boolean isSoap11 = soapFault.getNamespace().getNamespaceURI()
.equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Set the primary Code Value
SOAPFaultCode soapCode = factory.createSOAPFaultCode(soapFault);
QName soapValueQName = xmlFault.getCode().toQName(ns.getNamespaceURI());
if (isSoap11) {
soapCode.setText(soapValueQName);
} else {
SOAPFaultValue soapValue = factory.createSOAPFaultValue(soapCode);
soapValue.setText(soapValueQName);
}
// Set the primary Reason Text
SOAPFaultReason soapReason = factory.createSOAPFaultReason(soapFault);
if (isSoap11) {
soapReason.setText(xmlFault.getReason().getText());
} else {
SOAPFaultText soapText = factory.createSOAPFaultText(soapReason);
soapText.setText(xmlFault.getReason().getText());
soapText.setLang(xmlFault.getReason().getLang());
}
// Set the Detail and contents of Detail
Block[] blocks = xmlFault.getDetailBlocks();
if (blocks != null && blocks.length > 0) {
SOAPFaultDetail detail = factory.createSOAPFaultDetail(soapFault);
if (!ignoreDetailBlocks) {
for (int i = 0; i < blocks.length; i++) {
// A Block implements OMDataSource. So create OMSourcedElements
// for each of the Blocks.
OMSourcedElementImpl element =
new OMSourcedElementImpl(blocks[i].getQName(), factory, blocks[i]);
detail.addChild(element);
}
}
}
// Now set all of the secondary fault information
// Set the SubCodes
QName[] subCodes = xmlFault.getSubCodes();
if (subCodes != null && subCodes.length > 0) {
OMElement curr = soapCode;
for (int i = 0; i < subCodes.length; i++) {
SOAPFaultSubCode subCode = (i == 0) ?
factory.createSOAPFaultSubCode((SOAPFaultCode)curr) :
factory.createSOAPFaultSubCode((SOAPFaultSubCode)curr);
SOAPFaultValue soapSubCodeValue = factory.createSOAPFaultValue(subCode);
soapSubCodeValue.setText(subCodes[i]);
curr = subCode;
}
}
// Set the secondary reasons and languages
XMLFaultReason reasons[] = xmlFault.getSecondaryReasons();
if (reasons != null && reasons.length > 0) {
for (int i = 0; i < reasons.length; i++) {
SOAPFaultText soapReasonText = factory.createSOAPFaultText(soapReason);
soapReasonText.setText(reasons[i].getText());
soapReasonText.setLang(reasons[i].getLang());
}
}
// Set the Role
if (xmlFault.getRole() != null) {
SOAPFaultRole soapRole = factory.createSOAPFaultRole();
soapRole.setText(xmlFault.getRole());
soapFault.setRole(soapRole);
}
// Set the Node
if (xmlFault.getNode() != null) {
SOAPFaultNode soapNode = factory.createSOAPFaultNode();
soapNode.setText(xmlFault.getNode());
soapFault.setNode(soapNode);
}
return soapFault;
}
/**
* Create a SOAPFault representing the XMLFault. If there are 1 or more detail Blocks on the
* XMLFault, a SOAPFaultDetail is attached.
*
* @param xmlFault
* @param body
* @return SOAPFault (which is attached to body)
*/
public static javax.xml.soap.SOAPFault createSAAJFault(XMLFault xmlFault,
javax.xml.soap.SOAPBody body)
throws SOAPException, WebServiceException {
// Get the factory and create the soapFault
String protocolNS = body.getNamespaceURI();
javax.xml.soap.SOAPFault soapFault = body.addFault();
// 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>
// Set the primary Code Value
QName soapValueQName = xmlFault.getCode().toQName(protocolNS);
String prefix = soapFault.getPrefix();
String soapValue = null;
if (prefix == null || prefix.length() == 0) {
soapValue = soapValueQName.getLocalPart();
} else {
soapValue = prefix + ":" + soapValueQName.getLocalPart();
}
soapFault.setFaultCode(soapValue);
// Set the primary Reason Text
String reasonText = xmlFault.getReason().getText();
String reasonLang = xmlFault.getReason().getLang();
Locale locale = (reasonLang != null && reasonLang.length() > 0) ?
new Locale(reasonLang) :
Locale.getDefault();
soapFault.setFaultString(reasonText, locale);
// Set the Detail and contents of Detail
Block[] blocks = xmlFault.getDetailBlocks();
if (blocks != null && blocks.length > 0) {
Detail detail = soapFault.addDetail();
// Get a OM->SAAJ converter
SAAJConverterFactory converterFactory =
(SAAJConverterFactory)FactoryRegistry.getFactory(SAAJConverterFactory.class);
SAAJConverter converter = converterFactory.getSAAJConverter();
for (int i = 0; i < blocks.length; i++) {
try {
converter.toSAAJ(blocks[i].getOMElement(), detail);
} catch (XMLStreamException xse) {
ExceptionFactory.makeWebServiceException(xse);
}
}
}
// Now set all of the secondary fault information
// Set the SubCodes
QName[] subCodes = xmlFault.getSubCodes();
if (subCodes != null && subCodes.length > 0 &&
protocolNS.equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE)) {
for (int i = 0; i < subCodes.length; i++) {
soapFault.appendFaultSubcode(subCodes[i]);
}
}
// Set the secondary reasons and languages
XMLFaultReason reasons[] = xmlFault.getSecondaryReasons();
if (reasons != null && reasons.length > 0 &&
protocolNS.equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE)) {
for (int i = 0; i < reasons.length; i++) {
if (reasons[i].getLang() == null || reasons[i].getLang().length() == 0) {
locale = Locale.getDefault();
} else {
locale = new Locale(reasons[i].getLang());
}
soapFault.addFaultReasonText(reasons[i].getText(), locale);
}
}
// Set the Role
if (xmlFault.getRole() != null) {
soapFault.setFaultActor(
xmlFault.getRole()); // Use Fault actor because it is applicable for SOAP 1.1 and SOAP 1.2
}
// Set the Node...only applicable for SOAP 1.2
if (xmlFault.getNode() != null && protocolNS.equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE))
{
soapFault.setFaultRole(xmlFault.getNode());
}
return soapFault;
}
/**
* Converte a Locale object to an xmlLang String
* @param locale
* @return String of the form <locale.getLanguage()>-<locale.getCountry()>
*/
private static String localeToXmlLang(Locale locale) {
if (locale == null) {
return Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
}
String lang = locale.getLanguage();
String countryCode = locale.getCountry();
if (countryCode == null || countryCode.length() == 0) {
return lang;
} else {
return new String(lang + "-" + countryCode);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?