View Javadoc

1   /*
2    * Copyright (c) 2004 Peter Antman, Mogul  <peter.antman@mogul.com>
3    *
4    * $Id: AxisHandler.java,v 1.1 2004/05/26 14:47:08 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.amsterdam.plugins.axis;
21  import java.net.URL;
22  import java.net.MalformedURLException;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  
27  import org.apache.axis.client.Call;
28  import org.apache.axis.client.Service;
29  import javax.xml.namespace.QName;
30  
31  import org.backsource.utils.xml.ElementUtil;
32  import org.backsource.utils.xml.XmlException;
33  
34  import org.backsource.amsterdam.service.publisher.Publisher;
35  import org.backsource.amsterdam.service.ServiceMessage;
36  import org.backsource.amsterdam.service.ServiceException;
37  import org.backsource.amsterdam.metadata.XmlConfigurable;
38  import org.backsource.amsterdam.deployment.DeploymentException;
39  
40  import org.backsource.amsterdam.plugins.XMLHandlerBase;
41  /***
42   * Filter and Publisher that publishes a Document to the webservice specifyed in url.
43   * <p>It takes several arguments, a publisher may be confugured like this:
44   <pre>     &lt;publisher
45              className="org.backsource.amsterdam.plugins.axis.AxisHandler"&gt;
46              &lt;ws-url&gt;http://localhost:8080/axis-demo/services/DeliverDocument&lt;/ws-url&gt;
47              &lt;ns&gt;http://localhost:8080/axis-demo&lt;/ns&gt;
48              &lt;method&gt;handleDocument&lt;/method&gt;
49              &lt;user&gt;vv&lt;/user&gt;
50              &lt;password&gt;vv&lt;/password&gt;
51            &lt;/publisher&gt;
52  </pre>
53  <p>method is optional and defaults to handleDocument. user and password is also optional.</p>
54   *
55   * @author <a href="mailto:pra@mogul.com">Peter Antman</a>
56   * @version $Revision: 1.1 $
57   */
58  
59  public class AxisHandler extends XMLHandlerBase implements Publisher,XmlConfigurable {
60     URL endpoint;
61     String user;
62     String password;
63     String ns;
64     String method;
65     public AxisHandler (){
66        
67     }
68     public void importXml(Element element) throws DeploymentException {
69        super.importXml(element);
70        Element root = getRootElement(element);
71        try {
72           String wsUrlStr = ElementUtil.getContent( ElementUtil.getUniqueChild(root,"ws-url"),null);
73           endpoint = new URL(wsUrlStr);
74           ns = ElementUtil.getContent( ElementUtil.getUniqueChild(root,"ns"),null);
75           method = ElementUtil.getContent( ElementUtil.getOptionalChild(root,"method"),"handleDocument");
76           user = ElementUtil.getContent( ElementUtil.getOptionalChild(root,"user"),null);
77           password = ElementUtil.getContent( ElementUtil.getOptionalChild(root,"password"),null);
78        }catch (MalformedURLException  e) {
79           throw new DeploymentException("ws-url as not an url:" +e);
80        }catch ( XmlException e) {
81           throw new DeploymentException("Error in reading configuration: " + e, e);
82        } // end o
83        
84     }
85     /***
86      * Publish document to webservice
87      */
88     public Document handleMessage(ServiceMessage message, Document doc) throws ServiceException {
89        try {
90           Service service = new Service();
91           Call call = (Call) service.createCall();
92           call.setTargetEndpointAddress( endpoint );
93           call.setOperationName(new QName(ns, method));
94           call.setOperationStyle(org.apache.axis.enum.Style.MESSAGE );
95           
96           if ( user!= null) {
97              call.setUsername(user);
98              call.setPassword(password);
99           } // end of if ()
100          
101          Object ret = call.invoke( new Object[] { doc} );
102          return doc;
103       } catch (Exception e) {
104          throw new ServiceException("Could not invoke webservice "+e,e);
105       } // end of try-catch
106       
107    }
108    
109 }// AxisHandler