saajconverterimpl.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 641 行 · 第 1/2 页
JAVA
641 行
/*
* 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.impl;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.dom.ElementImpl;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axis2.jaxws.ExceptionFactory;
import org.apache.axis2.jaxws.i18n.Messages;
import org.apache.axis2.jaxws.message.util.SAAJConverter;
import org.apache.axis2.jaxws.message.util.SOAPElementReader;
import org.apache.axis2.jaxws.utility.SAAJFactory;
import org.apache.axis2.util.XMLUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;
import javax.xml.namespace.QName;
import javax.xml.soap.Detail;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.ws.WebServiceException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.util.Iterator;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/** SAAJConverterImpl Provides an conversion methods between OM<->SAAJ */
public class SAAJConverterImpl implements SAAJConverter {
private static final Log log = LogFactory.getLog(SAAJConverterImpl.class);
/** Constructed via SAAJConverterFactory */
SAAJConverterImpl() {
super();
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.util.SAAJConverter#toSAAJ(org.apache.axiom.soap.SOAPEnvelope)
*/
public SOAPEnvelope toSAAJ(org.apache.axiom.soap.SOAPEnvelope omEnvelope)
throws WebServiceException {
SOAPEnvelope soapEnvelope = null;
try {
// Build the default envelope
OMNamespace ns = omEnvelope.getNamespace();
MessageFactory mf = createMessageFactory(ns.getNamespaceURI());
SOAPMessage sm = mf.createMessage();
SOAPPart sp = sm.getSOAPPart();
soapEnvelope = sp.getEnvelope();
// The getSOAPEnvelope() call creates a default SOAPEnvelope with a SOAPHeader and SOAPBody.
// The SOAPHeader and SOAPBody are removed (they will be added back in if they are present in the
// OMEnvelope).
SOAPBody soapBody = soapEnvelope.getBody();
if (soapBody != null) {
soapBody.detachNode();
}
SOAPHeader soapHeader = soapEnvelope.getHeader();
if (soapHeader != null) {
soapHeader.detachNode();
}
// We don't know if there is a real OM tree or just a backing XMLStreamReader.
// The best way to walk the data is to get the XMLStreamReader and use this
// to build the SOAPElements
XMLStreamReader reader = omEnvelope.getXMLStreamReader();
NameCreator nc = new NameCreator(soapEnvelope);
buildSOAPTree(nc, soapEnvelope, null, reader, false);
} catch (WebServiceException e) {
throw e;
} catch (SOAPException e) {
throw ExceptionFactory.makeWebServiceException(e);
}
return soapEnvelope;
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.util.SAAJConverter#toOM(javax.xml.soap.SOAPEnvelope)
*/
public org.apache.axiom.soap.SOAPEnvelope toOM(SOAPEnvelope saajEnvelope)
throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("Converting SAAJ SOAPEnvelope to an OM SOAPEnvelope");
}
// Before we do the conversion, we have to fix the QNames for fault elements
_fixFaultElements(saajEnvelope);
// Get a XMLStreamReader backed by a SOAPElement tree
XMLStreamReader reader = new SOAPElementReader(saajEnvelope);
// Get a SOAP OM Builder. Passing null causes the version to be automatically triggered
StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(reader, null);
// Create and return the OM Envelope
org.apache.axiom.soap.SOAPEnvelope omEnvelope = builder.getSOAPEnvelope();
// TODO The following statement expands the OM tree. This is
// a brute force workaround to get around an apparent bug in the om serialization
// (the pull stream parsing was not pulling the final tag).
// Four things need to occur:
// a) analyze fix the serialization/pull stream problem.
// b) add a method signature to allow the caller to request build or no build
// c) add a method signature to allow the caller to enable/disable caching
// d) possibly add an optimization to use OMSE for the body elements...to flatten the tree.
try {
omEnvelope.build();
} catch (OMException ex){
try {
// Let's try to see if we can save the envelope as a string
// and then make it into axiom SOAPEnvelope
return toOM(toString(saajEnvelope));
} catch (TransformerException e) {
throw ExceptionFactory.makeWebServiceException(e);
}
}
return omEnvelope;
}
private org.apache.axiom.soap.SOAPEnvelope toOM(String xml)
throws WebServiceException {
XMLStreamReader reader;
try {
reader = StAXUtils.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes()));
} catch (XMLStreamException e) {
throw ExceptionFactory.makeWebServiceException(e);
}
// Get a SOAP OM Builder. Passing null causes the version to be automatically triggered
StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(reader, null);
// Create and return the OM Envelope
return builder.getSOAPEnvelope();
}
private String toString(SOAPEnvelope saajEnvelope) throws TransformerException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Transformer tf;
tf = TransformerFactory.newInstance().newTransformer();
tf.transform(new DOMSource(saajEnvelope.getOwnerDocument()), new StreamResult(baos));
return new String(baos.toByteArray());
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.util.SAAJConverter#toOM(javax.xml.soap.SOAPElement)
*/
public OMElement toOM(SOAPElement soapElement) throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("Converting SAAJ SOAPElement to an OMElement");
}
// Get a XMLStreamReader backed by a SOAPElement tree
XMLStreamReader reader = new SOAPElementReader(soapElement);
// Get a OM Builder.
StAXOMBuilder builder = new StAXOMBuilder(reader);
// Create and return the Element
OMElement om = builder.getDocumentElement();
// TODO The following statement expands the OM tree. This is
// a brute force workaround to get around an apparent bug in the om serialization
// (the pull stream parsing was not pulling the final tag).
// Three things need to occur:
// a) analyze fix the serialization/pull stream problem.
// b) add a method signature to allow the caller to request build or no build
// c) add a method signature to allow the caller to enable/disable caching
om.build();
return om;
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.util.SAAJConverter#toSAAJ(org.apache.axiom.om.OMElement, javax.xml.soap.SOAPElement)
*/
public SOAPElement toSAAJ(OMElement omElement, SOAPElement parent) throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("Converting OMElement to an SAAJ SOAPElement");
}
XMLStreamReader reader = null;
// If the OM element is not attached to a parser (builder), then the OM
// is built and you cannot ask for XMLStreamReaderWithoutCaching.
// This is probably a bug in OM. You should be able to ask the OM whether
// caching is supported.
if (omElement.getBuilder() == null) {
reader = omElement.getXMLStreamReader();
} else {
reader = omElement.getXMLStreamReaderWithoutCaching();
}
SOAPElement env = parent;
while (env != null && !(env instanceof SOAPEnvelope)) {
env = env.getParentElement();
}
if (env == null) {
throw ExceptionFactory
.makeWebServiceException(Messages.getMessage("SAAJConverterErr1"));
}
NameCreator nc = new NameCreator((SOAPEnvelope)env);
return buildSOAPTree(nc, null, parent, reader, false);
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.util.SAAJConverter#toSAAJ(org.apache.axiom.om.OMElement, javax.xml.soap.SOAPElement, javax.xml.soap.SOAPFactory)
*/
public SOAPElement toSAAJ(OMElement omElement, SOAPElement parent, SOAPFactory sf)
throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("Converting OMElement to an SAAJ SOAPElement");
}
XMLStreamReader reader = null;
// If the OM element is not attached to a parser (builder), then the OM
// is built and you cannot ask for XMLStreamReaderWithoutCaching.
// This is probably a bug in OM. You should be able to ask the OM whether
// caching is supported.
if (omElement.getBuilder() == null) {
reader = omElement.getXMLStreamReader();
} else {
reader = omElement.getXMLStreamReaderWithoutCaching();
}
NameCreator nc = new NameCreator(sf);
return buildSOAPTree(nc, null, parent, reader, false);
}
/**
* Build SOAPTree Either the root or the parent is null. If the root is null, a new element is
* created under the parent using information from the reader If the parent is null, the existing
* root is updated with the information from the reader
*
* @param nc NameCreator
* @param root SOAPElement (the element that represents the data in the reader)
* @param parent (the parent of the element represented by the reader)
* @param reader XMLStreamReader. the first START_ELEMENT matches the root
* @param quitAtBody - true if quit reading after the body START_ELEMENT
*/
protected SOAPElement buildSOAPTree(NameCreator nc,
SOAPElement root,
SOAPElement parent,
XMLStreamReader reader,
boolean quitAtBody)
throws WebServiceException {
try {
while (reader.hasNext()) {
int eventID = reader.next();
switch (eventID) {
case XMLStreamReader.START_ELEMENT: {
// The first START_ELEMENT defines the prefix and attributes of the root
if (parent == null) {
updateTagData(nc, root, reader, false);
parent = root;
} else {
parent = createElementFromTag(nc, parent, reader);
if (root == null) {
root = parent;
}
}
if (quitAtBody && parent instanceof SOAPBody) {
return root;
}
break;
}
case XMLStreamReader.ATTRIBUTE: {
String eventName = "ATTRIBUTE";
this._unexpectedEvent(eventName);
}
case XMLStreamReader.NAMESPACE: {
String eventName = "NAMESPACE";
this._unexpectedEvent(eventName);
}
case XMLStreamReader.END_ELEMENT: {
if (parent instanceof SOAPEnvelope) {
parent = null;
} else {
parent = parent.getParentElement();
}
break;
}
case XMLStreamReader.CHARACTERS: {
parent.addTextNode(reader.getText());
break;
}
case XMLStreamReader.CDATA: {
parent.addTextNode(reader.getText());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?