1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 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. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.core.soap;
// $Id: SOAPEnvelopeImpl.java 3772 2007-07-01 19:29:13Z thomas.diesler@jboss.com $
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.rpc.soap.SOAPFaultException;
import javax.xml.soap.Name;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import org.jboss.ws.Constants;
import org.jboss.ws.core.CommonSOAPFaultException;
import org.w3c.dom.Document;
/**
* The container for the SOAPHeader and SOAPBody portions of a SOAPPart object. By default, a
* SOAPMessage object is created with a SOAPPart object that has a SOAPEnvelope object.
* The SOAPEnvelope object by default has an empty SOAPBody object and an empty SOAPHeader object.
* The SOAPBody object is required, and the SOAPHeader object, though optional, is used in the majority of cases.
*
* @author Thomas.Diesler@jboss.org
*/
public class SOAPEnvelopeImpl extends SOAPElementImpl implements SOAPEnvelope
{
// Reference the enclosing SOAPPart, so that getOwnerDocument() works correctly
private SOAPPartImpl soapPart;
/** Construct a SOAP envelope for the given SOAP version URI prefix, etc.
*/
public SOAPEnvelopeImpl(SOAPPartImpl soapPart, SOAPElement element, boolean addHeaderAndBody) throws SOAPException
{
super((SOAPElementImpl)element);
this.soapPart = soapPart;
soapPart.setEnvelope(this);
String prefix = getPrefix();
String namespaceURI = getNamespaceURI();
String localName = getLocalName();
if ("Envelope".equals(localName) == false)
throw new IllegalArgumentException("Cannot create SOAP envelope from: " + element.getElementQName());
assertEnvelopeNamespace(namespaceURI);
addNamespaceDeclaration(prefix, namespaceURI);
if (addHeaderAndBody)
{
addHeader();
addBody();
}
}
/** Construct a SOAP envelope for the given SOAP version URI.
*/
SOAPEnvelopeImpl(SOAPPartImpl soapPart, String namespace, boolean addHeaderAndBody) throws SOAPException
{
super("Envelope", Constants.PREFIX_ENV, namespace);
this.soapPart = soapPart;
soapPart.setEnvelope(this);
assertEnvelopeNamespace(namespace);
addNamespaceDeclaration(getPrefix(), namespace);
if (addHeaderAndBody)
{
addHeader();
addBody();
}
}
public SOAPMessage getSOAPMessage()
{
return soapPart.getSOAPMessage();
}
public SOAPBody addBody() throws SOAPException
{
SOAPBody body = getBody();
if (body != null)
throw new SOAPException("SOAPEnvelope already has a body element");
body = new SOAPBodyImpl(getPrefix(), getNamespaceURI());
addChildElement(body);
return body;
}
public SOAPHeader addHeader() throws SOAPException
{
SOAPHeader header = getHeader();
if (header != null)
throw new SOAPException("SOAPEnvelope already has a header element");
header = new SOAPHeaderImpl(getPrefix(), getNamespaceURI());
return (SOAPHeader)addChildElement(header);
}
@Override
public SOAPElement addAttribute(Name name, String value) throws SOAPException
{
String envNamespace = getNamespaceURI();
if (Constants.NS_SOAP12_ENV.equals(envNamespace) && name.equals(new NameImpl("encodingStyle", Constants.PREFIX_ENV, envNamespace)))
throw new SOAPException("Cannot set encodingStyle on: " + getElementQName());
return super.addAttribute(name, value);
}
/** Make sure the child is either a SOAPHeader or SOAPBody */
public SOAPElement addChildElement(SOAPElement child) throws SOAPException
{
String envNamespace = getNamespaceURI();
if (Constants.NS_SOAP12_ENV.equals(envNamespace) && !(child instanceof SOAPHeader) && !(child instanceof SOAPBody))
throw new SOAPException("SOAPHeader or SOAPBody expected");
return super.addChildElement(child);
}
public Name createName(String localName) throws SOAPException
{
return new NameImpl(localName);
}
public Name createName(String localName, String prefix, String uri) throws SOAPException
{
return new NameImpl(localName, prefix, uri);
}
public SOAPBody getBody() throws SOAPException
{
Iterator it = getChildElements();
while (it.hasNext())
{
Node node = (Node)it.next();
if ("Body".equals(node.getLocalName()))
return (SOAPBody)node;
}
return null;
}
public SOAPHeader getHeader() throws SOAPException
{
Iterator it = getChildElements();
while (it.hasNext())
{
Node node = (Node)it.next();
if ("Header".equals(node.getLocalName()))
return (SOAPHeader)node;
}
return null;
}
/**
* Text nodes are not supported.
*/
public SOAPElement addTextNode(String value) throws SOAPException
{
if (value.trim().length() > 0)
throw new SOAPException("Cannot add Text node to SOAPEnvelope");
return super.addTextNode(value);
}
public Document getOwnerDocument()
{
return soapPart;
}
private void assertEnvelopeNamespace(String namespaceURI)
{
if (!Constants.NS_SOAP12_ENV.equals(namespaceURI) && !Constants.NS_SOAP11_ENV.equals(namespaceURI))
{
QName faultCode = Constants.SOAP11_FAULT_CODE_VERSION_MISMATCH;
String faultString = "Invalid SOAP envelope namespace: " + namespaceURI;
throw new CommonSOAPFaultException(faultCode, faultString);
}
}
}
|