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