1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | // Greenstone classes
|
---|
4 | import org.greenstone.gdbm.*;
|
---|
5 | import org.greenstone.gsdl3.util.*;
|
---|
6 |
|
---|
7 | // XML classes
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 | import org.w3c.dom.NodeList;
|
---|
11 |
|
---|
12 | //Java classes
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.io.File;
|
---|
16 | import java.io.InputStream;
|
---|
17 | import java.io.BufferedReader;
|
---|
18 | import java.io.InputStreamReader;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.net.HttpURLConnection;
|
---|
21 | import java.net.URLConnection;
|
---|
22 | import java.net.URL;
|
---|
23 | import java.net.Authenticator;
|
---|
24 | import java.net.MalformedURLException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | *
|
---|
28 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
29 | * @version $Revision: 9234 $
|
---|
30 | * Modified by <a href="mailto:[email protected]">Chi-Yu Huang</a>
|
---|
31 | */
|
---|
32 |
|
---|
33 | public class IViaSearch
|
---|
34 | extends AbstractSearch {
|
---|
35 |
|
---|
36 | // the services on offer
|
---|
37 | // these strings must match what is found in the properties file
|
---|
38 | protected static final String FIELD_PARAM = "fields";
|
---|
39 |
|
---|
40 | // have standard gs param names for hits per page, and start page
|
---|
41 | // these need to be mapped to iVia params
|
---|
42 | protected static final String IM_HITS_PARAM = "no_of_records_per_page";
|
---|
43 | protected static final String IM_START_PAGE_PARAM = "start_page_no";
|
---|
44 |
|
---|
45 | protected String ivia_server_url = null;
|
---|
46 |
|
---|
47 | public IViaSearch()
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | //Configure IViaSearch Service
|
---|
52 | public boolean configure(Element info, Element extra_info)
|
---|
53 | {
|
---|
54 | Element server_elem = (Element)GSXML.getChildByTagName(info, "iViaServer");
|
---|
55 | if (server_elem == null) {
|
---|
56 | System.err.println("IViaSearch.configure error: no iViaServer element found");
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 | ivia_server_url = server_elem.getAttribute("url");
|
---|
60 | if (ivia_server_url.equals("")) {
|
---|
61 | System.err.println("IViaSearch.configure error: no url for the iViaServer element");
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
65 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
66 | tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
67 | this.short_service_info.appendChild(tq_service);
|
---|
68 | does_paging = true;
|
---|
69 | // add some format info to service map if there is any
|
---|
70 | String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
|
---|
71 | Element format = (Element) GSXML.getNodeByPath(extra_info, path);
|
---|
72 | if (format != null) {
|
---|
73 | this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(format, true));
|
---|
74 | }
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Process a text query - implemented by concrete subclasses */
|
---|
79 | protected Element processTextQuery(Element request) {
|
---|
80 | // Create a new (empty) result message
|
---|
81 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
82 | result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
|
---|
83 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
84 | Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
85 | result.appendChild(doc_node_list);
|
---|
86 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
87 | if (param_list == null) {
|
---|
88 | System.err.println("IViaSearch Error:: TextQuery request had no paramList.");
|
---|
89 | return result; // Return the empty result
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Process the request parameters
|
---|
93 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
94 |
|
---|
95 | // Make sure a query has been specified
|
---|
96 | String query = (String) params.get(QUERY_PARAM);
|
---|
97 | if (query == null || query.equals("")) {
|
---|
98 | return result; // Return the empty result
|
---|
99 | }
|
---|
100 |
|
---|
101 | // tidy whitespace
|
---|
102 | query = query.replaceAll("\\s+", "+");
|
---|
103 | String url_string = ivia_server_url+"/cgi-bin/canned_search?theme=gsdl3&query="+query;
|
---|
104 |
|
---|
105 | // check for fields
|
---|
106 | String fields = (String) params.get(INDEX_PARAM);
|
---|
107 | if (fields != null && !fields.equals("")) {
|
---|
108 | url_string += "&fields="+fields;
|
---|
109 | }
|
---|
110 |
|
---|
111 | //check for hits per page
|
---|
112 | String hits_per_page = (String) params.get(HITS_PER_PAGE_PARAM);
|
---|
113 | if (hits_per_page != null && !hits_per_page.equals("")) {
|
---|
114 | url_string += "&"+IM_HITS_PARAM+"="+hits_per_page;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // check for start page
|
---|
118 | String start_page = (String) params.get(START_PAGE_PARAM);
|
---|
119 | if (start_page != null && !start_page.equals("")) {
|
---|
120 | url_string += "&"+IM_START_PAGE_PARAM+"="+start_page;
|
---|
121 | }
|
---|
122 | String results_num = null;
|
---|
123 | String doc_ids = null;
|
---|
124 | try {
|
---|
125 | ///system.err.println("IViaSearch, sending "+url_string);
|
---|
126 | BufferedReader reader = makeConnection(url_string);
|
---|
127 | results_num = reader.readLine();
|
---|
128 | doc_ids = reader.readLine();
|
---|
129 | } catch (Exception e) {
|
---|
130 | System.err.println("IViaSearch.TextQuery Error: exception happened during query");
|
---|
131 | e.printStackTrace();
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (results_num.startsWith("Resources: ")) {
|
---|
136 | results_num = results_num.substring(11);
|
---|
137 | } else {
|
---|
138 | System.err.println("IViaSearch.TextQuery Error: badly formatted results line: "+results_num);
|
---|
139 | return result;
|
---|
140 | }
|
---|
141 | if (doc_ids.startsWith("Ids: ")) {
|
---|
142 | doc_ids = doc_ids.substring(5).trim();
|
---|
143 | } else {
|
---|
144 | System.err.println("IViaSearch.TextQuery Error: badly formatted docs line: "+doc_ids);
|
---|
145 | return result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // get the num docs and add to a metadata list
|
---|
149 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
150 | result.appendChild(metadata_list);
|
---|
151 |
|
---|
152 | // Add a metadata element specifying the number of matching documents
|
---|
153 | long numdocs = Long.parseLong(results_num);
|
---|
154 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+numdocs);
|
---|
155 | String [] ids = doc_ids.split(" ");
|
---|
156 |
|
---|
157 | for (int d=0; d<ids.length; d++) {
|
---|
158 | Element doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
159 | doc_node.setAttribute(GSXML.NODE_ID_ATT, ids[d]);
|
---|
160 | doc_node_list.appendChild(doc_node);
|
---|
161 | }
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected BufferedReader makeConnection(String url_string) {
|
---|
166 | BufferedReader reader = null;
|
---|
167 | try {
|
---|
168 | URL url = new URL(url_string);
|
---|
169 | HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
---|
170 | InputStream input = connection.getInputStream();
|
---|
171 | reader = new BufferedReader(new InputStreamReader(input));
|
---|
172 | } catch (java.net.MalformedURLException e) {
|
---|
173 | System.err.println("IViaSearch Error: Malformed URL: "+url_string);
|
---|
174 | } catch (java.io.IOException e) {
|
---|
175 | System.err.println("IViaSearch Error: An error occurred during IO to url "+url_string);
|
---|
176 | }
|
---|
177 | return reader;
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected void getIndexData(ArrayList index_ids, ArrayList index_names,String lang){
|
---|
181 | index_ids.add("kw");
|
---|
182 | index_ids.add("au");
|
---|
183 | index_ids.add("su");
|
---|
184 | index_ids.add("ti");
|
---|
185 | index_ids.add("de");
|
---|
186 | index_ids.add("fu");
|
---|
187 | index_names.add(getTextString("param."+FIELD_PARAM+".kw", lang));
|
---|
188 | index_names.add(getTextString("param."+FIELD_PARAM+".au", lang));
|
---|
189 | index_names.add(getTextString("param."+FIELD_PARAM+".su", lang));
|
---|
190 | index_names.add(getTextString("param."+FIELD_PARAM+".ti", lang));
|
---|
191 | index_names.add(getTextString("param."+FIELD_PARAM+".de", lang));
|
---|
192 | index_names.add(getTextString("param."+FIELD_PARAM+".fu", lang));
|
---|
193 | }
|
---|
194 |
|
---|
195 | protected String getDocType (String node_id){
|
---|
196 | return node_id;
|
---|
197 | }
|
---|
198 | protected boolean hasChildren(String node_id){
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | protected boolean hasParent(String node_id){
|
---|
203 | return false;
|
---|
204 | }
|
---|
205 | }
|
---|