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