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

Last change on this file since 16688 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

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