001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.rdmserver;
007:
008: import com.sun.portal.search.rdm.*;
009: import com.sun.portal.search.db.*;
010: import com.sun.portal.search.soif.*;
011: import com.sun.portal.search.util.*;
012:
013: import java.io.*;
014: import java.util.*;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017: import javax.servlet.*;
018: import javax.servlet.http.*;
019: import java.text.*;
020:
021: /**
022: * RDMServlet:
023: * Server-side glue for RDM written for HTTP Servlet.
024: * Routes the unencapsulated RDM to the appropriate process,
025: * and returns RDM response to the client.
026: *
027: * Any request using the URI starting with /rdm/ is defined to be
028: * handled by the RDM server.
029: *
030: * Usage:
031: * In web.xml
032: * <servlet>
033: * <servlet-name>com.sun.portal.search.rdmserver.RDMServlet</servlet-name> XXX
034: * <servlet-class>com.sun.portal.search.rdmserver.RDMServlet</servlet-class>
035: * <init-param>
036: * <param-name>serverroot</param-name>
037: * <param-value>path/to/rdm_server_root_directory</param-value>
038: * </init-param>
039: * </servlet>
040: */
041: public class RDMServlet extends HttpServlet {
042:
043: // RDM objects - one of each per RDMServlet
044: RDMContext rdmcontext; // one per webapp?
045: RDMServer rdmserver;
046: RDMConfig rdmconfig;
047:
048: /** Initializes the servlet. */
049: public void init(ServletConfig svltconfig) throws ServletException {
050: super .init(svltconfig);
051:
052: try {
053: // messy - some config parms are per server, some per context
054: rdmcontext = new RDMServletContext(svltconfig);
055: rdmconfig = rdmcontext.getRDMConfig();
056: rdmserver = new RDMServer(rdmcontext);
057: rdmserver.init(rdmconfig);
058:
059: // default mgr if there is no class set for security manager
060: RDMSecurityManager sec_mgr = RDMSecurityManager
061: .getInstance();
062:
063: // Document level security setting
064: String mode = SearchConfig.getValue(SearchConfig.SECMODE);
065:
066: } catch (Exception e) {
067: try {
068: SearchLogger.getLogger().log(Level.WARNING,
069: "PSSH_CSPSRDMS0054", e);
070: rdmcontext.log("RDM Server failed to start", e);
071: } catch (Exception ignored) {
072: }
073: throw new ServletException(e);
074: }
075: SearchLogger.getLogger().log(Level.SEVERE, "PSSH_CSPSRDMS0055",
076: rdmserver.getVersion());
077: //rdmcontext.log("RDM Server is on-line: " + rdmserver.getVersion());
078: if (SearchLogger.getLogger().isLoggable(Level.FINEST))
079: dumpProps(svltconfig);
080: }
081:
082: void dumpProps(ServletConfig svltconfig) {
083: String args = "\nSystem Properties\n";
084: Enumeration en = System.getProperties().propertyNames();
085: while (en.hasMoreElements()) {
086: String arg = (String) en.nextElement();
087: args += arg + ":" + System.getProperty(arg) + "\n";
088: }
089: args += "\nContext attributes\n";
090: en = svltconfig.getServletContext().getAttributeNames();
091: while (en.hasMoreElements()) {
092: String arg = (String) en.nextElement();
093: args += arg + ":"
094: + svltconfig.getServletContext().getAttribute(arg)
095: + "\n";
096: }
097: args += "\nContext initparms\n";
098: en = svltconfig.getServletContext().getInitParameterNames();
099: while (en.hasMoreElements()) {
100: String arg = (String) en.nextElement();
101: args += arg
102: + ":"
103: + svltconfig.getServletContext().getInitParameter(
104: arg) + "\n";
105: }
106: args += "\nServlet initparms\n";
107: en = svltconfig.getInitParameterNames();
108: while (en.hasMoreElements()) {
109: String arg = (String) en.nextElement();
110: args += arg + ":" + svltconfig.getInitParameter(arg) + "\n";
111: }
112: SearchLogger.getLogger().log(Level.FINEST, "PSSH_CSPSRDMS0056",
113: args);
114: }
115:
116: /** Destroys the servlet. */
117: public void destroy() {
118: try {
119: rdmserver.shutdown();
120: } catch (Exception e) {
121: SearchLogger.getLogger().log(Level.WARNING,
122: "PSSH_CSPSRDMS0057", e);
123: }
124: }
125:
126: /** Build RDMRequest from query string */
127: public static RDMRequest buildRDMRequest(String queryString)
128: throws java.io.IOException {
129: Map parms = new HashMap();
130: Decoder.decodeQueryString(queryString, "UTF-8", parms);
131:
132: // Convert RDM Header information
133: String type = (String) parms.get("type");
134: String ql = (String) parms.get("ql");
135: String csid = (String) parms.get("csid");
136:
137: RDMHeader hdr = new RDMHeader(type, csid, ql, null);
138: String sso = (String) parms.get("stoken");
139: // adding following to allow proxyDN search
140: // used for services like profiler
141: if (sso != null)
142: hdr.setAccessToken(sso);
143: String user = (String) parms.get("user");
144: if (user != null)
145: hdr.setUser(user);
146: String pw = (String) parms.get("pw");
147: if (pw != null)
148: hdr.setPassword(pw);
149:
150: // Convert RDM Query information
151: String scope = (String) parms.get(RDM.A_RDM_SCOPE);
152: String mode = (String) parms.get(RDM.A_RDM_MODE);
153: String attr = (String) parms.get(RDM.A_RDM_VIEWATT);
154: String hits = (String) parms.get(RDM.A_RDM_VIEWHIT);
155: String order = (String) parms.get(RDM.A_RDM_VIEWORD);
156: String searchCat = (String) parms
157: .get(RDM.A_RDM_SEARCH_CATEGORY);
158: String database = (String) parms.get(RDM.A_RDM_DATABASE);
159: String highlight = (String) parms.get(RDM.A_RDM_HIGHLIGHT);
160: String highlightTags = (String) parms
161: .get(RDM.A_RDM_HIGHLIGHT_TAGS);
162: String highlightTagsField = (String) parms
163: .get(RDM.A_RDM_HIGHLIGHT_TAGS_FIELD);
164: String passageContext = (String) parms
165: .get(RDM.A_RDM_PASSAGE_CONTEXT);
166: String maxPassages = (String) parms.get(RDM.A_RDM_MAX_PASSAGES);
167:
168: // Javascript friendly param names
169: if (attr == null)
170: attr = (String) parms.get("va");
171: if (hits == null)
172: hits = (String) parms.get("vh");
173: if (order == null)
174: order = (String) parms.get("vo");
175: if (highlight == null)
176: highlight = (String) parms.get("hl");
177: if (highlightTags == null)
178: highlightTags = (String) parms.get("hlt");
179: if (highlightTagsField == null)
180: highlightTagsField = (String) parms.get("hltf");
181: if (passageContext == null)
182: passageContext = (String) parms.get("pc");
183: if (maxPassages == null)
184: maxPassages = (String) parms.get("mp");
185:
186: RDMQuery qry = new RDMQuery(scope);
187: if (mode != null && mode.length() > 0)
188: qry.setMode(mode);
189: if (attr != null && attr.length() > 0)
190: qry.setViewAttr(attr);
191: if (hits != null && hits.length() > 0)
192: qry.setViewHits(hits);
193: if (order != null && order.length() > 0)
194: qry.setViewOrder(order);
195: if (searchCat != null && searchCat.length() > 0)
196: qry.setSearchCategory(searchCat);
197: if (database != null && database.length() > 0)
198: qry.setDatabase(database);
199: if (highlight != null && highlight.length() > 0)
200: qry.setHighlight(highlight);
201: if (highlightTags != null && highlightTags.length() > 0)
202: qry.setHighlightTags(highlightTags);
203: if (highlightTagsField != null
204: && highlightTagsField.length() > 0)
205: qry.setHighlightTagsField(highlightTagsField);
206: if (passageContext != null && passageContext.length() > 0)
207: qry.setPassageContext(passageContext);
208: if (maxPassages != null && maxPassages.length() > 0)
209: qry.setMaxPassages(maxPassages);
210:
211: if (qry.getSOIF().getAttributeCount() == 0)
212: qry = null; // there was no query
213: RDMRequest rdmrequest = new RDMRequest(hdr, qry);
214: return rdmrequest;
215: }
216:
217: /**
218: * Handles the HTTP <code>GET</code> method.
219: * @param request servlet request
220: * @param response servlet response
221: */
222: protected void doGet(HttpServletRequest request,
223: HttpServletResponse response) throws ServletException,
224: java.io.IOException {
225: RDMRequest rdmrequest = null;
226: try {
227: String qs = request.getQueryString();
228: rdmrequest = buildRDMRequest(qs);
229: } catch (Exception e) {
230: SearchLogger.getLogger().log(Level.FINEST,
231: "PSSH_CSPSRDMS0058", e);
232: throw new ServletException(
233: "Did not receive valid RDM Request - GET method", e);
234: }
235: processRDMRequest(request, response, rdmrequest);
236: }
237:
238: /**
239: * Handles the HTTP <code>POST</code> method.
240: * @param request servlet request
241: * @param response servlet response
242: */
243: protected void doPost(HttpServletRequest request,
244: HttpServletResponse response) throws ServletException,
245: java.io.IOException {
246: RDMRequest rdmrequest = null;
247: SOIFInputStream sis = null;
248: try {
249: /*int n = 0, cnt = 0;
250: byte[] line = new byte[10000];
251: for (;;) {
252: if ((n = request.getInputStream().readLine(line, cnt, 10000)) == -1)
253: break;
254: cnt += n;
255: }
256: sis = new SOIFInputStream(new ByteArrayInputStream(line));*/
257: sis = new SOIFInputStream(request.getInputStream());
258: rdmrequest = new RDMRequest(sis);
259: } catch (Exception e) {
260: SearchLogger.getLogger().log(Level.FINEST,
261: "PSSH_CSPSRDMS0059", e);
262: throw new ServletException(
263: "Did not receive valid RDM Request - POST method",
264: e);
265: }
266: processRDMRequest(request, response, rdmrequest);
267: }
268:
269: /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
270: * @param request servlet request
271: * @param response servlet response
272: */
273: protected void processRDMRequest(HttpServletRequest request,
274: HttpServletResponse response, RDMRequest req)
275: throws ServletException, java.io.IOException {
276: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSRDMS0060");
277: try {
278:
279: SearchLogger.getLogger().log(Level.FINE,
280: "PSSH_CSPSRDMS0061", request.getContentType());
281: SearchLogger.getLogger().log(Level.FINE,
282: "PSSH_CSPSRDMS0063", request.getMethod());
283: SearchLogger.getLogger().log(Level.FINE,
284: "PSSH_CSPSRDMS0064", request.getRequestURI());
285: SearchLogger.getLogger().log(Level.FINE,
286: "PSSH_CSPSRDMS0065", request.getQueryString());
287:
288: // Set HTTP Response Header
289: response.setContentType(RDM.RDM_MIME_TYPE);
290: SearchLogger.getLogger().log(Level.FINE,
291: "PSSH_CSPSRDMS0066");
292:
293: // Create RDM response
294: RDMResponse res = new RDMResponse(new SOIFOutputStream(
295: response.getOutputStream()));
296: res.getHeader().setCSID(req.getHeader().getCSID());
297:
298: SearchLogger.getLogger().log(Level.FINE,
299: "PSSH_CSPSRDMS0067");
300: if (!RDMSecurityManager.getInstance().initRDMSToken(
301: request, req)) {
302: SearchLogger.getLogger().log(Level.WARNING,
303: "PSSH_CSPSRDMS0068", request.getRemoteAddr());
304: res.getHeader().setErrorMessage(
305: "Bad or missing security information");
306: res.getHeader().setResponseInterpret(
307: "0 results out of 0 hits across 0 documents");
308: res.sendHeader();
309: return;
310: }
311:
312: // Service the request
313: rdmserver.service(req, res);
314: } catch (Exception e) {
315: // This should never happen under normal circumstances
316: // - rdmserver.service(req, res) catches exceptions
317: SearchLogger.getLogger().log(Level.WARNING,
318: "PSSH_CSPSRDMS0138", e);
319: rdmcontext.log("Exception: processRDMRequest", e);
320: throw new ServletException(e);
321: } finally {
322: SearchLogger.getLogger().log(Level.FINE,
323: "PSSH_CSPSRDMS0069");
324: }
325: }
326:
327: /** Returns a short description of the servlet.
328: */
329: public String getServletInfo() {
330: return rdmserver.getVersion();
331: }
332: }
|