001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.handlers.addressing;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.soap.SOAP11Constants;
024: import org.apache.axiom.soap.SOAPEnvelope;
025: import org.apache.axiom.soap.SOAPFactory;
026: import org.apache.axiom.soap.SOAPFault;
027: import org.apache.axiom.soap.SOAPFaultCode;
028: import org.apache.axiom.soap.SOAPFaultDetail;
029: import org.apache.axiom.soap.SOAPFaultReason;
030: import org.apache.axiom.soap.SOAPFaultSubCode;
031: import org.apache.axiom.soap.SOAPFaultText;
032: import org.apache.axiom.soap.SOAPHeader;
033: import org.apache.axis2.AxisFault;
034: import org.apache.axis2.Constants;
035: import org.apache.axis2.addressing.AddressingConstants;
036: import org.apache.axis2.addressing.AddressingFaultsHelper;
037: import org.apache.axis2.context.MessageContext;
038: import org.apache.axis2.handlers.AbstractHandler;
039:
040: import javax.xml.namespace.QName;
041: import java.util.Iterator;
042:
043: /**
044: * This class is used to extract WS-Addressing Spec defined Faults and FaultDetail and convert them
045: * into understandable AxisFault objects.
046: */
047: public class AddressingInFaultHandler extends AbstractHandler implements
048: AddressingConstants {
049:
050: public InvocationResponse invoke(MessageContext msgContext)
051: throws AxisFault {
052: String action = msgContext.getWSAAction();
053:
054: if (Final.WSA_FAULT_ACTION.equals(action)
055: || Final.WSA_SOAP_FAULT_ACTION.equals(action)
056: || Submission.WSA_FAULT_ACTION.equals(action)) {
057: SOAPEnvelope envelope = msgContext.getEnvelope();
058: SOAPFault fault = envelope.getBody().getFault();
059:
060: if (fault == null) {
061: throw new AxisFault("Problem");
062: }
063:
064: SOAPFactory sf = ((SOAPFactory) envelope.getOMFactory());
065: SOAPFaultDetail detail = null;
066:
067: if (msgContext.isSOAP11()) {
068: SOAPHeader header = envelope.getHeader();
069: OMElement element = header
070: .getFirstChildWithName(Final.QNAME_WSA_HEADER_DETAIL);
071: if (element != null) {
072: detail = sf.createSOAPFaultDetail(fault);
073: Iterator i = element.getChildElements();
074: while (i.hasNext()) {
075: OMElement detailElement = (OMElement) i.next();
076: detail.addDetailEntry(detailElement);
077: }
078: }
079: } else {
080: detail = fault.getDetail();
081: }
082:
083: String faultDetailString = null;
084: if (detail != null) {
085: OMElement element = detail.getFirstElement();
086: if (element != null) {
087: faultDetailString = element.getText();
088: }
089: }
090:
091: String faultLocalName;
092: SOAPFaultCode code = fault.getCode();
093: SOAPFaultSubCode subCode = code.getSubCode();
094: if (subCode == null) {
095: faultLocalName = code.getTextAsQName().getLocalPart();
096: } else {
097: while (subCode.getSubCode() != null) {
098: subCode = subCode.getSubCode();
099: }
100: faultLocalName = subCode.getValue().getTextAsQName()
101: .getLocalPart();
102: }
103:
104: String newReason = AddressingFaultsHelper
105: .getMessageForAxisFault(faultLocalName,
106: faultDetailString);
107:
108: if (newReason != null) {
109: SOAPFaultReason sfr = sf.createSOAPFaultReason();
110: if (envelope.getNamespace().getNamespaceURI().equals(
111: SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
112: sfr.setText(newReason);
113: } else {
114: SOAPFaultText sft = sf.createSOAPFaultText();
115: sft.setText(newReason);
116: sfr.addSOAPText(sft);
117: }
118: // else call the on error method with the fault
119: AxisFault axisFault = new AxisFault(fault.getCode(),
120: sfr, fault.getNode(), fault.getRole(), detail);
121: msgContext.setProperty(
122: Constants.INBOUND_FAULT_OVERRIDE, axisFault);
123: }
124: }
125:
126: return InvocationResponse.CONTINUE;
127: }
128: }
|