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.Map;
021: import java.util.ResourceBundle;
022: import java.util.logging.Logger;
023:
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamWriter;
026:
027: import org.w3c.dom.Element;
028: import org.w3c.dom.NodeList;
029:
030: import org.apache.cxf.binding.soap.SoapFault;
031: import org.apache.cxf.binding.soap.SoapMessage;
032: import org.apache.cxf.common.i18n.BundleUtils;
033: import org.apache.cxf.common.i18n.Message;
034: import org.apache.cxf.common.util.StringUtils;
035: import org.apache.cxf.interceptor.Fault;
036: import org.apache.cxf.interceptor.URIMappingInterceptor;
037: import org.apache.cxf.phase.Phase;
038: import org.apache.cxf.staxutils.StaxUtils;
039:
040: public class Soap12FaultOutInterceptor extends AbstractSoapInterceptor {
041: private static final Logger LOG = Logger
042: .getLogger(URIMappingInterceptor.class.getName());
043: private static final ResourceBundle BUNDLE = BundleUtils
044: .getBundle(Soap12FaultOutInterceptor.class);
045:
046: public Soap12FaultOutInterceptor() {
047: super (Phase.MARSHAL);
048: }
049:
050: public void handleMessage(SoapMessage message) throws Fault {
051: LOG.info(getClass()
052: + (String) message.get(SoapMessage.CONTENT_TYPE));
053: message.put(org.apache.cxf.message.Message.RESPONSE_CODE,
054: new Integer(500));
055:
056: XMLStreamWriter writer = message
057: .getContent(XMLStreamWriter.class);
058: Fault f = (Fault) message.getContent(Exception.class);
059:
060: SoapFault fault = SoapFault
061: .createFault(f, message.getVersion());
062:
063: try {
064: Map<String, String> namespaces = fault.getNamespaces();
065: for (Map.Entry<String, String> e : namespaces.entrySet()) {
066: writer.writeNamespace(e.getKey(), e.getValue());
067: }
068:
069: String ns = message.getVersion().getNamespace();
070: String defaultPrefix = StaxUtils.getUniquePrefix(writer,
071: ns, true);
072:
073: writer.writeStartElement(defaultPrefix, "Fault", ns);
074:
075: writer.writeStartElement(defaultPrefix, "Code", ns);
076: writer.writeStartElement(defaultPrefix, "Value", ns);
077:
078: writer.writeCharacters(fault.getCodeString(
079: getFaultCodePrefix(writer, fault.getFaultCode()),
080: defaultPrefix));
081: writer.writeEndElement();
082:
083: if (fault.getSubCode() != null) {
084: writer.writeStartElement(defaultPrefix, "Subcode", ns);
085: writer.writeCharacters(fault.getSubCodeString(
086: getFaultCodePrefix(writer, fault.getSubCode()),
087: defaultPrefix));
088: writer.writeEndElement();
089: }
090: writer.writeEndElement();
091:
092: writer.writeStartElement(defaultPrefix, "Reason", ns);
093: writer.writeStartElement(defaultPrefix, "Text", ns);
094: writer.writeAttribute("xml",
095: "http://www.w3.org/XML/1998/namespace", "lang ",
096: getLangCode());
097: if (fault.getMessage() != null) {
098: writer.writeCharacters(fault.getMessage());
099: } else {
100: writer
101: .writeCharacters("Fault occurred while processing.");
102: }
103: writer.writeEndElement();
104: writer.writeEndElement();
105:
106: if (fault.hasDetails()) {
107: Element detail = fault.getDetail();
108: writer.writeStartElement(defaultPrefix, "Detail", ns);
109:
110: NodeList details = detail.getChildNodes();
111: for (int i = 0; i < details.getLength(); i++) {
112: StaxUtils.writeNode(details.item(i), writer, true);
113: }
114:
115: // Details
116: writer.writeEndElement();
117: }
118:
119: if (fault.getRole() != null) {
120: writer.writeStartElement("faultactor");
121: writer.writeCharacters(fault.getRole());
122: writer.writeEndElement();
123: }
124:
125: // Fault
126: writer.writeEndElement();
127: } catch (XMLStreamException xe) {
128: throw new Fault(new Message("XML_WRITE_EXC", BUNDLE), xe);
129: }
130: }
131:
132: private String getLangCode() {
133: String code = BUNDLE.getLocale().getDisplayLanguage();
134: if (StringUtils.isEmpty(code)) {
135: return "en";
136: }
137: return code;
138: }
139: }
|