source: trunk/gsdl3/packages/gsdl-as/src/org/greenstone/gsdlas/GreenstoneCommunicator.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

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