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

Last change on this file since 28966 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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 Katherine Don
25 * @author Chi-Yu Huang
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 Document result_doc = XMLConverter.newDOM();
79 // Create a new (empty) result message
80 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
81 result.setAttribute(GSXML.FROM_ATT, QUERY_SERVICE);
82 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
83 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
84 result.appendChild(doc_node_list);
85 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
86 if (param_list == null) {
87 logger.error("TextQuery request had no paramList.");
88 return result; // Return the empty result
89 }
90
91 // Process the request parameters
92 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
93
94 // Make sure a query has been specified
95 String query = (String) params.get(QUERY_PARAM);
96 if (query == null || query.equals("")) {
97 return result; // Return the empty result
98 }
99
100 // tidy whitespace
101 query = query.replaceAll("\\s+", "+");
102 String url_string = ivia_server_url+"/cgi-bin/canned_search?theme=gsdl3&query="+query;
103
104 // check for fields
105 String fields = (String) params.get(INDEX_PARAM);
106 fields = checkFieldParam(fields); // removes invalid fields
107 if (!fields.equals("")) {
108 url_string += "&fields="+fields;
109 }
110 //check for hits per page
111 String hits_per_page = (String) params.get(HITS_PER_PAGE_PARAM);
112 if (hits_per_page != null && !hits_per_page.equals("")) {
113 url_string += "&"+IM_HITS_PARAM+"="+hits_per_page;
114 }
115
116 // check for start page
117 String start_page = (String) params.get(START_PAGE_PARAM);
118 if (start_page != null && !start_page.equals("")) {
119 url_string += "&"+IM_START_PAGE_PARAM+"="+start_page;
120 }
121 String results_num = null;
122 String doc_ids = null;
123 BufferedReader reader = null;
124 try {
125 logger.debug("sending "+url_string);
126 reader = Misc.makeHttpConnection(url_string);
127 results_num = reader.readLine();
128 doc_ids = reader.readLine();
129 } catch (java.net.MalformedURLException e) {
130 GSXML.addError(result, "Malformed URL: "+url_string);
131 return result;
132 } catch (java.io.IOException e) {
133 GSXML.addError(result, "IOException during connection to "+url_string+": "+e.toString());
134 return result;
135 }
136
137 if (results_num.startsWith("Resources: ") && doc_ids.startsWith("Ids: ")) {
138 results_num = results_num.substring(11);
139 doc_ids = doc_ids.substring(5).trim();
140
141 } else {
142 logger.error("badly formatted results:");
143 StringBuffer result_string = new StringBuffer();
144 result_string.append("Error: badly formatted result from IVia server:\n ");
145 result_string.append(results_num);
146 result_string.append(doc_ids);
147 String line;
148 try {
149 while((line = reader.readLine()) != null) {
150 result_string.append(line);
151 }
152 } catch (Exception e) {
153 result_string.append("Exception: "+e);
154 }
155 GSXML.addError(result, result_string.toString());
156
157 return result;
158 }
159
160 // get the num docs and add to a metadata list
161 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
162 result.appendChild(metadata_list);
163
164 // Add a metadata element specifying the number of matching documents
165 long numdocs = Long.parseLong(results_num);
166 GSXML.addMetadata(metadata_list, "numDocsMatched", ""+numdocs);
167 String [] ids = doc_ids.split(" ");
168
169 for (int d=0; d<ids.length; d++) {
170 Element doc_node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
171 doc_node.setAttribute(GSXML.NODE_ID_ATT, ids[d]);
172 doc_node_list.appendChild(doc_node);
173 }
174 return result;
175 }
176
177 protected String checkFieldParam(String fields) {
178
179 if (fields == null) {
180 // return the default
181 return "";
182 }
183 StringBuffer new_fields = new StringBuffer();
184 String [] ids = fields.split(",");
185 for (int i=0; i<ids.length; i++) {
186 if(index_ids.contains(ids[i])) {
187 new_fields.append(ids[i]);
188 new_fields.append(",");
189 }
190 }
191 if (new_fields.length() == 0) {
192 return "";
193 }
194 return new_fields.toString();
195 }
196 /**
197 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
198 */
199 protected void getIndexData(ArrayList<String> index_ids, ArrayList<String> index_names,String lang){
200 index_ids.addAll(this.index_ids);
201 index_names.add(getTextString("param."+INDEX_PARAM+".kw", lang));
202 index_names.add(getTextString("param."+INDEX_PARAM+".au", lang));
203 index_names.add(getTextString("param."+INDEX_PARAM+".su", lang));
204 index_names.add(getTextString("param."+INDEX_PARAM+".ti", lang));
205 index_names.add(getTextString("param."+INDEX_PARAM+".de", lang));
206 index_names.add(getTextString("param."+INDEX_PARAM+".fu", lang));
207 }
208
209}
Note: See TracBrowser for help on using the repository browser.