[9234] | 1 | package org.greenstone.gsdl3.service;
|
---|
| 2 |
|
---|
| 3 | // Greenstone classes
|
---|
[9874] | 4 | import org.greenstone.gsdl3.core.GSException;
|
---|
[22085] | 5 | import org.greenstone.util.Misc;
|
---|
[9234] | 6 | import org.greenstone.gsdl3.util.*;
|
---|
| 7 |
|
---|
| 8 | // XML classes
|
---|
| 9 | import org.w3c.dom.Element;
|
---|
| 10 | import org.w3c.dom.Document;
|
---|
| 11 | import org.w3c.dom.NodeList;
|
---|
| 12 |
|
---|
| 13 | import java.util.HashMap;
|
---|
| 14 | import java.util.ArrayList;
|
---|
| 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 |
|
---|
[13124] | 26 | import org.apache.log4j.*;
|
---|
| 27 |
|
---|
[9234] | 28 | public class IViaRetrieve
|
---|
| 29 | extends AbstractDocumentRetrieve {
|
---|
[13124] | 30 |
|
---|
[13270] | 31 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.IViaRetrieve.class.getName());
|
---|
[9234] | 32 |
|
---|
[13124] | 33 |
|
---|
[9234] | 34 | protected String ivia_server_url = null;
|
---|
| 35 |
|
---|
[9289] | 36 | public IViaRetrieve() {
|
---|
| 37 | does_structure = false;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[9234] | 40 | //Configure IViaRetrieve Service
|
---|
| 41 | public boolean configure(Element info, Element extra_info)
|
---|
| 42 | {
|
---|
[9289] | 43 | if (!super.configure(info, extra_info)) {
|
---|
| 44 | return false;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[9234] | 47 | Element server_elem = (Element)GSXML.getChildByTagName(info, "iViaServer");
|
---|
| 48 | if (server_elem == null) {
|
---|
[13124] | 49 | logger.error("no iViaServer element found");
|
---|
[9234] | 50 | return false;
|
---|
| 51 | }
|
---|
| 52 | ivia_server_url = server_elem.getAttribute("url");
|
---|
| 53 | if (ivia_server_url.equals("")) {
|
---|
[13124] | 54 | logger.error("no url for the iViaServer element");
|
---|
[9234] | 55 | return false;
|
---|
| 56 | }
|
---|
| 57 | return true;
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** gets a document by sending a request to iVia, then processes it and creates a documentNode around the text */
|
---|
[28966] | 62 | protected Element getNodeContent(Document doc, String doc_id, String lang)
|
---|
[9874] | 63 | throws GSException {
|
---|
[9234] | 64 |
|
---|
| 65 | String url_string = ivia_server_url+"/cgi-bin/view_record?theme=gsdl3&record_id="+doc_id;
|
---|
| 66 |
|
---|
| 67 | StringBuffer buffer = new StringBuffer();
|
---|
| 68 | try {
|
---|
[9874] | 69 | BufferedReader reader = Misc.makeHttpConnection(url_string);
|
---|
[9234] | 70 | String line;
|
---|
| 71 | while((line = reader.readLine())!= null) {
|
---|
| 72 | buffer.append(line);
|
---|
| 73 | }
|
---|
[9874] | 74 | } catch (java.net.MalformedURLException e) {
|
---|
| 75 | throw new GSException("Malformed URL: "+url_string, GSXML.ERROR_TYPE_SYSTEM);
|
---|
| 76 | } catch (java.io.IOException e) {
|
---|
| 77 | throw new GSException("IOException during connection to "+url_string+": "+e.toString(),GSXML.ERROR_TYPE_SYSTEM);
|
---|
[9234] | 78 | }
|
---|
[9874] | 79 |
|
---|
[9234] | 80 | String node_content = buffer.toString();
|
---|
| 81 |
|
---|
| 82 | String escaped_content = GSXML.xmlSafe(node_content);
|
---|
| 83 |
|
---|
| 84 | StringBuffer processed_content = new StringBuffer(escaped_content.length());
|
---|
| 85 | processed_content.append("<nodeContent>");
|
---|
| 86 | int pos = 0;
|
---|
| 87 | int lastpos = 0;
|
---|
| 88 | while ((pos = escaped_content.indexOf("<a ", lastpos))!= -1) {
|
---|
| 89 | processed_content.append(escaped_content.substring(lastpos, pos));
|
---|
| 90 | int endpos = escaped_content.indexOf("</a>", pos);
|
---|
| 91 | if (endpos == -1) {
|
---|
| 92 | break;
|
---|
| 93 | }
|
---|
| 94 | String link = escaped_content.substring(pos, endpos+10);
|
---|
| 95 | link = convertLink(link);
|
---|
| 96 | processed_content.append(link);
|
---|
| 97 | lastpos = endpos+10;
|
---|
| 98 | }
|
---|
| 99 | processed_content.append(escaped_content.substring(lastpos)); // get the last bit
|
---|
| 100 | processed_content.append("</nodeContent>");
|
---|
| 101 |
|
---|
[9874] | 102 | Document content_doc = this.converter.getDOM(processed_content.toString());
|
---|
| 103 | if (content_doc == null) {
|
---|
[13124] | 104 | logger.error("Couldn't parse node content");
|
---|
| 105 | logger.error(processed_content.toString());
|
---|
[9874] | 106 | return null;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | Element content_element = content_doc.getDocumentElement();
|
---|
[9234] | 110 |
|
---|
[28966] | 111 | return (Element)doc.importNode(content_element,true);
|
---|
[9234] | 112 | }
|
---|
| 113 |
|
---|
| 114 | /** converts a url from an <a> element into a greenstone suitable one */
|
---|
| 115 | protected String convertLink(String aref) {
|
---|
| 116 | if (aref.indexOf("href="http") != -1) {
|
---|
| 117 | return aref; // an external link
|
---|
| 118 | }
|
---|
| 119 | String type = "other";
|
---|
| 120 | if (aref.indexOf("/cgi-bin/canned_search")!=-1) {
|
---|
| 121 | type="query";
|
---|
| 122 | } else if (aref.indexOf("/cgi-bin/click_through") != -1) {
|
---|
| 123 | type = "external";
|
---|
| 124 | } else if (aref.indexOf("/cgi-bin/view_record") != -1) {
|
---|
| 125 | type="document";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | int href_start = aref.indexOf("href="")+11;
|
---|
| 129 | int href_end = aref.indexOf(">", href_start);
|
---|
| 130 | String href = aref.substring(href_start, href_end);
|
---|
| 131 | String link_content = aref.substring(href_end+4, aref.length()-10);
|
---|
| 132 |
|
---|
| 133 | if (type.equals("external")) {
|
---|
| 134 | // the external link is everything after the http at the end.
|
---|
| 135 | String address = href.substring(href.lastIndexOf("http"));
|
---|
| 136 | address = address.replaceAll("%3[aA]", ":");
|
---|
| 137 | address = address.replaceAll("%2[fF]", "/");
|
---|
| 138 |
|
---|
| 139 | return "<a href=\""+address+"\">"+link_content+"</a>";
|
---|
| 140 | }
|
---|
| 141 | if (type.equals("other")) {
|
---|
| 142 | return "other type of link ("+link_content+")";
|
---|
| 143 | }
|
---|
| 144 | StringBuffer result = new StringBuffer();
|
---|
| 145 | result.append("<link type='");
|
---|
| 146 | result.append(type);
|
---|
| 147 | result.append("'");
|
---|
| 148 | if (type.equals("query")) {
|
---|
| 149 | result.append(" service='TextQuery'");
|
---|
| 150 | }
|
---|
| 151 | result.append(">");
|
---|
| 152 | // add in the parameters
|
---|
| 153 | href = href.substring(href.indexOf("?")+1);
|
---|
| 154 | String [] params = href.split("&");
|
---|
| 155 | for (int i=0; i<params.length; i++) {
|
---|
| 156 | String param = params[i];
|
---|
| 157 | int eq_pos = param.indexOf("=");
|
---|
| 158 | if (eq_pos != -1) {
|
---|
| 159 |
|
---|
| 160 | result.append("<param name='"+param.substring(0, eq_pos)+"' value='"+param.substring(eq_pos+1)+"'/>");
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | result.append(link_content);
|
---|
| 164 | result.append("</link>");
|
---|
| 165 |
|
---|
| 166 | return result.toString();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // iVia craps out if we ask for a metadata which is not valid. So need
|
---|
| 170 | // to make sure we only ask for acceptable fields.
|
---|
| 171 | protected boolean isAcceptableMetadata(String meta) {
|
---|
| 172 | String valid_metadata = ",title,url,ivia_description,keywords,subjects,";
|
---|
| 173 | if (valid_metadata.indexOf(","+meta+",")!=-1) {
|
---|
| 174 | return true;
|
---|
| 175 | }
|
---|
| 176 | return false;
|
---|
| 177 | }
|
---|
[9289] | 178 |
|
---|
[9234] | 179 | protected String translateId(String oid){
|
---|
| 180 | int p = oid.lastIndexOf('.');
|
---|
| 181 | if (p != oid.length()-3) {
|
---|
[13124] | 182 | logger.info("translateoid error: '.' is not the third to last char!!");
|
---|
[9234] | 183 | return oid;
|
---|
| 184 | }
|
---|
| 185 | String top = oid.substring(0, p);
|
---|
| 186 | return top;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | protected String translateExternalId(String id){
|
---|
| 190 | return id;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | protected String getDocType(String node_id){
|
---|
| 194 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
| 195 | }
|
---|
| 196 | protected String getRootId(String node_id){
|
---|
| 197 | return node_id;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[25635] | 200 | protected ArrayList<String> getChildrenIds(String node_id){
|
---|
[9234] | 201 | return null;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | protected String getParentId(String node_id){
|
---|
| 205 | return null;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[28966] | 208 | protected Element getMetadataList (Document doc, String doc_id,
|
---|
[9234] | 209 | boolean all_metadata,
|
---|
[26215] | 210 | ArrayList<String> metadata_names,
|
---|
| 211 | String lang)
|
---|
[9874] | 212 | throws GSException {
|
---|
[9234] | 213 |
|
---|
[28966] | 214 | Element meta_list = doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
[9234] | 215 |
|
---|
| 216 | // do the query to the iVia server
|
---|
| 217 | StringBuffer field_list= new StringBuffer();
|
---|
| 218 | boolean metadata_found = false;
|
---|
| 219 |
|
---|
| 220 | for (int i=0; i<metadata_names.size();i++){
|
---|
[25635] | 221 | if (isAcceptableMetadata(metadata_names.get(i))){
|
---|
[9234] | 222 | metadata_found = true;
|
---|
[25635] | 223 | field_list.append(metadata_names.get(i));
|
---|
[9234] | 224 | field_list.append(",");
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | if (!metadata_found){
|
---|
| 228 | return meta_list;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | String url_string = ivia_server_url+"/cgi-bin/view_record_set?theme=gsdl3&record_id_list="+doc_id+"&field_list="+field_list.toString();
|
---|
| 232 | try {
|
---|
[9874] | 233 | BufferedReader reader = Misc.makeHttpConnection(url_string);
|
---|
[9234] | 234 | String line;
|
---|
| 235 | while ((line = reader.readLine()) != null) {
|
---|
| 236 | //metadata entry
|
---|
| 237 | int col_pos = line.indexOf(':');
|
---|
| 238 | if (col_pos == -1) {
|
---|
| 239 | // end of the metadata for this doc
|
---|
| 240 | break;
|
---|
| 241 | }
|
---|
| 242 | String name = line.substring(0,col_pos);
|
---|
| 243 | String value = line.substring(col_pos+2); // includes a space
|
---|
[28966] | 244 | GSXML.addMetadata(meta_list, name, value);
|
---|
[9234] | 245 | }
|
---|
[9874] | 246 | } catch (java.net.MalformedURLException e) {
|
---|
| 247 | throw new GSException("Malformed URL: "+url_string, GSXML.ERROR_TYPE_SYSTEM);
|
---|
| 248 | } catch (java.io.IOException e) {
|
---|
| 249 | throw new GSException("IOException: "+e.toString(), GSXML.ERROR_TYPE_SYSTEM);
|
---|
[9234] | 250 | }
|
---|
| 251 | return meta_list;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | protected String getStructureInfo(String doc_id, String info_type){
|
---|
| 255 | return "";
|
---|
| 256 | }
|
---|
| 257 | }
|
---|