source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/IViaSearch.java@ 25635

Last change on this file since 25635 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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