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