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

Last change on this file since 21795 was 16781, checked in by ak19, 16 years ago

Since XMLConverter method nodeToElement() has now been made static, need not create an instance of it (complete with DOMParser and all) merely to call this method.

  • 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: 16781 $
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 /** The no-args constructor */
59 public SOAPCommunicator() {
60 }
61
62 public boolean configure(Element site_elem) {
63
64 String type = site_elem.getAttribute(GSXML.TYPE_ATT);
65 if (!type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
66 logger.error("wrong type of site");
67 return false;
68 }
69 remote_site_name_ = site_elem.getAttribute(GSXML.NAME_ATT);
70 if (remote_site_name_.equals("")) {
71 logger.error("must have name attribute in site element");
72 return false;
73 }
74 remote_site_address_ = site_elem.getAttribute(GSXML.ADDRESS_ATT);
75 String local_site_name = site_elem.getAttribute(GSXML.LOCAL_SITE_ATT);
76 if (remote_site_address_.equals("") && local_site_name.equals("")) {
77 logger.error("must have address or localSite attributes in site element");
78 return false;
79 }
80 if (remote_site_address_.equals("")) {
81 remote_site_address_ = GlobalProperties.getGSDL3WebAddress()+"/services/"+local_site_name;
82 }
83
84 try {
85 Service service = new Service();
86 call_ = (Call) service.createCall();
87 call_.setTargetEndpointAddress( new java.net.URL(remote_site_address_) );
88 } catch (Exception e) {
89 logger.error("SOAPCommunicator.configure() Error: Exception occurred "+e);
90 return false;
91 }
92 return true;
93 }
94
95 public Node process(Node message_node) {
96
97 Element message = XMLConverter.nodeToElement(message_node);
98
99 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
100 if (requests.getLength()==0) {
101 // no requests
102 return null;
103 }
104
105 // 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
106 // for now, use local_site_name_ as the flag.
107 if (local_site_name_!=null) { // no local site
108
109 for (int i=0;i<requests.getLength(); i++) {
110 Element e = (Element)requests.item(i);
111 String to = e.getAttribute(GSXML.TO_ATT);
112 to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
113 e.setAttribute(GSXML.TO_ATT, to);
114 String from = e.getAttribute(GSXML.FROM_ATT);
115 from = GSPath.appendLink(from, local_site_name_);
116 e.setAttribute(GSXML.FROM_ATT, from);
117 }
118 }
119 // else do nothing to the requests, just pass it on
120
121 // the soap impl needs a namespace for the top level element
122 Element message_to_send = message;
123 if (message.getNamespaceURI() == null) {
124 message_to_send = GSXML.duplicateWithNewNameNS(message.getOwnerDocument(), message, "gs3:"+message.getTagName(), "urn:foo", true);
125 }
126
127 // do the soap query
128 Element result = null;
129 try {
130 SOAPBodyElement[] input = new SOAPBodyElement[1];
131 input[0] = new SOAPBodyElement(message_to_send);
132 Vector output = (Vector) call_.invoke( input );
133 SOAPBodyElement elem = (SOAPBodyElement) output.get(0);
134 result = elem.getAsDOM();
135 } catch (Exception e) {
136 e.printStackTrace();
137 return null;
138 }
139
140 if (local_site_name_ != null) {// have to modify the from field
141
142 NodeList responses = result.getElementsByTagName(GSXML.RESPONSE_ELEM);
143 for (int i=0;i<responses.getLength(); i++) {
144 Element e = (Element)responses.item(i);
145 String from = e.getAttribute(GSXML.FROM_ATT);
146 from = GSPath.prependLink(from, remote_site_name_);
147 e.setAttribute(GSXML.FROM_ATT, from);
148 }
149 } // else return as is
150
151 return result;
152 }
153
154 public static void main (String [] args) {
155 SOAPCommunicator comm = new SOAPCommunicator();
156 XMLConverter converter = new XMLConverter();
157 String message = "<site name=\"localsite\" address=\"http://kanuka.cs.waikato.ac.nz:7070/axis/services/localsite\" type=\"soap\"/>";
158 Element site_elem = converter.getDOM(message).getDocumentElement();
159 comm.configure(site_elem);
160 message = "<message><request type=\"describe\" to=\"\" lang=\"en\"/></message>";
161 Element request_elem = converter.getDOM(message).getDocumentElement();
162 Node response = comm.process(request_elem);
163
164 logger.error("response was "+converter.getPrettyString(response));
165 }
166}
167
168
169
170
171
172
173
174
Note: See TracBrowser for help on using the repository browser.