saajconverterimpl.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 641 行 · 第 1/2 页
JAVA
641 行
break;
}
case XMLStreamReader.COMMENT: {
// SOAP really doesn't have an adequate representation for comments.
// The defacto standard is to add the whole element as a text node.
parent.addTextNode("<!--" + reader.getText() + "-->");
break;
}
case XMLStreamReader.SPACE: {
parent.addTextNode(reader.getText());
break;
}
case XMLStreamReader.START_DOCUMENT: {
// Ignore
break;
}
case XMLStreamReader.END_DOCUMENT: {
// Close reader and ignore
reader.close();
break;
}
case XMLStreamReader.PROCESSING_INSTRUCTION: {
// Ignore
break;
}
case XMLStreamReader.ENTITY_REFERENCE: {
// Ignore. this is unexpected in a web service message
break;
}
case XMLStreamReader.DTD: {
// Ignore. this is unexpected in a web service message
break;
}
default:
this._unexpectedEvent("EventID " + String.valueOf(eventID));
}
}
} catch (WebServiceException e) {
throw e;
} catch (XMLStreamException e) {
throw ExceptionFactory.makeWebServiceException(e);
} catch (SOAPException e) {
throw ExceptionFactory.makeWebServiceException(e);
}
return root;
}
/**
* Create SOAPElement from the current tag data
*
* @param nc NameCreator
* @param parent SOAPElement for the new SOAPElement
* @param reader XMLStreamReader whose cursor is at the START_ELEMENT
* @return
*/
protected SOAPElement createElementFromTag(NameCreator nc,
SOAPElement parent,
XMLStreamReader reader)
throws SOAPException {
// Unfortunately, the SAAJ object is a product of both the
// QName of the element and the parent object. For example,
// All element children of a SOAPBody must be object's that are SOAPBodyElements.
// createElement creates the proper child element.
QName qName = reader.getName();
SOAPElement child = createElement(parent, qName);
// Update the tag data on the child
updateTagData(nc, child, reader, true);
return child;
}
/**
* Create child SOAPElement
*
* @param parent SOAPElement
* @param name Name
* @return
*/
protected SOAPElement createElement(SOAPElement parent, QName qName)
throws SOAPException {
SOAPElement child;
if (parent instanceof SOAPEnvelope) {
if (qName.getNamespaceURI().equals(parent.getNamespaceURI())) {
if (qName.getLocalPart().equals("Body")) {
child = ((SOAPEnvelope)parent).addBody();
} else {
child = ((SOAPEnvelope)parent).addHeader();
}
} else {
child = parent.addChildElement(qName);
}
} else if (parent instanceof SOAPBody) {
if (qName.getNamespaceURI().equals(parent.getNamespaceURI()) &&
qName.getLocalPart().equals("Fault")) {
child = ((SOAPBody)parent).addFault();
} else {
child = ((SOAPBody)parent).addBodyElement(qName);
}
} else if (parent instanceof SOAPHeader) {
child = ((SOAPHeader)parent).addHeaderElement(qName);
} else if (parent instanceof SOAPFault) {
// This call assumes that the addChildElement implementation
// is smart enough to add "Detail" or "SOAPFaultElement" objects.
child = parent.addChildElement(qName);
} else if (parent instanceof Detail) {
child = ((Detail)parent).addDetailEntry(qName);
} else {
child = parent.addChildElement(qName);
}
return child;
}
/**
* update the tag data of the SOAPElement
*
* @param NameCreator nc
* @param element SOAPElement
* @param reader XMLStreamReader whose cursor is at START_ELEMENT
*/
protected void updateTagData(NameCreator nc,
SOAPElement element,
XMLStreamReader reader,
boolean newElement) throws SOAPException {
String prefix = reader.getPrefix();
prefix = (prefix == null) ? "" : prefix;
// Make sure the prefix is correct
if (prefix.length() > 0 && !element.getPrefix().equals(prefix)) {
// Due to a bug in Axiom DOM or in the reader...not sure where yet,
// there may be a non-null prefix and no namespace
String ns = reader.getNamespaceURI();
if (ns != null && ns.length() != 0) {
element.setPrefix(prefix);
}
}
if (!newElement) {
// Add the namespace declarations from the reader for the missing namespaces
int size = reader.getNamespaceCount();
for (int i=0; i<size; i++) {
String pre = reader.getNamespacePrefix(i);
String ns = reader.getNamespaceURI(i);
String existingNS = element.getNamespaceURI(pre);
if (!ns.equals(existingNS)) {
element.removeNamespaceDeclaration(pre); // Is it necessary to remove the existing prefix/ns
element.addNamespaceDeclaration(pre, ns);
}
}
} else {
// Add the namespace declarations from the reader
int size = reader.getNamespaceCount();
for (int i=0; i<size; i++) {
element.addNamespaceDeclaration(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
}
}
addAttributes(nc, element, reader);
return;
}
/**
* add attributes
*
* @param NameCreator nc
* @param element SOAPElement which is the target of the new attributes
* @param reader XMLStreamReader whose cursor is at START_ELEMENT
* @throws SOAPException
*/
protected void addAttributes(NameCreator nc,
SOAPElement element,
XMLStreamReader reader) throws SOAPException {
// Add the attributes from the reader
int size = reader.getAttributeCount();
for (int i = 0; i < size; i++) {
QName qName = reader.getAttributeName(i);
String prefix = reader.getAttributePrefix(i);
String value = reader.getAttributeValue(i);
Name name = nc.createName(qName.getLocalPart(), prefix, qName.getNamespaceURI());
element.addAttribute(name, value);
}
}
private void _unexpectedEvent(String event) throws WebServiceException {
// Review We need NLS for this message, but this code will probably
// be added to JAX-WS. So for now we there is no NLS.
// TODO NLS
throw ExceptionFactory
.makeWebServiceException(Messages.getMessage("SAAJConverterErr2", event));
}
/*
* A utility method to fix the localnames of elements with an Axis2 SAAJ
* tree. The SAAJ impl relies on the Axiom SOAP APIs, which represent
* all faults as SOAP 1.2. This has to be corrected before we can convert
* to OM or the faults will not be handled correctly.
*/
private void _fixFaultElements(SOAPEnvelope env) {
try {
// If we have a SOAP 1.2 envelope, then there's nothing to do.
if (env.getNamespaceURI().equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
return;
}
SOAPBody body = env.getBody();
if (body != null && !body.hasFault()) {
if (log.isDebugEnabled()) {
log.debug("No fault found. No conversion necessary.");
}
return;
}
else if (body != null && body.hasFault()) {
if (log.isDebugEnabled()) {
log.debug("A fault was found. Converting the fault child elements to SOAP 1.1 format");
}
SOAPFault fault = body.getFault();
Iterator itr = fault.getChildElements();
while (itr.hasNext()) {
SOAPElement se = (SOAPElement) itr.next();
if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME)) {
if (log.isDebugEnabled()) {
log.debug("Converting: faultcode");
}
// Axis2 SAAJ stores the acutal faultcode text under a SOAPFaultValue object, so we have to
// get that and add it as a text node under the original element.
Node value = se.getFirstChild();
if (value != null && value instanceof org.apache.axis2.saaj.SOAPElementImpl) {
org.apache.axis2.saaj.SOAPElementImpl valueElement = (org.apache.axis2.saaj.SOAPElementImpl) value;
ElementImpl e = valueElement.getElement();
String content = e.getText();
SOAPElement child = fault.addChildElement(new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_CODE_LOCAL_NAME));
child.addTextNode(content);
se.detachNode();
}
}
else if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME)) {
if (log.isDebugEnabled()) {
log.debug("Converting: detail");
}
se.setElementQName(new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_DETAIL_LOCAL_NAME));
}
else if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME)) {
if (log.isDebugEnabled()) {
log.debug("Converting: faultstring");
}
se.setElementQName(new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_STRING_LOCAL_NAME));
// Axis2 SAAJ stores the acutal faultstring text under a SOAPFaultValue object, so we have to
// get that and add it as a text node under the original element.
Node value = se.getFirstChild();
if (value != null && value instanceof org.apache.axis2.saaj.SOAPElementImpl) {
org.apache.axis2.saaj.SOAPElementImpl valueElement = (org.apache.axis2.saaj.SOAPElementImpl) value;
ElementImpl e = valueElement.getElement();
String content = e.getText();
SOAPElement child = fault.addChildElement(new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_STRING_LOCAL_NAME));
child.addTextNode(content);
se.detachNode();
}
}
}
}
} catch (SOAPException e) {
if (log.isDebugEnabled()) {
log.debug("An error occured while converting fault elements: " + e.getMessage());
}
throw ExceptionFactory.makeWebServiceException(e);
}
}
/**
* A Name can be created from either a SOAPEnvelope or SOAPFactory. Either one or the other is
* available when the converter is called. NameCreator provides a level of abstraction which
* simplifies the code.
*/
protected class NameCreator {
private SOAPEnvelope env = null;
private SOAPFactory sf = null;
public NameCreator(SOAPEnvelope env) {
this.env = env;
}
public NameCreator(SOAPFactory sf) {
this.sf = sf;
}
/**
* Creates a Name
*
* @param localName
* @param prefix
* @param uri
* @return Name
*/
public Name createName(String localName, String prefix, String uri)
throws SOAPException {
if (sf != null) {
return sf.createName(localName, prefix, uri);
} else {
return env.createName(localName, prefix, uri);
}
}
}
public MessageFactory createMessageFactory(String namespace)
throws SOAPException, WebServiceException {
return SAAJFactory.createMessageFactory(namespace);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?