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.jbi.interceptor;
019:
020: import java.util.ResourceBundle;
021:
022: import javax.xml.stream.XMLStreamException;
023: import javax.xml.stream.XMLStreamWriter;
024:
025: import org.w3c.dom.Element;
026: import org.w3c.dom.NodeList;
027:
028: import org.apache.cxf.binding.jbi.JBIConstants;
029: import org.apache.cxf.binding.jbi.JBIFault;
030: import org.apache.cxf.binding.jbi.JBIMessage;
031: import org.apache.cxf.common.i18n.BundleUtils;
032: import org.apache.cxf.common.i18n.Message;
033: import org.apache.cxf.helpers.NSStack;
034: import org.apache.cxf.interceptor.Fault;
035: import org.apache.cxf.phase.AbstractPhaseInterceptor;
036: import org.apache.cxf.phase.Phase;
037: import org.apache.cxf.staxutils.StaxUtils;
038:
039: public class JBIFaultOutInterceptor extends
040: AbstractPhaseInterceptor<JBIMessage> {
041:
042: private static final ResourceBundle BUNDLE = BundleUtils
043: .getBundle(JBIFaultOutInterceptor.class);
044:
045: public JBIFaultOutInterceptor() {
046: super (Phase.MARSHAL);
047: }
048:
049: public void handleMessage(JBIMessage message) throws Fault {
050: message.put(org.apache.cxf.message.Message.RESPONSE_CODE,
051: new Integer(500));
052: NSStack nsStack = new NSStack();
053: nsStack.push();
054:
055: try {
056: XMLStreamWriter writer = getWriter(message);
057: Fault fault = getFault(message);
058: JBIFault jbiFault = JBIFault.createFault(fault);
059: nsStack.add(JBIConstants.NS_JBI_BINDING);
060: String prefix = nsStack
061: .getPrefix(JBIConstants.NS_JBI_BINDING);
062: StaxUtils.writeStartElement(writer, prefix,
063: JBIFault.JBI_FAULT_ROOT,
064: JBIConstants.NS_JBI_BINDING);
065: if (!jbiFault.hasDetails()) {
066: writer.writeEmptyElement("fault");
067: } else {
068: Element detail = jbiFault.getDetail();
069: NodeList details = detail.getChildNodes();
070: for (int i = 0; i < details.getLength(); i++) {
071: if (details.item(i) instanceof Element) {
072: StaxUtils.writeNode(details.item(i), writer,
073: true);
074: break;
075: }
076: }
077: }
078: writer.writeEndElement();
079: writer.flush();
080:
081: } catch (XMLStreamException xe) {
082: throw new Fault(new Message("XML_WRITE_EXC", BUNDLE), xe);
083: }
084: }
085:
086: protected Fault getFault(JBIMessage message) {
087: Exception e = message.getContent(Exception.class);
088: Fault fault;
089: if (e == null) {
090: throw new IllegalStateException(new Message("NO_EXCEPTION",
091: BUNDLE).toString());
092: } else if (e instanceof Fault) {
093: fault = (Fault) e;
094: } else {
095: fault = new Fault(e);
096: }
097: return fault;
098: }
099:
100: protected XMLStreamWriter getWriter(JBIMessage message) {
101: XMLStreamWriter writer = message
102: .getContent(XMLStreamWriter.class);
103: if (writer == null) {
104: throw new IllegalStateException(new Message(
105: "NO_XML_STREAM_WRITER", BUNDLE).toString());
106: }
107: return writer;
108: }
109: }
|