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 | import org.apache.axis.client.Call;
|
---|
35 | import org.apache.axis.client.Service;
|
---|
36 | import org.apache.axis.message.SOAPBodyElement;
|
---|
37 | import javax.xml.namespace.QName;
|
---|
38 |
|
---|
39 | import org.w3c.dom.Node;
|
---|
40 | import org.w3c.dom.NodeList;
|
---|
41 | import org.w3c.dom.Document;
|
---|
42 | import org.w3c.dom.Element;
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * The Client side of a SOAP Connection
|
---|
46 | *
|
---|
47 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
48 | * @version $Revision: 9874 $
|
---|
49 | * @see <a href="http://www.w3.org/TR/SOAP/">Simple Object Access Protocol (SOAP) 1.1 </a>
|
---|
50 | */
|
---|
51 | public class SOAPCommunicator
|
---|
52 | extends Communicator {
|
---|
53 |
|
---|
54 | /** address of site to connect to */
|
---|
55 | protected String remote_site_address_=null;
|
---|
56 |
|
---|
57 | /** the call object that does the SOAP talking */
|
---|
58 | private Call call_ = null;
|
---|
59 |
|
---|
60 | /** The no-args constructor */
|
---|
61 | public SOAPCommunicator() {
|
---|
62 |
|
---|
63 |
|
---|
64 | // Set Encoding Style to standard SOAP encoding
|
---|
65 | //call_.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
|
---|
66 | // set Encoding Style to use literal XML
|
---|
67 | //call_.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public boolean configure(Element site_elem) {
|
---|
71 |
|
---|
72 |
|
---|
73 | String type = site_elem.getAttribute(GSXML.TYPE_ATT);
|
---|
74 | if (!type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
|
---|
75 | System.err.println("SOAPCommunicator: wrong type of site");
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | remote_site_name_ = site_elem.getAttribute(GSXML.NAME_ATT);
|
---|
79 | remote_site_address_ = site_elem.getAttribute(GSXML.ADDRESS_ATT);
|
---|
80 | if (remote_site_name_==null || remote_site_name_.equals("")) {
|
---|
81 | System.err.println("SOAPCommunicator: must have name attribute in site element");
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 | if (remote_site_address_==null || remote_site_address_.equals("")) {
|
---|
85 | System.err.println("SOAPCommunicator: must have address attribute in site element");
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 | try {
|
---|
89 | Service service = new Service();
|
---|
90 | call_ = (Call) service.createCall();
|
---|
91 |
|
---|
92 | //call_.setTargetObjectURI(remote_site_name_);
|
---|
93 | //call_.setMethodName("process");
|
---|
94 | call_.setTargetEndpointAddress( new java.net.URL(remote_site_address_) );
|
---|
95 | // call_.setOperationStyle(org.apache.axis.enum.Style.MESSAGE_STR);
|
---|
96 | //call_.setOperationUse(org.apache.axis.enum.Use.LITERAL_STR);
|
---|
97 | //call_.setOperationName(new QName("http://soapinterop.org/", "process"));
|
---|
98 | } catch (Exception e) {
|
---|
99 | System.err.println("SOAPCommunicator.configure() Error: Exception occurred "+e);
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public Element process(Element message) {
|
---|
106 |
|
---|
107 | NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
|
---|
108 | if (requests.getLength()==0) {
|
---|
109 | // no requests
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // if the communicator is part of a site, it needs to edit the to and from fields, otherwise it just passes the message on, as is
|
---|
114 | // for now, use local_site_name_ as the flag.
|
---|
115 | if (local_site_name_!=null) { // no local site
|
---|
116 |
|
---|
117 | for (int i=0;i<requests.getLength(); i++) {
|
---|
118 | Element e = (Element)requests.item(i);
|
---|
119 | String to = e.getAttribute(GSXML.TO_ATT);
|
---|
120 | to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
|
---|
121 | e.setAttribute(GSXML.TO_ATT, to);
|
---|
122 | String from = e.getAttribute(GSXML.FROM_ATT);
|
---|
123 | from = GSPath.appendLink(from, local_site_name_);
|
---|
124 | e.setAttribute(GSXML.FROM_ATT, from);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | // else do nothing to the requests, just pass it on
|
---|
128 |
|
---|
129 | // the soap impl needs a namespace for the top level element
|
---|
130 | Element message_to_send = message;
|
---|
131 | if (message.getNamespaceURI() == null) {
|
---|
132 | message_to_send = GSXML.duplicateWithNewNameNS(message.getOwnerDocument(), message, "gs3:"+message.getTagName(), "urn:foo", true);
|
---|
133 | //System.err.println("**"+new XMLConverter().getPrettyString(new_message));
|
---|
134 | }
|
---|
135 |
|
---|
136 | // do the soap query
|
---|
137 | Element result = null;
|
---|
138 | try {
|
---|
139 | SOAPBodyElement[] input = new SOAPBodyElement[1];
|
---|
140 | input[0] = new SOAPBodyElement(message_to_send);
|
---|
141 | Vector output = (Vector) call_.invoke( input );
|
---|
142 | SOAPBodyElement elem = (SOAPBodyElement) output.get(0);
|
---|
143 | result = elem.getAsDOM();
|
---|
144 | } catch (Exception e) {
|
---|
145 | e.printStackTrace();
|
---|
146 | return null;
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (local_site_name_ != null) {// have to modify the from field
|
---|
150 |
|
---|
151 | NodeList responses = result.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
152 | for (int i=0;i<responses.getLength(); i++) {
|
---|
153 | Element e = (Element)responses.item(i);
|
---|
154 | String from = e.getAttribute(GSXML.FROM_ATT);
|
---|
155 | from = GSPath.prependLink(from, remote_site_name_);
|
---|
156 | e.setAttribute(GSXML.FROM_ATT, from);
|
---|
157 | }
|
---|
158 | } // else return as is
|
---|
159 |
|
---|
160 | return result;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public static void main (String [] args) {
|
---|
164 | SOAPCommunicator comm = new SOAPCommunicator();
|
---|
165 | XMLConverter converter = new XMLConverter();
|
---|
166 | String message = "<site name=\"localsite\" address=\"http://kanuka.cs.waikato.ac.nz:7070/axis/services/localsite\" type=\"soap\"/>";
|
---|
167 | Element site_elem = converter.getDOM(message).getDocumentElement();
|
---|
168 | comm.configure(site_elem);
|
---|
169 | message = "<message><request type=\"describe\" to=\"\" lang=\"en\"/></message>";
|
---|
170 | //message = "<message><request type=\"describe\" to=\"\" lang=\"en\"/></message>";
|
---|
171 | Element request_elem = converter.getDOM(message).getDocumentElement();
|
---|
172 | Element response = comm.process(request_elem);
|
---|
173 |
|
---|
174 | System.err.println("response was "+converter.getPrettyString(response));
|
---|
175 | }
|
---|
176 | /*
|
---|
177 | public Element process(Element message) {
|
---|
178 |
|
---|
179 | NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
|
---|
180 | if (requests.getLength()==0) {
|
---|
181 | // no requests
|
---|
182 | return null;
|
---|
183 | }
|
---|
184 |
|
---|
185 | // if the communicator is part of a site, it needs to edit the to and from fields, otherwise it just passes the message on, as is
|
---|
186 | // for now, use local_site_name_ as the flag.
|
---|
187 | if (local_site_name_!=null) { // no local site
|
---|
188 |
|
---|
189 | for (int i=0;i<requests.getLength(); i++) {
|
---|
190 | Element e = (Element)requests.item(i);
|
---|
191 | String to = e.getAttribute(GSXML.TO_ATT);
|
---|
192 | to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
|
---|
193 | e.setAttribute(GSXML.TO_ATT, to);
|
---|
194 | String from = e.getAttribute(GSXML.FROM_ATT);
|
---|
195 | from = GSPath.appendLink(from, local_site_name_);
|
---|
196 | e.setAttribute(GSXML.FROM_ATT, from);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | // else do nothing to the message, just pass it on
|
---|
200 |
|
---|
201 | // needs to catch SOAPException, MalformedURLException
|
---|
202 | try {
|
---|
203 |
|
---|
204 | // Set Method Parameters - use literal xml
|
---|
205 | // Parameter param = new Parameter("xml_in",
|
---|
206 | // org.w3c.dom.Element.class,
|
---|
207 | // message,
|
---|
208 | // Constants.NS_URI_LITERAL_XML);
|
---|
209 |
|
---|
210 | // Vector param_list = new Vector ();
|
---|
211 | // param_list.addElement (param);
|
---|
212 | // call_.setParams (param_list);
|
---|
213 |
|
---|
214 | // // Set the URL for the Web Service
|
---|
215 | // URL url = new URL(remote_site_address_);
|
---|
216 |
|
---|
217 | // Invoke the Service
|
---|
218 | Response resp = call_.invoke (url, "");
|
---|
219 |
|
---|
220 | // Check for Faults
|
---|
221 | if (!resp.generatedFault()) { // no error
|
---|
222 | // Extract Return value
|
---|
223 | Parameter result = resp.getReturnValue ();
|
---|
224 | Element result_node = (Element) result.getValue();
|
---|
225 |
|
---|
226 | if (local_site_name_ != null) {// have to modify the from field
|
---|
227 |
|
---|
228 | NodeList responses = result_node.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
229 | for (int i=0;i<responses.getLength(); i++) {
|
---|
230 | Element e = (Element)responses.item(i);
|
---|
231 | String from = e.getAttribute(GSXML.FROM_ATT);
|
---|
232 | from = GSPath.prependLink(from, remote_site_name_);
|
---|
233 | e.setAttribute(GSXML.FROM_ATT, from);
|
---|
234 | }
|
---|
235 | } // else return as is
|
---|
236 | return result_node;
|
---|
237 | }
|
---|
238 | else { // error
|
---|
239 | // Extract Fault Code and String
|
---|
240 | Fault f = resp.getFault();
|
---|
241 | String faultCode = f.getFaultCode();
|
---|
242 | String faultString = f.getFaultString();
|
---|
243 | System.err.println("Fault Occurred (details follow):");
|
---|
244 | System.err.println("Fault Code: "+faultCode);
|
---|
245 | System.err.println("Fault String: "+faultString);
|
---|
246 | return converter_.getDOM("<"+GSXML.MESSAGE_ELEM+">Fault Occurred. No greeting for you!</"+GSXML.MESSAGE_ELEM+">").getDocumentElement();
|
---|
247 | }
|
---|
248 | } catch (SOAPException e) {
|
---|
249 | System.out.println("SOAP error:"+e.getMessage());
|
---|
250 | return null;
|
---|
251 | } catch (MalformedURLException e) {
|
---|
252 | System.out.println("URL error:"+e.getMessage());
|
---|
253 | return null;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | */
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 |
|
---|
261 |
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|