1 | /*
|
---|
2 | * SOAPCommunicator.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.comms;
|
---|
20 |
|
---|
21 | import org.greenstone.gsdl3.util.*;
|
---|
22 | import org.greenstone.gsdl3.core.*;
|
---|
23 |
|
---|
24 | // classes needed for SOAP stuff
|
---|
25 | import java.net.URL;
|
---|
26 | import java.net.MalformedURLException;
|
---|
27 | import java.util.Vector;
|
---|
28 | import org.apache.soap.SOAPException;
|
---|
29 | import org.apache.soap.Fault;
|
---|
30 | import org.apache.soap.Constants;
|
---|
31 | import org.apache.soap.rpc.Call;
|
---|
32 | import org.apache.soap.rpc.Parameter;
|
---|
33 | import org.apache.soap.rpc.Response;
|
---|
34 |
|
---|
35 | import org.w3c.dom.Node;
|
---|
36 | import org.w3c.dom.NodeList;
|
---|
37 | import org.w3c.dom.Document;
|
---|
38 | import org.w3c.dom.Element;
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * The Client side of a SOAP Connection
|
---|
42 | *
|
---|
43 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
44 | * @version $Revision: 3504 $
|
---|
45 | * @see <a href="http://www.w3.org/TR/SOAP/">Simple Object Access Protocol (SOAP) 1.1 </a>
|
---|
46 | */
|
---|
47 | public class SOAPCommunicator
|
---|
48 | extends Communicator {
|
---|
49 |
|
---|
50 | /** the call object that does the SOAP talking */
|
---|
51 | private Call call_ = null;
|
---|
52 |
|
---|
53 | /** The no-args constructor */
|
---|
54 | public SOAPCommunicator() {
|
---|
55 |
|
---|
56 | call_ = new Call();
|
---|
57 | // Set Encoding Style to standard SOAP encoding
|
---|
58 | //call_.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
|
---|
59 | // set Encoding Style to use literal XML
|
---|
60 | call_.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void setConnectionName(String name) {
|
---|
64 | System.out.println("name="+name);
|
---|
65 | connection_name_ = name;
|
---|
66 | // Set Object URI and Method Name
|
---|
67 | call_.setTargetObjectURI (name);
|
---|
68 | call_.setMethodName ("process"); // everything uses process
|
---|
69 | }
|
---|
70 |
|
---|
71 | public boolean configure() {
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | // put this into Communicator??
|
---|
77 | public Element process(Element xml_in) {
|
---|
78 | // needs to catch SOAPException, MalformedURLException
|
---|
79 | try {
|
---|
80 | // add the message encloser back on, and edit the to field
|
---|
81 | Document newdoc = converter_.newDOM();
|
---|
82 | Element top = newdoc.createElement("message");
|
---|
83 | top.appendChild(newdoc.importNode(xml_in, true));
|
---|
84 | //newdoc.appendChild(top);
|
---|
85 | NodeList requests = top.getElementsByTagName("request");
|
---|
86 | if (requests.getLength()==0) {
|
---|
87 | // no requests
|
---|
88 | return null;
|
---|
89 | }
|
---|
90 | for (int i=0;i<requests.getLength(); i++) {
|
---|
91 | Element e = (Element)requests.item(i);
|
---|
92 | String to = e.getAttribute("to");
|
---|
93 | to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
|
---|
94 | e.setAttribute("to", to);
|
---|
95 | String from = e.getAttribute("from");
|
---|
96 | from = GSPath.appendLink(from, local_site_name_);
|
---|
97 | e.setAttribute("from", from);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Set Method Parameters
|
---|
101 |
|
---|
102 | // dont use Strings anymore
|
---|
103 | //String converted_request = converter_.getString(top);
|
---|
104 | //System.out.println("converted request = "+converted_request);
|
---|
105 | //
|
---|
106 | //Parameter param = new Parameter("xml_in", String.class,
|
---|
107 | //converted_request, Constants.NS_URI_SOAP_ENC);
|
---|
108 |
|
---|
109 |
|
---|
110 | // use straight xml
|
---|
111 | Parameter param = new Parameter("xml_in",
|
---|
112 | org.w3c.dom.Element.class,
|
---|
113 | top,
|
---|
114 | Constants.NS_URI_LITERAL_XML);
|
---|
115 |
|
---|
116 | Vector param_list = new Vector ();
|
---|
117 | param_list.addElement (param);
|
---|
118 | call_.setParams (param_list);
|
---|
119 |
|
---|
120 | // Set the URL for the Web Service
|
---|
121 | URL url = new URL(connection_address_);
|
---|
122 |
|
---|
123 | // Invoke the Service
|
---|
124 | Response resp = call_.invoke (url, "");
|
---|
125 |
|
---|
126 | // Check for Faults
|
---|
127 | if (!resp.generatedFault()) {
|
---|
128 | // Extract Return value
|
---|
129 | Parameter result = resp.getReturnValue ();
|
---|
130 | //String res = (String) result.getValue();
|
---|
131 | //Element result_node = converter_.getDOM(res).getDocumentElement();
|
---|
132 | Element result_node = (Element) result.getValue();
|
---|
133 |
|
---|
134 | // have to modify the from field
|
---|
135 |
|
---|
136 | NodeList responses = result_node.getElementsByTagName("response");
|
---|
137 | for (int i=0;i<responses.getLength(); i++) {
|
---|
138 | Element e = (Element)responses.item(i);
|
---|
139 | String from = e.getAttribute("from");
|
---|
140 | from = GSPath.prependLink(from, connection_name_);
|
---|
141 | e.setAttribute("from", from);
|
---|
142 | }
|
---|
143 |
|
---|
144 | return result_node;
|
---|
145 | }
|
---|
146 | else {
|
---|
147 | // Extract Fault Code and String
|
---|
148 | Fault f = resp.getFault();
|
---|
149 | String faultCode = f.getFaultCode();
|
---|
150 | String faultString = f.getFaultString();
|
---|
151 | System.err.println("Fault Occurred (details follow):");
|
---|
152 | System.err.println("Fault Code: "+faultCode);
|
---|
153 | System.err.println("Fault String: "+faultString);
|
---|
154 | return converter_.getDOM("<message>Fault Occurred. No greeting for you!</message>").getDocumentElement();
|
---|
155 | }
|
---|
156 | } catch (SOAPException e) {
|
---|
157 | System.out.println("SOAP error:"+e.getMessage());
|
---|
158 | return null;
|
---|
159 | } catch (MalformedURLException e) {
|
---|
160 | System.out.println("URL error:"+e.getMessage());
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public String process(String xml_in) {
|
---|
166 | Element e = converter_.getDOM(xml_in).getDocumentElement();
|
---|
167 | Node result = process(e);
|
---|
168 | return converter_.getString(result);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|