source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/IViaSearch.java@ 9797

Last change on this file since 9797 was 9797, checked in by kjdon, 19 years ago

moved the makeConnetion to util.Misc, added error nodes to the result if there was an error

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