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

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

some todo and licensing comments

  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 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 /**
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 // TODO make sure to pass IDs of whole documents, not of parts of docs
146 result.add(docID);
147 }
148
149 return result;
150 }
151
152 private Element sendToGreenstone(Element message) throws Exception {
153 Call call = new Call();
154 call.setTargetObjectURI("localsite");
155 call.setMethodName("process");
156 // set Encoding Style to use literal XML
157 call.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
158 // Set Method Parameters - use literal xml
159 Parameter param = new Parameter("message",
160 org.w3c.dom.Element.class,
161 message,
162 Constants.NS_URI_LITERAL_XML);
163
164 Vector paramList = new Vector();
165 paramList.addElement(param);
166 call.setParams(paramList);
167
168 // Invoke the Service
169 Response response = call.invoke(host, "");
170
171 // Check for Faults
172 if (!response.generatedFault()) { // no error
173 // Extract Return value
174 Parameter result = response.getReturnValue();
175 return (Element) result.getValue();
176 } else {
177 // Extract Fault Code and String
178 Fault f = response.getFault();
179 String faultCode = f.getFaultCode();
180 String faultString = f.getFaultString();
181 throw new Exception(faultCode + ": Fault occured: " + faultString);
182 }
183 }
184
185 /**
186 * @return
187 */
188 private Document createSubsetDescriptionRequest(String object, String subsetType) {
189 Document document = builder.newDocument();
190 Element root = document.createElement("message");
191 document.appendChild(root);
192
193 Element requestElement = document.createElement("request");
194 requestElement.setAttribute("lang", "en");
195 requestElement.setAttribute("type", "describe");
196 requestElement.setAttribute("to", object);
197 root.appendChild(requestElement);
198
199 Element paramListElement = document.createElement("paramList");
200 requestElement.appendChild(paramListElement);
201
202 Element paramElement = document.createElement("param");
203 paramElement.setAttribute("name", "subset");
204 paramElement.setAttribute("value", subsetType);
205 paramListElement.appendChild(paramElement);
206 return document;
207 }
208}
Note: See TracBrowser for help on using the repository browser.