package org.jboss.axis.message;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.Message;
import org.jboss.axis.MessageContext;
import org.jboss.axis.client.AxisClient;
import org.jboss.axis.configuration.NullProvider;
import org.jboss.axis.encoding.DeserializationContext;
import org.jboss.axis.encoding.DeserializationContextImpl;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.schema.SchemaVersion;
import org.jboss.axis.soap.SOAPConstants;
import org.jboss.axis.utils.Mapping;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
public class SOAPEnvelopeAxisImpl extends SOAPEnvelopeImpl
{
private static Logger log = Logger.getLogger(SOAPEnvelopeAxisImpl.class.getName());
private SOAPHeaderAxisImpl header;
private SOAPBodyAxisImpl body;
public Vector trailers = new Vector();
private SOAPConstants soapConstants;
private SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001;
public String messageType;
private boolean modified;
private boolean processingRPCInvocation;
public SOAPEnvelopeAxisImpl()
{
this(true, SOAPConstants.SOAP11_CONSTANTS);
}
public SOAPEnvelopeAxisImpl(SOAPConstants soapConstants)
{
this(true, soapConstants);
}
public SOAPEnvelopeAxisImpl(SOAPConstants soapConstants,
SchemaVersion schemaVersion)
{
this(true, soapConstants, schemaVersion);
}
public SOAPEnvelopeAxisImpl(boolean registerPrefixes, SOAPConstants soapConstants)
{
this(registerPrefixes, soapConstants, SchemaVersion.SCHEMA_2001);
}
public SOAPEnvelopeAxisImpl(boolean registerPrefixes, SOAPConstants soapConstants, SchemaVersion schemaVersion)
{
super(Constants.ELEM_ENVELOPE, Constants.NS_PREFIX_SOAP_ENV,
(soapConstants != null) ? soapConstants.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI());
if (soapConstants == null)
soapConstants = Constants.DEFAULT_SOAP_VERSION;
this.soapConstants = soapConstants;
this.schemaVersion = schemaVersion;
setHeader(new SOAPHeaderAxisImpl(this, soapConstants));
setBody(new SOAPBodyAxisImpl(this, soapConstants));
if (registerPrefixes)
{
if (namespaces == null)
namespaces = new ArrayList();
namespaces.add(new Mapping(soapConstants.getEnvelopeURI(),
Constants.NS_PREFIX_SOAP_ENV));
namespaces.add(new Mapping(schemaVersion.getXsdURI(),
Constants.NS_PREFIX_SCHEMA_XSD));
namespaces.add(new Mapping(schemaVersion.getXsiURI(),
Constants.NS_PREFIX_SCHEMA_XSI));
}
setDirty(true);
}
public static SOAPEnvelopeAxisImpl newSOAPEnvelope(InputStream input) throws SAXException
{
SOAPEnvelopeAxisImpl env = new SOAPEnvelopeAxisImpl();
InputSource is = new InputSource(input);
AxisClient tmpEngine = new AxisClient(new NullProvider());
MessageContext msgContext = new MessageContext(tmpEngine);
DeserializationContext dser = new DeserializationContextImpl(is, msgContext, Message.REQUEST, env);
dser.parse();
return env;
}
public String getMessageType()
{
return messageType;
}
public void setMessageType(String messageType)
{
this.messageType = messageType;
}
public Vector getBodyElements() throws AxisFault
{
if (body != null)
{
return new Vector(body.getBodyElements());
}
else
{
return new Vector();
}
}
public Vector getTrailers()
{
return trailers;
}
public SOAPBodyElementAxisImpl getFirstBody() throws AxisFault
{
return (body != null ? body.getFirstBody() : null);
}
public boolean isModified()
{
return modified;
}
public void setModified(boolean modified)
{
this.modified = modified;
log.debug("setModifiedAfterSerialization: " + modified);
}
public boolean isProcessingRPCInvocation()
{
return processingRPCInvocation;
}
public void setProcessingRPCInvocation(boolean flag)
{
log.debug("setProcessingRPCInvocation: " + flag);
this.processingRPCInvocation = flag;
}
public Vector getHeaders() throws AxisFault
{
if (header != null)
{
return new Vector(header.getChildren());
}
else
{
return new Vector();
}
}
public void addHeader(SOAPHeaderElementAxisImpl hdr)
{
if (header == null)
{
header = new SOAPHeaderAxisImpl(this, soapConstants);
}
hdr.setEnvelope(this);
header.addHeader(hdr);
setDirty(true);
}
public void addBodyElement(SOAPBodyElementAxisImpl element)
{
element.setEnvelope(this);
body.addBodyElement(element);
setDirty(true);
}
public void removeHeaders()
{
header = null;
}
public void setHeader(SOAPHeaderAxisImpl header)
{
try
{
if (this.header != null)
removeChild(this.header);
header.setParentElement(this);
this.header = header;
}
catch (SOAPException ex)
{
log.fatal(Messages.getMessage("exception00"), ex);
}
}
public void removeHeader(SOAPHeaderElementAxisImpl hdr)
{
if (header != null)
{
header.removeHeader(hdr);
setDirty(true);
}
}
public Node removeChild(Node oldChild) throws DOMException
{
super.removeChild(oldChild);
if (oldChild instanceof SOAPHeaderAxisImpl)
removeHeaders();
else if (oldChild instanceof SOAPBodyAxisImpl)
removeBody();
return oldChild;
}
public void removeBody()
{
body = null;
}
public void setBody(SOAPBodyAxisImpl newBody)
{
if (body != null)
{
removeChild(body);
}
try
{
newBody.setParentElement(this);
body = newBody;
}
catch (SOAPException ex)
{
log.fatal(Messages.getMessage("exception00"), ex);
}
}
public void removeBodyElement(SOAPBodyElementAxisImpl element)
{
if (body != null)
{
body.removeBodyElement(element);
setDirty(true);
}
}
public void removeTrailer(SOAPElementAxisImpl element)
{
if (log.isDebugEnabled())
log.debug(Messages.getMessage("removeTrailer00"));
trailers.removeElement(element);
setDirty(true);
}
public void clearBody()
{
if (body != null)
{
body.clearBody();
setDirty(true);
}
}
public void addTrailer(SOAPElementAxisImpl element)
{
if (log.isDebugEnabled())
log.debug(Messages.getMessage("removeTrailer00"));
element.setEnvelope(this);
trailers.addElement(element);
setDirty(true);
}
public SOAPHeaderElementAxisImpl getHeaderByName(String namespace,
String localPart)
throws AxisFault
{
return getHeaderByName(namespace, localPart, false);
}
public SOAPHeaderElementAxisImpl getHeaderByName(String namespace,
String localPart,
boolean accessAllHeaders)
throws AxisFault
{
if (header != null)
{
return header.getHeaderByName(namespace,
localPart,
accessAllHeaders);
}
else
{
return null;
}
}
public SOAPBodyElementAxisImpl getBodyByName(String namespace, String localPart) throws AxisFault
{
return (body != null ? body.getBodyByName(namespace, localPart) : null);
}
public Enumeration getHeadersByName(String namespace, String localPart)
throws AxisFault
{
return getHeadersByName(namespace, localPart, false);
}
public Enumeration getHeadersByName(String namespace, String localPart,
boolean accessAllHeaders)
throws AxisFault
{
if (header != null)
{
return header.getHeadersByName(namespace,
localPart,
accessAllHeaders);
}
else
{
return new Vector().elements();
}
}
public void outputImpl(SerializationContext context) throws Exception
{
boolean oldPretty = context.getPretty();
context.setPretty(true);
if (namespaces != null)
{
for (Iterator i = namespaces.iterator(); i.hasNext();)
{
Mapping mapping = (Mapping)i.next();
context.registerPrefixForURI(mapping.getPrefix(),
mapping.getNamespaceURI());
}
}
Enumeration en;
context.startElement(new QName(soapConstants.getEnvelopeURI(),
Constants.ELEM_ENVELOPE), attributes);
for (Iterator it = getChildElements(); it.hasNext();)
{
Node childNode = (Node)it.next();
if (childNode instanceof SOAPHeaderAxisImpl || childNode instanceof SOAPBodyAxisImpl)
continue;
if (childNode instanceof SOAPElementAxisImpl)
((SOAPElementAxisImpl)childNode).output(context);
else if (childNode instanceof TextImpl)
context.writeString(childNode.getNodeValue());
}
if (header != null)
{
header.outputImpl(context);
}
if (body != null)
{
body.outputImpl(context);
}
en = trailers.elements();
while (en.hasMoreElements())
{
SOAPElementAxisImpl element = (SOAPElementAxisImpl)en.nextElement();
element.output(context);
}
context.endElement();
context.setPretty(oldPretty);
setModified(false);
}
public SOAPConstants getSOAPConstants()
{
return soapConstants;
}
public void setSoapConstants(SOAPConstants soapConstants)
{
this.soapConstants = soapConstants;
}
public SchemaVersion getSchemaVersion()
{
return schemaVersion;
}
public void setSchemaVersion(SchemaVersion schemaVersion)
{
this.schemaVersion = schemaVersion;
}
public javax.xml.soap.SOAPBody addBody() throws SOAPException
{
if (body == null)
{
setBody(new SOAPBodyAxisImpl(this, soapConstants));
return body;
}
else
{
throw new SOAPException(Messages.getMessage("bodyPresent"));
}
}
public javax.xml.soap.SOAPHeader addHeader() throws SOAPException
{
if (header == null)
{
header = new SOAPHeaderAxisImpl(this, soapConstants);
return header;
}
else
{
throw new SOAPException(Messages.getMessage("headerPresent"));
}
}
public javax.xml.soap.Name createName(String localName)
throws SOAPException
{
return new NameImpl(localName);
}
public javax.xml.soap.Name createName(String localName,
String prefix,
String uri)
throws SOAPException
{
return new NameImpl(localName, prefix, uri);
}
public javax.xml.soap.SOAPBody getBody()
{
return body;
}
public javax.xml.soap.SOAPHeader getHeader()
{
return header;
}
public void setSAAJEncodingCompliance(boolean comply)
{
this.body.setSAAJEncodingCompliance(comply);
}
}