📄 defaultsoapfaultwriter.java
字号:
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
* published by JBoss Inc.; either version 1.0 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.jbpm.bpel.integration.server;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.rpc.soap.SOAPFaultException;
import javax.xml.soap.Detail;
import javax.xml.soap.DetailEntry;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Element;
import org.jbpm.bpel.xml.util.XmlUtil;
/**
* @author Alejandro Guizar
* @version $Revision: 1.1 $ $Date: 2007/01/18 12:58:28 $
*/
public class DefaultSoapFaultWriter implements SoapFaultWriter {
public void writeFault(SOAPEnvelope envelope, SOAPFaultException exception,
Map parts) throws SOAPException {
SOAPBody body = envelope.getBody();
// remove existing children
body.removeContents();
// add a soapenv:Fault element
SOAPFault fault = body.addFault();
writeFaultCode(fault, exception.getFaultCode());
writeFaultString(fault, exception.getFaultString());
if (parts != null)
writeDetail(fault, parts);
}
private static void writeFaultCode(SOAPFault fault, QName code)
throws SOAPException {
String codeNamespace = code.getNamespaceURI();
// is code in a namespace?
if (codeNamespace.length() > 0) {
String codePrefix = XmlUtil.getPrefix(codeNamespace, fault);
// isn't the namespace declared?
if (codePrefix == null)
// propose an arbitrary prefix
codePrefix = "faultCodeNS";
// set code as qualified name
SOAPEnvelope envelope = (SOAPEnvelope) fault.getParentElement()
.getParentElement();
fault.setFaultCode(envelope.createName(code.getLocalPart(), codePrefix,
codeNamespace));
}
else {
// set code as local name
fault.setFaultCode(code.getLocalPart());
}
}
private static void writeFaultString(SOAPFault fault, String string)
throws SOAPException {
fault.setFaultString(string);
}
private static void writeDetail(SOAPFault fault, Map parts)
throws SOAPException {
// create detail element
Detail detail = fault.addDetail();
// iterate through the fault message parts
SOAPEnvelope envelope = (SOAPEnvelope) fault.getParentElement()
.getParentElement();
Iterator partIt = parts.values().iterator();
while (partIt.hasNext()) {
Element part = (Element) partIt.next();
writeDetailEntry(detail, part, envelope);
}
}
private static void writeDetailEntry(Detail detail, Element part,
SOAPEnvelope envelope) throws SOAPException {
String partLocalName = part.getLocalName();
String partNamespace = part.getNamespaceURI();
// obtain the detail entry name from the part name
Name entryName;
if (StringUtils.isEmpty(partNamespace)) {
entryName = envelope.createName(partLocalName);
}
else {
entryName = envelope.createName(partLocalName, XmlUtil.getPrefix(
partNamespace, part), partNamespace);
}
// create detail entry element
DetailEntry detailEntry = detail.addDetailEntry(entryName);
// copy fault part to detail entry
XmlUtil.copy(detailEntry, part);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -