001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.saaj;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import javax.xml.namespace.QName;
023: import javax.xml.soap.SOAPConstants;
024: import javax.xml.soap.SOAPException;
025: import javax.xml.soap.SOAPHeader;
026: import javax.xml.soap.SOAPHeaderElement;
027:
028: import org.springframework.util.ObjectUtils;
029: import org.springframework.util.StringUtils;
030: import org.springframework.ws.soap.SoapHeaderElement;
031: import org.springframework.ws.soap.SoapHeaderException;
032: import org.springframework.ws.soap.soap12.Soap12Header;
033:
034: /**
035: * SAAJ-specific implementation of the <code>Soap12Header</code> interface. Wraps a {@link javax.xml.soap.SOAPHeader}.
036: *
037: * @author Arjen Poutsma
038: * @since 1.0.0
039: */
040: class SaajSoap12Header extends SaajSoapHeader implements Soap12Header {
041:
042: SaajSoap12Header(SOAPHeader header) {
043: super (header);
044: }
045:
046: public SoapHeaderElement addNotUnderstoodHeaderElement(
047: QName headerName) {
048: try {
049: SOAPHeaderElement headerElement = getImplementation()
050: .addNotUnderstoodHeaderElement(getSaajHeader(),
051: headerName);
052: return new SaajSoapHeaderElement(headerElement);
053: } catch (SOAPException ex) {
054: throw new SaajSoapHeaderException(ex);
055: }
056: }
057:
058: public SoapHeaderElement addUpgradeHeaderElement(
059: String[] supportedSoapUris) {
060: try {
061: SOAPHeaderElement headerElement = getImplementation()
062: .addUpgradeHeaderElement(getSaajHeader(),
063: supportedSoapUris);
064: return new SaajSoapHeaderElement(headerElement);
065: } catch (SOAPException ex) {
066: throw new SaajSoapHeaderException(ex);
067: }
068: }
069:
070: public Iterator examineHeaderElementsToProcess(String[] roles,
071: boolean isUltimateDestination) throws SoapHeaderException {
072: List result = new ArrayList();
073: Iterator iterator = getImplementation()
074: .examineAllHeaderElements(getSaajHeader());
075: while (iterator.hasNext()) {
076: SOAPHeaderElement saajHeaderElement = (SOAPHeaderElement) iterator
077: .next();
078: String headerRole = saajHeaderElement.getRole();
079: if (shouldProcess(headerRole, roles, isUltimateDestination)) {
080: result.add(saajHeaderElement);
081: }
082: }
083: return new SaajSoapHeaderElementIterator(result.iterator());
084:
085: }
086:
087: private boolean shouldProcess(String headerRole, String[] roles,
088: boolean isUltimateDestination) {
089: if (!StringUtils.hasLength(headerRole)) {
090: return true;
091: }
092: if (SOAPConstants.URI_SOAP_1_2_ROLE_NEXT.equals(headerRole)) {
093: return true;
094: }
095: if (SOAPConstants.URI_SOAP_1_2_ROLE_ULTIMATE_RECEIVER
096: .equals(headerRole)) {
097: return isUltimateDestination;
098: }
099: if (SOAPConstants.URI_SOAP_1_2_ROLE_NONE.equals(headerRole)) {
100: return false;
101: }
102: if (!ObjectUtils.isEmpty(roles)) {
103: for (int i = 0; i < roles.length; i++) {
104: if (roles[i].equals(headerRole)) {
105: return true;
106: }
107: }
108: }
109: return false;
110: }
111: }
|