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