1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.wsrf.handler;
36
37 import java.io.ByteArrayInputStream;
38 import java.io.InputStream;
39 import java.io.StringWriter;
40 import java.util.Vector;
41
42 import javax.xml.namespace.QName;
43 import javax.xml.stream.XMLStreamException;
44 import javax.xml.stream.XMLStreamWriter;
45
46 import org.apache.axiom.om.OMAbstractFactory;
47 import org.apache.axiom.om.OMElement;
48 import org.apache.axiom.om.impl.OMNodeEx;
49 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
50 import org.apache.axiom.om.util.StAXUtils;
51 import org.apache.axis2.AxisFault;
52 import org.apache.axis2.context.MessageContext;
53 import org.apache.axis2.util.StreamWrapper;
54 import org.apache.log4j.Logger;
55 import org.apache.muse.util.xml.XmlUtils;
56 import org.apache.muse.ws.addressing.WsaConstants;
57 import org.apache.muse.ws.addressing.soap.SoapConstants;
58 import org.apache.muse.ws.addressing.soap.SoapFault;
59 import org.apache.xmlbeans.XmlObject;
60 import org.w3c.dom.Document;
61 import org.w3c.dom.Element;
62
63
64
65
66
67
68
69
70 public class AxisHandler
71 {
72
73 MuseMessageHandler delegate = new MuseMessageHandler();
74
75 private static final Logger LOG = Logger.getLogger( AxisHandler.class );
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 @SuppressWarnings( { "unchecked" } )
91 public final OMElement handleRequest( OMElement request ) throws AxisFault
92 {
93
94 LOG.trace( "entering WSAG4J AxisHandler" );
95
96 try
97 {
98
99 OMElement response = delegate.handleRequest( request );
100
101
102
103
104
105
106 if ( ( response.getQName().equals( SoapConstants.FAULT_QNAME ) ) )
107 {
108
109
110
111 MessageContext msgContext = MessageContext.getCurrentMessageContext();
112
113 StringWriter writer = new StringWriter();
114 try
115 {
116 XMLStreamWriter writer2 = StAXUtils.createXMLStreamWriter( writer );
117 try
118 {
119 response.serialize( writer2 );
120 writer2.flush();
121 }
122 finally
123 {
124 writer2.close();
125 }
126 }
127 catch ( XMLStreamException e )
128 {
129 throw new AxisFault( "Can not serialize OM Element in WSAG4J error handling", e );
130 }
131
132 InputStream in = new ByteArrayInputStream( writer.toString().getBytes() );
133
134 msgContext.setWSAAction( WsaConstants.FAULT_URI );
135
136 msgContext.setProperty( org.apache.axis2.Constants.FAULT_NAME, "BaseFault" );
137
138 AxisFault f = null;
139 try
140 {
141 XmlObject parsedFault = XmlObject.Factory.parse( in );
142
143 Element node = null;
144
145 if ( parsedFault.getDomNode() instanceof Element )
146 {
147 node = (Element) parsedFault.getDomNode();
148 }
149 else if ( parsedFault.getDomNode() instanceof Document )
150 {
151 node = XmlUtils.getFirstElement( parsedFault.getDomNode() );
152 }
153
154 if ( node == null )
155 {
156 String message = "Parsed fault must no be null: " + parsedFault.xmlText();
157 throw new Exception( message );
158 }
159
160 SoapFault fault = new SoapFault( (Element) node );
161
162 String message = fault.getMessage();
163 Throwable cause = fault.getCause();
164
165 if ( cause == null )
166 {
167 f = new AxisFault( message );
168 }
169 else
170 {
171 f = new AxisFault( message, cause );
172 }
173
174 if ( fault.getCode() != null )
175 {
176 f.setFaultCode( fault.getCode() );
177 }
178
179 if ( fault.getSubCode() != null )
180 {
181 f.setFaultSubCodes( new Vector<QName>() );
182 f.getFaultSubCodes().add( fault.getSubCode() );
183 }
184
185 if ( fault.getDetail() != null )
186 {
187 XmlObject detail = XmlObject.Factory.parse( fault.getDetail() );
188
189 StAXOMBuilder builder =
190 new StAXOMBuilder( OMAbstractFactory.getOMFactory(),
191 new StreamWrapper( detail.newXMLStreamReader() ) );
192 OMElement documentElement = builder.getDocumentElement();
193
194 ( (OMNodeEx) documentElement ).setParent( null );
195
196 f.setDetail( documentElement );
197 }
198 }
199 catch ( Exception ex )
200 {
201 String message = "Error in WSAG4J Fault handling.";
202 LOG.error( message );
203 LOG.error( ex );
204 throw new AxisFault( message, ex );
205 }
206
207 throw f;
208 }
209
210 return response;
211 }
212 finally
213 {
214 LOG.trace( "leaving WSAG4J AxisHandler" );
215 }
216 }
217 }