01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.binding.xml.interceptor;
19:
20: import java.util.ResourceBundle;
21:
22: import javax.xml.stream.XMLStreamException;
23: import javax.xml.stream.XMLStreamReader;
24:
25: import org.w3c.dom.Element;
26:
27: import org.apache.cxf.binding.xml.XMLFault;
28: import org.apache.cxf.common.i18n.BundleUtils;
29: import org.apache.cxf.interceptor.AbstractInDatabindingInterceptor;
30: import org.apache.cxf.interceptor.ClientFaultConverter;
31: import org.apache.cxf.interceptor.Fault;
32: import org.apache.cxf.message.Message;
33: import org.apache.cxf.phase.Phase;
34: import org.apache.cxf.staxutils.DepthXMLStreamReader;
35: import org.apache.cxf.staxutils.FragmentStreamReader;
36: import org.apache.cxf.staxutils.StaxUtils;
37:
38: public class XMLFaultInInterceptor extends
39: AbstractInDatabindingInterceptor {
40:
41: private static final ResourceBundle BUNDLE = BundleUtils
42: .getBundle(XMLFaultInInterceptor.class);
43:
44: public XMLFaultInInterceptor() {
45: this (Phase.UNMARSHAL);
46: }
47:
48: public XMLFaultInInterceptor(String phase) {
49: super (phase);
50: addBefore(ClientFaultConverter.class.getName());
51: }
52:
53: public void handleMessage(Message message) throws Fault {
54:
55: XMLStreamReader xsr = message.getContent(XMLStreamReader.class);
56: DepthXMLStreamReader reader = new DepthXMLStreamReader(xsr);
57:
58: try {
59: reader.nextTag();
60: if (!StaxUtils.toNextElement(reader)) {
61: throw new Fault(new org.apache.cxf.common.i18n.Message(
62: "ILLEAGAL_XMLFAULT_FORMAT", BUNDLE));
63: }
64: String exMessage = reader.getElementText();
65: Fault fault = new XMLFault(exMessage);
66: reader.nextTag();
67: if (StaxUtils.toNextElement(reader)) {
68: // handling detail
69: Element detail = StaxUtils.read(
70: new FragmentStreamReader(reader))
71: .getDocumentElement();
72: fault.setDetail(detail);
73: }
74: message.setContent(Exception.class, fault);
75: } catch (XMLStreamException xse) {
76: throw new Fault(new org.apache.cxf.common.i18n.Message(
77: "STAX_READ_EXC", BUNDLE));
78: }
79:
80: }
81: }
|