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