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: */package org.apache.cxf.binding.soap.interceptor;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamReader;
026: import javax.xml.xpath.XPathConstants;
027:
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.binding.soap.Soap12;
033: import org.apache.cxf.binding.soap.SoapFault;
034: import org.apache.cxf.binding.soap.SoapMessage;
035: import org.apache.cxf.common.util.StringUtils;
036: import org.apache.cxf.helpers.XMLUtils;
037: import org.apache.cxf.helpers.XPathUtils;
038: import org.apache.cxf.interceptor.ClientFaultConverter;
039: import org.apache.cxf.interceptor.Fault;
040: import org.apache.cxf.phase.Phase;
041: import org.apache.cxf.staxutils.FragmentStreamReader;
042: import org.apache.cxf.staxutils.StaxUtils;
043:
044: public class Soap12FaultInInterceptor extends AbstractSoapInterceptor {
045:
046: public Soap12FaultInInterceptor() {
047: super (Phase.UNMARSHAL);
048: addBefore(ClientFaultConverter.class.getName());
049: }
050:
051: public void handleMessage(SoapMessage message) throws Fault {
052: String exMessage = null;
053: QName faultCode = null;
054: QName subCode = null;
055: String role = null;
056: String node = null;
057: Element detail = null;
058:
059: XMLStreamReader reader = message
060: .getContent(XMLStreamReader.class);
061: Map<String, String> ns = new HashMap<String, String>();
062: ns.put("s", Soap12.SOAP_NAMESPACE);
063: XPathUtils xu = new XPathUtils(ns);
064:
065: try {
066: Document fault = StaxUtils.read(new FragmentStreamReader(
067: reader));
068: String faultCodeString = (String) xu.getValue(
069: "//s:Fault/s:Code/s:Value/text()", fault,
070: XPathConstants.STRING);
071:
072: faultCode = XMLUtils.getQName(faultCodeString, fault);
073:
074: String subCodeString = (String) xu.getValue(
075: "//s:Fault/s:Code/s:Subcode/s:Value/text()", fault,
076: XPathConstants.STRING);
077: if (StringUtils.isEmpty(subCodeString)) {
078: subCode = XMLUtils.getQName(subCodeString, fault);
079: }
080:
081: exMessage = (String) xu.getValue(
082: "//s:Fault/s:Reason/s:Text/text()", fault,
083: XPathConstants.STRING);
084:
085: Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail",
086: fault, XPathConstants.NODE);
087: if (detailNode != null) {
088: detail = (Element) detailNode;
089: }
090:
091: role = (String) xu.getValue("//s:Fault/s:Role/text()",
092: fault, XPathConstants.STRING);
093:
094: node = (String) xu.getValue("//s:Fault/s:Node/text()",
095: fault, XPathConstants.STRING);
096: } catch (XMLStreamException e) {
097: throw new SoapFault("Could not parse message.", message
098: .getVersion().getSender());
099: }
100:
101: SoapFault fault = new SoapFault(exMessage, faultCode);
102: fault.setSubCode(subCode);
103: fault.setDetail(detail);
104: fault.setRole(role);
105: fault.setNode(node);
106:
107: message.setContent(Exception.class, fault);
108: }
109:
110: }
|