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

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

changed the location of the GDBMWrapper and DBInfo classes, so had to change some of the import statements

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