View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Handles incoming SOAP messages and invokes WSRF stack. In case of an error, the exception is handled for
65   * correct serialization.
66   * 
67   * @author Oliver Waeldrich
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       * Invokes WSRF stack and handles errors from the WSRF stack. The WSRF stack returns SOAP errors already
79       * as XML, we therefore check the QName of the response and in case of error the proper Axis fault is
80       * created.
81       * 
82       * @param request
83       *            the Axis2 request
84       * 
85       * @return the Axis2 response message
86       * 
87       * @throws AxisFault
88       *             the converted axis fault in case a SOAP error was created by the WSRF environment.
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             // if the operation was a fault, there will always be
103             // a SOAP 1.2 Fault element in the response body
104             //
105             // OMElement fault = envelope.getBody().getFirstChildWithName(SoapConstants.FAULT_QNAME);
106             if ( ( response.getQName().equals( SoapConstants.FAULT_QNAME ) ) )
107             {
108                 //
109                 // the response contains an error
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 }