001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.protocol.soap;
037:
038: import com.sun.xml.ws.api.SOAPVersion;
039: import static com.sun.xml.ws.api.SOAPVersion.SOAP_11;
040: import static com.sun.xml.ws.api.SOAPVersion.SOAP_12;
041: import com.sun.xml.ws.api.WSBinding;
042: import com.sun.xml.ws.api.addressing.AddressingVersion;
043: import com.sun.xml.ws.api.message.Header;
044: import com.sun.xml.ws.api.message.HeaderList;
045: import com.sun.xml.ws.api.message.Message;
046: import com.sun.xml.ws.api.message.Messages;
047: import com.sun.xml.ws.api.pipe.Tube;
048: import com.sun.xml.ws.api.pipe.TubeCloner;
049: import com.sun.xml.ws.api.pipe.helper.AbstractFilterTubeImpl;
050: import com.sun.xml.ws.message.DOMHeader;
051: import org.w3c.dom.Element;
052:
053: import javax.xml.namespace.QName;
054: import javax.xml.soap.SOAPElement;
055: import javax.xml.soap.SOAPException;
056: import javax.xml.soap.SOAPFault;
057: import javax.xml.ws.WebServiceException;
058: import javax.xml.ws.soap.SOAPBinding;
059: import javax.xml.ws.soap.SOAPFaultException;
060: import java.util.HashSet;
061: import java.util.Set;
062: import java.util.logging.Logger;
063:
064: /**
065: * @author Rama Pulavarthi
066: */
067:
068: abstract class MUTube extends AbstractFilterTubeImpl {
069:
070: private static final String MU_FAULT_DETAIL_LOCALPART = "NotUnderstood";
071: private final static QName MU_HEADER_DETAIL = new QName(
072: SOAPVersion.SOAP_12.nsUri, MU_FAULT_DETAIL_LOCALPART);
073: //TODO: change
074: protected static final Logger logger = Logger
075: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
076: + ".soap.decoder");
077: private final static String MUST_UNDERSTAND_FAULT_MESSAGE_STRING = "One or more mandatory SOAP header blocks not understood";
078:
079: protected final SOAPVersion soapVersion;
080: private final AddressingVersion addressingVersion;
081:
082: protected MUTube(WSBinding binding, Tube next) {
083: super (next);
084: // MUPipe should n't be used for bindings other than SOAP.
085: if (!(binding instanceof SOAPBinding)) {
086: throw new WebServiceException(
087: "MUPipe should n't be used for bindings other than SOAP.");
088: }
089: this .soapVersion = binding.getSOAPVersion();
090: this .addressingVersion = binding.getAddressingVersion();
091: }
092:
093: protected MUTube(MUTube that, TubeCloner cloner) {
094: super (that, cloner);
095: soapVersion = that.soapVersion;
096: addressingVersion = that.addressingVersion;
097: }
098:
099: /**
100: * @param headers HeaderList that needs MU processing
101: * @param roles Roles configured on the Binding. Required Roles supposed to be assumbed a by a
102: * SOAP Binding implementation are added.
103: * @param knownHeaders Set of headers that this binding understands
104: * @return returns the headers that have mustUnderstand attribute and are not understood
105: * by the binding.
106: */
107: protected final Set<QName> getMisUnderstoodHeaders(
108: HeaderList headers, Set<String> roles,
109: Set<QName> knownHeaders) {
110: Set<QName> notUnderstoodHeaders = null;
111:
112: understandAddressingHeaders(knownHeaders);
113:
114: for (int i = 0; i < headers.size(); i++) {
115: if (!headers.isUnderstood(i)) {
116: Header header = headers.get(i);
117: if (!header.isIgnorable(soapVersion, roles)) {
118: QName qName = new QName(header.getNamespaceURI(),
119: header.getLocalPart());
120: if (!knownHeaders.contains(qName)) {
121: logger
122: .finest("Element not understood="
123: + qName);
124: if (notUnderstoodHeaders == null)
125: notUnderstoodHeaders = new HashSet<QName>();
126: notUnderstoodHeaders.add(qName);
127: }
128: }
129: }
130: }
131: return notUnderstoodHeaders;
132: }
133:
134: /**
135: * Understand WS-Addressing headers if WS-Addressing is enabled
136: *
137: * @param knownHeaders Set of headers that this binding understands
138: */
139: private void understandAddressingHeaders(Set<QName> knownHeaders) {
140: if (addressingVersion != null) {
141: knownHeaders.add(addressingVersion.actionTag);
142: knownHeaders.add(addressingVersion.faultToTag);
143: knownHeaders.add(addressingVersion.fromTag);
144: knownHeaders.add(addressingVersion.messageIDTag);
145: knownHeaders.add(addressingVersion.relatesToTag);
146: knownHeaders.add(addressingVersion.replyToTag);
147: knownHeaders.add(addressingVersion.toTag);
148: }
149: }
150:
151: /**
152: * @param notUnderstoodHeaders
153: * @return SOAPfaultException with SOAPFault representing the MustUnderstand SOAP Fault.
154: * notUnderstoodHeaders are added in the fault detail.
155: */
156: final SOAPFaultException createMUSOAPFaultException(
157: Set<QName> notUnderstoodHeaders) {
158: try {
159: SOAPFault fault = createMUSOAPFault();
160: setMUFaultString(fault, notUnderstoodHeaders);
161: return new SOAPFaultException(fault);
162: } catch (SOAPException e) {
163: throw new WebServiceException(e);
164: }
165: }
166:
167: /**
168: * This should be used only in ServerMUPipe
169: *
170: * @param notUnderstoodHeaders
171: * @return Message representing a SOAPFault
172: * In SOAP 1.1, notUnderstoodHeaders are added in the fault Detail
173: * in SOAP 1.2, notUnderstoodHeaders are added as the SOAP Headers
174: */
175:
176: final Message createMUSOAPFaultMessage(
177: Set<QName> notUnderstoodHeaders) {
178: try {
179: SOAPFault fault = createMUSOAPFault();
180: if (soapVersion == SOAP_11) {
181: setMUFaultString(fault, notUnderstoodHeaders);
182: }
183: Message muFaultMessage = Messages.create(fault);
184: if (soapVersion == SOAP_12) {
185: addHeader(muFaultMessage, notUnderstoodHeaders);
186: }
187: return muFaultMessage;
188: } catch (SOAPException e) {
189: throw new WebServiceException(e);
190: }
191: }
192:
193: private void setMUFaultString(SOAPFault fault,
194: Set<QName> notUnderstoodHeaders) throws SOAPException {
195: fault.setFaultString("MustUnderstand headers:"
196: + notUnderstoodHeaders + " are not understood");
197: }
198:
199: private static void addHeader(Message m,
200: Set<QName> notUnderstoodHeaders) throws SOAPException {
201: for (QName qname : notUnderstoodHeaders) {
202: SOAPElement soapEl = SOAP_12.saajSoapFactory
203: .createElement(MU_HEADER_DETAIL);
204: soapEl.addNamespaceDeclaration("abc", qname
205: .getNamespaceURI());
206: soapEl.setAttribute("qname", "abc:" + qname.getLocalPart());
207: Header header = new DOMHeader<Element>(soapEl);
208: m.getHeaders().add(header);
209: }
210: }
211:
212: private SOAPFault createMUSOAPFault() throws SOAPException {
213: return soapVersion.saajSoapFactory.createFault(
214: MUST_UNDERSTAND_FAULT_MESSAGE_STRING,
215: soapVersion.faultCodeMustUnderstand);
216: }
217: }
|