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

Last change on this file since 33256 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

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