source: trunk/gsdl3/src/java/org/greenstone/gsdl3/comms/SOAPCommunicator.java@ 11022

Last change on this file since 11022 was 11022, checked in by kjdon, 18 years ago

cleaned out the commented out stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
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 */
19package org.greenstone.gsdl3.comms;
20
21import org.greenstone.gsdl3.util.*;
22import org.greenstone.gsdl3.core.*;
23
24// classes needed for SOAP stuff
25import java.net.URL;
26import java.net.MalformedURLException;
27import java.util.Vector;
28import org.apache.axis.client.Call;
29import org.apache.axis.client.Service;
30import org.apache.axis.message.SOAPBodyElement;
31import javax.xml.namespace.QName;
32
33import org.w3c.dom.Node;
34import org.w3c.dom.NodeList;
35import org.w3c.dom.Document;
36import org.w3c.dom.Element;
37
38/*
39 * The Client side of a SOAP Connection
40 *
41 * @author <a href="mailto:[email protected]">Katherine Don</a>
42 * @version $Revision: 11022 $
43 * @see <a href="http://www.w3.org/TR/SOAP/">Simple Object Access Protocol (SOAP) 1.1 </a>
44 */
45public class SOAPCommunicator
46 extends Communicator {
47
48 /** address of site to connect to */
49 protected String remote_site_address_=null;
50
51 /** the call object that does the SOAP talking */
52 private Call call_ = null;
53
54 /** The no-args constructor */
55 public SOAPCommunicator() {
56
57 }
58
59 public boolean configure(Element site_elem) {
60
61 String type = site_elem.getAttribute(GSXML.TYPE_ATT);
62 if (!type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
63 System.err.println("SOAPCommunicator: wrong type of site");
64 return false;
65 }
66 remote_site_name_ = site_elem.getAttribute(GSXML.NAME_ATT);
67 if (remote_site_name_.equals("")) {
68 System.err.println("SOAPCommunicator: must have name attribute in site element");
69 return false;
70 }
71 remote_site_address_ = site_elem.getAttribute(GSXML.ADDRESS_ATT);
72 String local_site_name = site_elem.getAttribute(GSXML.LOCAL_SITE_ATT);
73 if (remote_site_address_.equals("") && local_site_name.equals("")) {
74 System.err.println("SOAPCommunicator: must have address or localSite attributes in site element");
75 return false;
76 }
77 if (remote_site_address_.equals("")) {
78 remote_site_address_ = GlobalProperties.getGSDL3WebAddress()+"/services/"+local_site_name;
79 }
80
81 try {
82 Service service = new Service();
83 call_ = (Call) service.createCall();
84 call_.setTargetEndpointAddress( new java.net.URL(remote_site_address_) );
85 } catch (Exception e) {
86 System.err.println("SOAPCommunicator.configure() Error: Exception occurred "+e);
87 return false;
88 }
89 return true;
90 }
91
92 public Element process(Element message) {
93
94 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
95 if (requests.getLength()==0) {
96 // no requests
97 return null;
98 }
99
100 // 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
101 // for now, use local_site_name_ as the flag.
102 if (local_site_name_!=null) { // no local site
103
104 for (int i=0;i<requests.getLength(); i++) {
105 Element e = (Element)requests.item(i);
106 String to = e.getAttribute(GSXML.TO_ATT);
107 to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
108 e.setAttribute(GSXML.TO_ATT, to);
109 String from = e.getAttribute(GSXML.FROM_ATT);
110 from = GSPath.appendLink(from, local_site_name_);
111 e.setAttribute(GSXML.FROM_ATT, from);
112 }
113 }
114 // else do nothing to the requests, just pass it on
115
116 // the soap impl needs a namespace for the top level element
117 Element message_to_send = message;
118 if (message.getNamespaceURI() == null) {
119 message_to_send = GSXML.duplicateWithNewNameNS(message.getOwnerDocument(), message, "gs3:"+message.getTagName(), "urn:foo", true);
120 //System.err.println("**"+new XMLConverter().getPrettyString(new_message));
121 }
122
123 // do the soap query
124 Element result = null;
125 try {
126 SOAPBodyElement[] input = new SOAPBodyElement[1];
127 input[0] = new SOAPBodyElement(message_to_send);
128 Vector output = (Vector) call_.invoke( input );
129 SOAPBodyElement elem = (SOAPBodyElement) output.get(0);
130 result = elem.getAsDOM();
131 } catch (Exception e) {
132 e.printStackTrace();
133 return null;
134 }
135
136 if (local_site_name_ != null) {// have to modify the from field
137
138 NodeList responses = result.getElementsByTagName(GSXML.RESPONSE_ELEM);
139 for (int i=0;i<responses.getLength(); i++) {
140 Element e = (Element)responses.item(i);
141 String from = e.getAttribute(GSXML.FROM_ATT);
142 from = GSPath.prependLink(from, remote_site_name_);
143 e.setAttribute(GSXML.FROM_ATT, from);
144 }
145 } // else return as is
146
147 return result;
148 }
149
150 public static void main (String [] args) {
151 SOAPCommunicator comm = new SOAPCommunicator();
152 XMLConverter converter = new XMLConverter();
153 String message = "<site name=\"localsite\" address=\"http://kanuka.cs.waikato.ac.nz:7070/axis/services/localsite\" type=\"soap\"/>";
154 Element site_elem = converter.getDOM(message).getDocumentElement();
155 comm.configure(site_elem);
156 message = "<message><request type=\"describe\" to=\"\" lang=\"en\"/></message>";
157 Element request_elem = converter.getDOM(message).getDocumentElement();
158 Element response = comm.process(request_elem);
159
160 System.err.println("response was "+converter.getPrettyString(response));
161 }
162}
163
164
165
166
167
168
169
170
Note: See TracBrowser for help on using the repository browser.