View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * 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   **/
18  package org.codehaus.activesoap;
19  
20  import org.codehaus.activesoap.soap.SoapVersion;
21  import org.codehaus.activesoap.transport.LocalTransportClient;
22  import org.codehaus.activesoap.transport.TransportClient;
23  import org.codehaus.activesoap.util.DocumentFilterXMLStreamWriter;
24  import org.codehaus.activesoap.util.DelegateXMLStreamWriter;
25  import org.codehaus.activesoap.util.LoggingXMLStreamWriter;
26  import org.codehaus.activesoap.util.NullXMLStreamWriter;
27  import org.codehaus.activesoap.handler.DefaultHandlerRegistry;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.xmlbeans.XmlObject;
31  
32  import javax.xml.stream.XMLStreamReader;
33  import javax.xml.stream.XMLStreamWriter;
34  import javax.xml.stream.XMLStreamException;
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.io.StringWriter;
39  
40  
41  /***
42   * Represents a SOAP client for invoking web services operations
43   *
44   * @version $Revision: 1.9 $
45   */
46  public class SoapClient extends RestClient {
47      private static final transient Log log = LogFactory.getLog(SoapClient.class);
48  
49      private SoapVersion soap;
50      private List headerHandlers = new ArrayList();
51  
52      /***
53       * Factory method to create a new client to an in memory SoapService
54       */
55      public static SoapClient newLocalClient(SoapService soapService) {
56          return new SoapClient(new LocalTransportClient(soapService), soapService);
57      }
58  
59      public SoapClient(TransportClient transport, SoapService service) {
60          super(transport, service);
61          this.soap = service.getSoapVersion();
62      }
63  
64      public void addHeaderHandler(Handler handler) {
65          headerHandlers.add(handler);
66      }
67  
68      public void removeHeaderHandler(Handler handler) {
69          headerHandlers.remove(handler);
70      }
71  
72      public Object parseResponse(XMLStreamReader in) throws Exception {
73          ResponseHandler responseHandler = new ResponseHandler();
74  
75          // lets create a temporary client side soap service to make a dummy invoke
76          DefaultHandlerRegistry registry = new DefaultHandlerRegistry(responseHandler);
77          SoapService service = new SoapService(registry);
78          service.invoke(in, NullXMLStreamWriter.getInstance());
79  
80          return responseHandler.body;
81      }
82  
83      // Implementation methods
84      //-------------------------------------------------------------------------
85      protected class ResponseHandler implements Handler {
86          public Object body;
87  
88          public void invoke(MessageExchange exchange) throws Exception {
89              XMLStreamReader in = exchange.getIn();
90              XMLStreamWriter out = exchange.getOut();
91              body = SoapClient.super.parseResponse(in);
92          }
93      }
94  
95      protected SoapService getSoapService() {
96          return (SoapService) getService();
97      }
98  
99      protected void processBody(MessageExchange exchange, XMLStreamWriter out, Handler generateBodyHandler) throws Exception {
100         out.setPrefix(soap.getPrefix(), soap.getNamespace());
101         soap.writeStartElement(out, "Envelope");
102         out.writeNamespace(soap.getPrefix(), soap.getNamespace());
103         processHeaders(exchange, out);
104         soap.writeStartElement(out, "Body");
105         XMLStreamWriter filter = new DocumentFilterXMLStreamWriter(out);
106         if (log.isDebugEnabled()) {
107             filter = new LoggingXMLStreamWriter(filter, log);
108         }
109         super.processBody(exchange, filter, generateBodyHandler);
110         out.writeEndElement();
111         out.writeEndElement();
112     }
113 
114     protected void processHeaders(MessageExchange exchange, XMLStreamWriter out) throws Exception {
115         if (!headerHandlers.isEmpty()) {
116             soap.writeStartElement(out, "Header");
117             for (Iterator iter = headerHandlers.iterator(); iter.hasNext();) {
118                 Handler handler = (Handler) iter.next();
119                 handler.invoke(exchange.newInstance(null, new DocumentFilterXMLStreamWriter(out)));
120             }
121             out.writeEndElement();
122         }
123     }
124 }