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

Last change on this file since 13270 was 13270, checked in by shaoqun, 17 years ago

replace Category class which is deprecated with Logger class

  • 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
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: 13270 $
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
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 Element process(Element message) {
97
98 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
99 if (requests.getLength()==0) {
100 // no requests
101 return null;
102 }
103
104 // 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
105 // for now, use local_site_name_ as the flag.
106 if (local_site_name_!=null) { // no local site
107
108 for (int i=0;i<requests.getLength(); i++) {
109 Element e = (Element)requests.item(i);
110 String to = e.getAttribute(GSXML.TO_ATT);
111 to = GSPath.removeFirstLink(to); // remove the server name, should check that it is present
112 e.setAttribute(GSXML.TO_ATT, to);
113 String from = e.getAttribute(GSXML.FROM_ATT);
114 from = GSPath.appendLink(from, local_site_name_);
115 e.setAttribute(GSXML.FROM_ATT, from);
116 }
117 }
118 // else do nothing to the requests, just pass it on
119
120 // the soap impl needs a namespace for the top level element
121 Element message_to_send = message;
122 if (message.getNamespaceURI() == null) {
123 message_to_send = GSXML.duplicateWithNewNameNS(message.getOwnerDocument(), message, "gs3:"+message.getTagName(), "urn:foo", true);
124 }
125
126 // do the soap query
127 Element result = null;
128 try {
129 SOAPBodyElement[] input = new SOAPBodyElement[1];
130 input[0] = new SOAPBodyElement(message_to_send);
131 Vector output = (Vector) call_.invoke( input );
132 SOAPBodyElement elem = (SOAPBodyElement) output.get(0);
133 result = elem.getAsDOM();
134 } catch (Exception e) {
135 e.printStackTrace();
136 return null;
137 }
138
139 if (local_site_name_ != null) {// have to modify the from field
140
141 NodeList responses = result.getElementsByTagName(GSXML.RESPONSE_ELEM);
142 for (int i=0;i<responses.getLength(); i++) {
143 Element e = (Element)responses.item(i);
144 String from = e.getAttribute(GSXML.FROM_ATT);
145 from = GSPath.prependLink(from, remote_site_name_);
146 e.setAttribute(GSXML.FROM_ATT, from);
147 }
148 } // else return as is
149
150 return result;
151 }
152
153 public static void main (String [] args) {
154 SOAPCommunicator comm = new SOAPCommunicator();
155 XMLConverter converter = new XMLConverter();
156 String message = "<site name=\"localsite\" address=\"http://kanuka.cs.waikato.ac.nz:7070/axis/services/localsite\" type=\"soap\"/>";
157 Element site_elem = converter.getDOM(message).getDocumentElement();
158 comm.configure(site_elem);
159 message = "<message><request type=\"describe\" to=\"\" lang=\"en\"/></message>";
160 Element request_elem = converter.getDOM(message).getDocumentElement();
161 Element response = comm.process(request_elem);
162
163 logger.error("response was "+converter.getPrettyString(response));
164 }
165}
166
167
168
169
170
171
172
173
Note: See TracBrowser for help on using the repository browser.