01: /*
02: * RemoteSearch.java
03: *
04: * Created on February 11, 2004, 5:14 PM
05: */
06:
07: package com.sun.portal.search.autoclassify;
08:
09: import com.sun.portal.search.rdm.*;
10: import com.sun.portal.search.soif.*;
11: import com.sun.portal.search.util.SearchLogger;
12: import java.net.*;
13: import java.util.logging.Level;
14:
15: /**
16: *
17: * @author ac120954
18: */
19: public class RemoteSearch {
20: String scope = null;
21: String viewAttributes = null;
22: String queryLanguage = null;
23: String database = null;
24: String RDMServer = null;
25: String user = null;
26: String password = null;
27:
28: /** Creates a new instance of RemoteSearch */
29: public RemoteSearch(String scope, String viewAttributes,
30: String queryLanguage, String database, String RDMServer,
31: String user, String password) {
32: this .scope = scope;
33: this .viewAttributes = viewAttributes;
34: this .queryLanguage = queryLanguage;
35: this .database = database;
36: this .RDMServer = RDMServer;
37: this .user = user;
38: this .password = password;
39: }
40:
41: public SOIFInputStream getResult() throws Exception {
42: SOIFInputStream resultStream = null;
43: RDMHeader reqHeader = null;
44: RDMQuery qry = new RDMQuery(scope);
45: String type = "rd-request";
46:
47: // reset result info
48: RDMHeader resHeader = null;
49: //RDM_Response_Interpret = null;
50: SOIFBuffer sb = new SOIFBuffer();
51: reqHeader = new RDMHeader(type, null, queryLanguage, null);
52: if (user != null) {
53: reqHeader.setUser(user);
54: if (password != null) {
55: reqHeader.setPassword(password);
56: }
57: }
58: qry.setViewHits("1..99999");
59: if (viewAttributes != null) {
60: qry.setViewAttr(viewAttributes);
61: }
62:
63: if (database != null) {
64: qry.setDatabase(database);
65: }
66: SOIFOutputStream sos = new SOIFOutputStream(sb);
67: sos.write(reqHeader.getSOIF());
68: sos.write(qry.getSOIF());
69:
70: /* set up the input stream and parse the rdm header */
71: URLConnection rc = new URL(RDMServer).openConnection();
72: rc.setAllowUserInteraction(true);
73: rc.setDoOutput(true);
74: rc.getOutputStream().write(sb.toByteArray());
75: resultStream = new SOIFInputStream(rc.getInputStream());
76: try {
77: resHeader = new RDMHeader(resultStream);
78: } catch (Exception e) {
79: throw new Exception(
80: "Inavlid response, might not from a search server",
81: e);
82: }
83: String rdmEMsg = resHeader.getErrorMessage();
84: if (rdmEMsg != null) {
85: if (!rdmEMsg.equalsIgnoreCase("0 results")) {
86: SearchLogger.getLogger().log(Level.WARNING, rdmEMsg);
87: return null;
88: }
89:
90: }
91: return resultStream;
92:
93: }
94:
95: }
|