xmlspineimpl.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 693 行 · 第 1/2 页
JAVA
693 行
/*
* 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.impl;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.OMContainerEx;
import org.apache.axiom.om.impl.llom.OMElementImpl;
import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
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.SOAPFaultDetail;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
import org.apache.axiom.soap.impl.llom.soap12.SOAP12Factory;
import org.apache.axis2.jaxws.ExceptionFactory;
import org.apache.axis2.jaxws.i18n.Messages;
import org.apache.axis2.jaxws.message.Block;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.jaxws.message.Protocol;
import org.apache.axis2.jaxws.message.XMLFault;
import org.apache.axis2.jaxws.message.factory.BlockFactory;
import org.apache.axis2.jaxws.message.factory.OMBlockFactory;
import org.apache.axis2.jaxws.message.util.MessageUtils;
import org.apache.axis2.jaxws.message.util.Reader2Writer;
import org.apache.axis2.jaxws.message.util.XMLFaultUtils;
import org.apache.axis2.jaxws.registry.FactoryRegistry;
import org.apache.axis2.jaxws.utility.JavaUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.ws.WebServiceException;
import java.util.Iterator;
/**
* XMLSpineImpl
* <p/>
* An XMLSpine consists is an OMEnvelope (either a default one or one create from an incoming
* message). As Blocks are added or requested, they are placed in the tree as OMSourcedElements.
* <p/>
* NOTE: For XML/HTTP (REST) messages, a SOAP 1.1 envelope is built and the xml payload is
* placed in the body. This purposely mimics the implementation used by Axis2.
*/
class XMLSpineImpl implements XMLSpine {
private static Log log = LogFactory.getLog(XMLSpine.class);
private static OMBlockFactory obf =
(OMBlockFactory)FactoryRegistry.getFactory(OMBlockFactory.class);
private Protocol protocol = Protocol.unknown;
private Style style = Style.DOCUMENT;
private int indirection = 0;
private SOAPEnvelope root = null;
private SOAPFactory soapFactory = null;
private boolean consumed = false;
private Message parent = null;
/**
* Create a lightweight representation of this protocol (i.e. the Envelope, Header and Body)
*
* @param protocol Protocol
* @param style Style
* @param indirection (0 or 1) indicates location of body blocks
* @param initialPayload (OMElement or null...used to add rest payload)
*/
public XMLSpineImpl(Protocol protocol, Style style, int indirection, OMElement payload) {
super();
this.protocol = protocol;
this.style = style;
this.indirection = indirection;
soapFactory = _getFactory(protocol);
root = _createEmptyEnvelope(style, soapFactory);
if (payload != null) {
((SOAPEnvelope)root).getBody().addChild(payload);
}
}
/**
* Create spine from an existing OM tree
*
* @param envelope
* @param style Style
* @param indirection (0 or 1) indicates location of body blocks
* @throws WebServiceException
*/
public XMLSpineImpl(SOAPEnvelope envelope, Style style, int indirection, Protocol protocol)
throws WebServiceException {
super();
this.style = style;
this.indirection = indirection;
this.protocol = protocol;
init(envelope);
// If null, detect protocol from soap namespace
if (protocol == null) {
if (root.getNamespace().getNamespaceURI()
.equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
this.protocol = Protocol.soap11;
} else if (root.getNamespace().getNamespaceURI()
.equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
this.protocol = Protocol.soap12;
}
}
}
/**
* @param envelope
* @throws WebServiceException
*/
private void init(SOAPEnvelope envelope) throws WebServiceException {
root = envelope;
soapFactory = MessageUtils.getSOAPFactory(root);
// Advance past the header
SOAPHeader header = root.getHeader();
if (header == null) {
header = soapFactory.createSOAPHeader(root);
}
// Now advance the parser to the body element
SOAPBody body = root.getBody();
if (body == null) {
// Create the body if one does not exist
body = soapFactory.createSOAPBody(root);
}
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.XMLPart#getProtocol()
*/
public Protocol getProtocol() {
return protocol;
}
/*
* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.XMLPart#getParent()
*/
public Message getParent() {
return parent;
}
/*
* Set the backpointer to this XMLPart's parent Message
*/
public void setParent(Message p) {
parent = p;
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.XMLPart#outputTo(javax.xml.stream.XMLStreamWriter,
* boolean)
*/
public void outputTo(XMLStreamWriter writer, boolean consume)
throws XMLStreamException, WebServiceException {
Reader2Writer r2w = new Reader2Writer(getXMLStreamReader(consume));
r2w.outputTo(writer);
}
public XMLStreamReader getXMLStreamReader(boolean consume) throws WebServiceException {
if (consume) {
if (root.getBuilder() != null && !root.getBuilder().isCompleted()) {
return root.getXMLStreamReaderWithoutCaching();
} else {
return root.getXMLStreamReader();
}
} else {
return root.getXMLStreamReader();
}
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.impl.XMLSpine#getXMLFault()
*/
public XMLFault getXMLFault() throws WebServiceException {
if (!isFault()) {
return null;
}
// Advance through all of the detail blocks
int numDetailBlocks = getNumDetailBlocks();
Block[] blocks = null;
if (numDetailBlocks > 0) {
blocks = new Block[numDetailBlocks];
SOAPFaultDetail detail = root.getBody().getFault().getDetail();
for (int i = 0; i < numDetailBlocks; i++) {
OMElement om = this._getChildOMElement(detail, i);
blocks[i] = this._getBlockFromOMElement(om, null, obf, false);
}
}
XMLFault xmlFault = XMLFaultUtils.createXMLFault(root.getBody().getFault(), blocks);
return xmlFault;
}
private int getNumDetailBlocks() throws WebServiceException {
if (isFault()) {
SOAPFault fault = root.getBody().getFault();
return _getNumChildElements(fault.getDetail());
}
return 0;
}
public void setXMLFault(XMLFault xmlFault) throws WebServiceException {
// Clear out the existing body and detail blocks
SOAPBody body = root.getBody();
getNumDetailBlocks(); // Forces parse of existing detail blocks
getNumBodyBlocks(); // Forces parse over body
OMNode child = body.getFirstOMChild();
while (child != null) {
child.detach();
child = body.getFirstOMChild();
}
// Add a SOAPFault to the body.
SOAPFault soapFault = XMLFaultUtils.createSOAPFault(xmlFault, body, false);
}
public boolean isConsumed() {
return consumed;
}
public OMElement getAsOMElement() throws WebServiceException {
return root;
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.XMLPart#getNumBodyBlocks()
*/
public int getNumBodyBlocks() throws WebServiceException {
return _getNumChildElements(_getBodyBlockParent());
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.impl.XMLSpine#getBodyBlock(int, java.lang.Object,
* org.apache.axis2.jaxws.message.factory.BlockFactory)
*/
public Block getBodyBlock(int index, Object context, BlockFactory blockFactory)
throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("getBodyBlock: Get the " + index + "block using the block factory, " +
blockFactory);
}
// Forces the parser to read all of the blocks
getNumBodyBlocks();
// Get the indicated block
OMElement omElement = _getChildOMElement(_getBodyBlockParent(), index);
if (omElement == null) {
// Null indicates that no block is available
if (log.isDebugEnabled()) {
log.debug("getBodyBlock: The block was not found ");
}
return null;
}
if (log.isDebugEnabled()) {
log.debug("getBodyBlock: Found omElement " + omElement.getQName());
}
return this._getBlockFromOMElement(omElement, context, blockFactory, false);
}
/* (non-Javadoc)
* @see org.apache.axis2.jaxws.message.impl.XMLSpine#getBodyBlock(int, java.lang.Object,
* org.apache.axis2.jaxws.message.factory.BlockFactory)
*/
public Block getBodyBlock(Object context, BlockFactory blockFactory)
throws WebServiceException {
if (log.isDebugEnabled()) {
log.debug("getBodyBlock PERFORMANT: Get the block using the block factory, " +
blockFactory);
}
// TODO Need to upgrade the code to get Blocks that represent text and elements.
// Calling getBodyBlock assumes that there is only one or zero body blocks in the message.
// Subsequent Blocks are lost. If the caller needs access to multiple body blocks,
// then getBodyBlocks(index,...) should be used
// Get the indicated block
OMElement omElement = _getChildOMElement(_getBodyBlockParent(), 0);
if (omElement == null) {
// Null indicates that no block is available
if (log.isDebugEnabled()) {
log.debug("getBodyBlock: The block was not found ");
}
return null;
}
if (log.isDebugEnabled()) {
log.debug("getBodyBlock: Found omElement " + omElement.getQName());
}
return this._getBlockFromOMElement(omElement, context, blockFactory, true);
}
public void setBodyBlock(int index, Block block) throws WebServiceException {
// Forces the parser to read all of the blocks
getNumBodyBlocks();
block.setParent(getParent());
OMElement bElement = _getBodyBlockParent();
OMElement om = this._getChildOMElement(bElement, index);
// The block is supposed to represent a single element.
// But if it does not represent an element , the following will fail.
QName qName = block.getQName();
OMElement newOM = _createOMElementFromBlock(qName, block, soapFactory);
if (om == null) {
bElement.addChild(newOM);
} else {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?