source: trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java@ 8609

Last change on this file since 8609 was 8609, checked in by schweer, 19 years ago

putting the alerting service servlet under version control

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/*
2 * Created on Nov 17, 2004
3 * Copyright (C) Andrea Schweer, 2004
4 *
5 * This file is part of the Greenstone Alerting Service.
6 * Refer to the COPYING file in the base directory of this package
7 * for licensing information.
8 */
9package org.greenstone.gsdlas;
10
11import java.net.URL;
12import java.util.*;
13
14import javax.xml.parsers.*;
15
16import org.apache.soap.Constants;
17import org.apache.soap.Fault;
18import org.apache.soap.rpc.*;
19import org.w3c.dom.*;
20
21
22/**
23 * @author schweer
24 *
25 * TODO To change the template for this generated type comment go to Window -
26 * Preferences - Java - Code Style - Code Templates
27 */
28public class GreenstoneCommunicator {
29
30 private URL host;
31 private DocumentBuilder builder;
32
33 public GreenstoneCommunicator(URL host) throws Exception {
34 this.host = host;
35 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
36 try {
37 builder = factory.newDocumentBuilder();
38 } catch (ParserConfigurationException e) {
39 throw new Exception(e);
40 }
41
42 }
43
44 public String[] getCollectionNames() throws Exception {
45 Document document = createSubsetDescriptionRequest("", "collectionList");
46
47 Element response = sendToGreenstone(document.getDocumentElement());
48
49 NodeList nodeList = response.getElementsByTagName("collection");
50 String[] result = new String[nodeList.getLength()];
51 for (int i = 0; i < nodeList.getLength(); i++) {
52 Node node = nodeList.item(i);
53 Node name = node.getAttributes().getNamedItem("name");
54 result[i] = name.getNodeValue();
55 }
56
57 return result;
58 }
59
60 public boolean supportsTextSearch(String collection) throws Exception {
61 Document document = createSubsetDescriptionRequest(collection, "serviceList");
62
63 Element response = sendToGreenstone(document.getDocumentElement());
64
65 NodeList nodeList = response.getElementsByTagName("service");
66
67 boolean hasTextQuery = false;
68 for (int i = 0; i < nodeList.getLength(); i++) {
69 Node node = nodeList.item(i);
70 String name = node.getAttributes().getNamedItem("name").getNodeValue();
71 if (name.equals("TextQuery")) {
72 hasTextQuery = true;
73 break;
74 }
75 }
76
77 if (!hasTextQuery) return false; // doesn't support text queries at all
78
79 document = createSubsetDescriptionRequest(collection + "/TextQuery", "paramList");
80
81 response = sendToGreenstone(document.getDocumentElement());
82
83 nodeList = response.getElementsByTagName("param");
84
85 for (int i = 0; i < nodeList.getLength(); i++) {
86 Node node = nodeList.item(i);
87 String name = node.getAttributes().getNamedItem("name").getNodeValue();
88 if (name == null) break;
89 String value = node.getAttributes().getNamedItem("type").getNodeValue();
90 if (value == null) break;
91 if (name.equals("query") && value.equals("string")) {
92 return true;
93 }
94 }
95
96 return false;
97 }
98
99 public Set fullTextSearch(String collection, String query) throws Exception {
100 Document document = builder.newDocument();
101 Element root = document.createElement("message");
102 document.appendChild(root);
103
104 Element requestElement = document.createElement("request");
105 requestElement.setAttribute("lang", "en");
106 requestElement.setAttribute("type", "process");
107 requestElement.setAttribute("to", collection + "/TextQuery");
108 root.appendChild(requestElement);
109
110 Element paramListElement = document.createElement("paramList");
111 requestElement.appendChild(paramListElement);
112
113 Element paramElement = document.createElement("param");
114 paramElement.setAttribute("name", "query");
115 paramElement.setAttribute("value", query);
116 // BTS 3 what if numReturnedDocs < numMatchedDocs
117 paramListElement.appendChild(paramElement);
118
119
120 Set result = new TreeSet();
121
122 Element response = sendToGreenstone(document.getDocumentElement());
123
124 NodeList documentNodes = response.getElementsByTagName("documentNode");
125
126 for(int i = 0; i < documentNodes.getLength(); i++) {
127 Node node = documentNodes.item(i);
128 String docID = node.getAttributes().getNamedItem("nodeID").getNodeValue();
129 // TODO what about the nodeType
130 result.add(docID);
131 }
132
133 return result;
134 }
135
136 private Element sendToGreenstone(Element message) throws Exception {
137 Call call = new Call();
138 call.setTargetObjectURI("teevee.localsite");
139 call.setMethodName("process");
140 // set Encoding Style to use literal XML
141 call.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
142 // Set Method Parameters - use literal xml
143 Parameter param = new Parameter("message",
144 org.w3c.dom.Element.class,
145 message,
146 Constants.NS_URI_LITERAL_XML);
147
148 Vector paramList = new Vector();
149 paramList.addElement(param);
150 call.setParams(paramList);
151
152 // Invoke the Service
153 Response response = call.invoke(host, "");
154
155 // Check for Faults
156 if (!response.generatedFault()) { // no error
157 // Extract Return value
158 Parameter result = response.getReturnValue();
159 return (Element) result.getValue();
160 } else {
161 // Extract Fault Code and String
162 Fault f = response.getFault();
163 String faultCode = f.getFaultCode();
164 String faultString = f.getFaultString();
165 throw new Exception(faultCode + ": Fault occured: " + faultString);
166 }
167 }
168
169 /**
170 * @return
171 */
172 private Document createSubsetDescriptionRequest(String object, String subsetType) {
173 Document document = builder.newDocument();
174 Element root = document.createElement("message");
175 document.appendChild(root);
176
177 Element requestElement = document.createElement("request");
178 requestElement.setAttribute("lang", "en");
179 requestElement.setAttribute("type", "describe");
180 requestElement.setAttribute("to", object);
181 root.appendChild(requestElement);
182
183 Element paramListElement = document.createElement("paramList");
184 requestElement.appendChild(paramListElement);
185
186 Element paramElement = document.createElement("param");
187 paramElement.setAttribute("name", "subset");
188 paramElement.setAttribute("value", subsetType);
189 paramListElement.appendChild(paramElement);
190 return document;
191 }
192}
Note: See TracBrowser for help on using the repository browser.