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.XMLStreamWriter;
24:
25: import org.w3c.dom.Node;
26:
27: import org.apache.cxf.binding.xml.XMLConstants;
28: import org.apache.cxf.binding.xml.XMLFault;
29: import org.apache.cxf.common.i18n.BundleUtils;
30: import org.apache.cxf.helpers.DOMUtils;
31: import org.apache.cxf.helpers.NSStack;
32: import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
33: import org.apache.cxf.interceptor.Fault;
34: import org.apache.cxf.message.Message;
35: import org.apache.cxf.phase.Phase;
36: import org.apache.cxf.staxutils.StaxUtils;
37:
38: public class XMLFaultOutInterceptor extends
39: AbstractOutDatabindingInterceptor {
40:
41: private static final ResourceBundle BUNDLE = BundleUtils
42: .getBundle(XMLFaultOutInterceptor.class);
43:
44: public XMLFaultOutInterceptor() {
45: super (Phase.MARSHAL);
46: }
47:
48: public XMLFaultOutInterceptor(String phase) {
49: super (phase);
50: }
51:
52: public void handleMessage(Message message) throws Fault {
53: message.put(org.apache.cxf.message.Message.RESPONSE_CODE,
54: new Integer(500));
55: NSStack nsStack = new NSStack();
56: nsStack.push();
57:
58: XMLStreamWriter writer = message
59: .getContent(XMLStreamWriter.class);
60: Fault f = (Fault) message.getContent(Exception.class);
61: XMLFault xmlFault = XMLFault.createFault(f);
62: try {
63: nsStack.add(XMLConstants.NS_XML_FORMAT);
64: String prefix = nsStack
65: .getPrefix(XMLConstants.NS_XML_FORMAT);
66: StaxUtils
67: .writeStartElement(writer, prefix,
68: XMLFault.XML_FAULT_ROOT,
69: XMLConstants.NS_XML_FORMAT);
70: StaxUtils.writeStartElement(writer, prefix,
71: XMLFault.XML_FAULT_STRING,
72: XMLConstants.NS_XML_FORMAT);
73: Throwable t = xmlFault.getCause();
74: writer.writeCharacters(t == null ? xmlFault.getMessage()
75: : t.toString());
76: // fault string
77: writer.writeEndElement();
78: // call StaxUtils to write Fault detail.
79:
80: if (xmlFault.getDetail() != null) {
81: StaxUtils.writeStartElement(writer, prefix,
82: XMLFault.XML_FAULT_DETAIL,
83: XMLConstants.NS_XML_FORMAT);
84: StaxUtils
85: .writeNode(DOMUtils.getChild(xmlFault
86: .getDetail(), Node.ELEMENT_NODE),
87: writer, false);
88: writer.writeEndElement();
89: }
90: // fault root
91: writer.writeEndElement();
92: writer.flush();
93: } catch (XMLStreamException xe) {
94: throw new Fault(new org.apache.cxf.common.i18n.Message(
95: "XML_WRITE_EXC", BUNDLE), xe);
96: }
97: }
98: }
|