KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > JbiFaultSerializer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jsr181.xfire;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.stream.XMLStreamWriter;
25
26 import org.apache.servicemix.jsr181.Jsr181ConfigurationMBean;
27 import org.codehaus.xfire.MessageContext;
28 import org.codehaus.xfire.XFireRuntimeException;
29 import org.codehaus.xfire.exchange.InMessage;
30 import org.codehaus.xfire.exchange.MessageSerializer;
31 import org.codehaus.xfire.exchange.OutMessage;
32 import org.codehaus.xfire.fault.XFireFault;
33 import org.codehaus.xfire.util.jdom.StaxSerializer;
34 import org.jdom.Element;
35
36 public class JbiFaultSerializer implements MessageSerializer {
37
38     private Jsr181ConfigurationMBean configuration;
39     
40     public JbiFaultSerializer(Jsr181ConfigurationMBean configuration) {
41         this.configuration = configuration;
42     }
43     
44     public void readMessage(InMessage message, MessageContext context) throws XFireFault {
45         throw new UnsupportedOperationException JavaDoc();
46     }
47
48     public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context) throws XFireFault {
49         try {
50             XFireFault fault = (XFireFault) message.getBody();
51             writer.writeStartElement("fault");
52             writer.writeStartElement("message");
53             writer.writeCharacters(fault.getMessage());
54             writer.writeEndElement(); // message
55
if (fault.hasDetails()) {
56                 Element detail = fault.getDetail();
57                 writer.writeStartElement("detail");
58                 StaxSerializer serializer = new StaxSerializer();
59                 List JavaDoc details = detail.getContent();
60                 for (int i = 0; i < details.size(); i++) {
61                     serializer.writeElement((Element) details.get(i), writer);
62                 }
63                 writer.writeEndElement(); // detail
64
}
65             if (configuration.isPrintStackTraceInFaults()) {
66                 writer.writeStartElement("stack");
67                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
68                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
69                 fault.printStackTrace(pw);
70                 pw.close();
71                 writer.writeCData(sw.toString());
72                 writer.writeEndElement(); // stack
73
}
74             writer.writeEndElement(); // fault
75
} catch (XMLStreamException e) {
76             throw new XFireRuntimeException("Couldn't create fault.", e);
77         }
78     }
79
80 }
81
Popular Tags