View Javadoc

    1   package org.codehaus.xfire.xmlbeans.client;
    2
    3   import java.util.ArrayList;
    4
    5   import javax.xml.stream.XMLStreamException;
    6   import javax.xml.stream.XMLStreamReader;
    7   import javax.xml.stream.XMLStreamWriter;
    8
    9   import org.apache.xmlbeans.XmlCursor;
    10  import org.apache.xmlbeans.XmlException;
    11  import org.apache.xmlbeans.XmlObject;
    12  import org.apache.xmlbeans.XmlOptions;
    13  import org.codehaus.xfire.client.AbstractClientHandler;
    14  import org.codehaus.xfire.fault.XFireFault;
    15  import org.codehaus.xfire.util.STAXUtils;
    16  import org.codehaus.xfire.xmlbeans.XmlBeansFault;
    17
    18
    19  /***
    20   * Handles XmlBeans requests and response for SOAP/REST clients.
    21   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    22   * @since Oct 27, 2004
    23   */
    24  public class XMLBeansClientHandler
    25      extends AbstractClientHandler
    26  {
    27      private XmlObject[] request;
    28      private XmlObject[] response;
    29      private XmlOptions options;
    30
    31      public XMLBeansClientHandler()
    32      {
    33          options = new XmlOptions();
    34          options.setCharacterEncoding("UTF-8");
    35      }
    36
    37      public XMLBeansClientHandler(XmlOptions options)
    38      {
    39          this.options = options;
    40      }
    41
    42      /***
    43       * @return Returns the request.
    44       */
    45      public XmlObject[] getRequest()
    46      {
    47          return request;
    48      }
    49
    50      /***
    51       * @param request The request to set.
    52       */
    53      public void setRequest(XmlObject[] request)
    54      {
    55          this.request = request;
    56      }
    57
    58      /***
    59       * @return Returns the response.
    60       */
    61      public XmlObject[] getResponse()
    62      {
    63          return response;
    64      }
    65
    66      /***
    67       * @param response The response to set.
    68       */
    69      public void setResponse(XmlObject[] response)
    70      {
    71          this.response = response;
    72      }
    73
    74      public void writeRequest( XMLStreamWriter writer )
    75      	throws XMLStreamException
    76      {
    77          if ( request != null )
    78          {
    79  	        for ( int i = 0; i < request.length; i++ )
    80  	        {
    81  	            STAXUtils.copy(request[i].newXMLStreamReader(options), writer);
    82  	        }
    83          }
    84      }
    85
    86
    87      public void handleResponse(XMLStreamReader reader)
    88      	throws XMLStreamException, XFireFault
    89      {
    90          try
    91          {
    92              ArrayList responseElements = new ArrayList();
    93
    94              boolean more = true;
    95              int event = reader.getEventType();
    96              while ( more )
    97              {
    98                  switch( event )
    99                  {
    100                     case XMLStreamReader.END_DOCUMENT:
    101                         more = false;
    102                         break;
    103                     case XMLStreamReader.START_ELEMENT:
    104                         responseElements.add(XmlObject.Factory.parse( reader ));
    105                     case XMLStreamReader.END_ELEMENT:
    106                         // TODO: there should be a more generic way to do this
    107                         // so that it works with SOAP and REST at any point
    108                         // in any stream.
    109                         if ( reader.getLocalName().equals("Envelope") )
    110                             more = false;
    111                         else if ( reader.getLocalName().equals("Header") )
    112                             more = false;
    113                         break;
    114                     default:
    115                         break;
    116                 }
    117
    118                 if ( more )
    119                     event = reader.next();
    120             }
    121
    122             response = (XmlObject[]) responseElements.toArray(new XmlObject[responseElements.size()]);
    123
    124             XmlCursor cursor = response[0].newCursor();
    125             cursor.toFirstChild();
    126
    127             if ( response.length == 1
    128                  &&
    129                  cursor.getName().getLocalPart().equals("Fault") )
    130             {
    131                 throw new XmlBeansFault(response[0]);
    132             }
    133         }
    134         catch (XmlException e)
    135         {
    136             throw new XMLStreamException("Couldn't parse response.", e);
    137         }
    138     }
    139 }