📄 searchservlet.java
字号:
int ws; // Word Scores: 1==show word scores for each hit String oldqt; // Old Query Terms: used by smart no hits int tt; // Display all topics: 0==default 1==all Date after; // Advanced Query Form Date before; // Advanced Query Form //String op[]; // op0 .. opN Advanced Operation //String fl[]; // fl0 .. flN Advanced Field //String ty[]; // ty0 .. tyN Advanced Type //String tx[]; // tx0 .. txN Advanced Text String qtxt; // Composed User Query Text (including Advanced options) StandardSearchTopic topic; DateFormat fmt; // Display format for dates (set by la parameter) Query query; // Parsed and compiled Query Map styleSettings; // Style settings (selected by style parameter) String spellingSuggestion; // null if no suggestion boolean showedQuickLink = false; SearchResultList searchResults = null; // The results of the search Collection relatedTopics = null; int availableHitCount = 0; // Max number of hits available to display int serverHitCount = 0; // Total number of hits SearchRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { this.req = req; this.resp = resp; decideLocaleForRequest(); // set response content type resp.setContentType("text/html; charset="+page_output_charset); // Now get the PrintWriter -- it will translate output to page_output_charset out = resp.getWriter(); } /** * Decide on the Locale to use for parsing the query, * the Locale for the UI of the search request response, * and the character set of the HTTP response. * @since XPA2.2 */ protected void decideLocaleForRequest() { // See if a Locale is specified with the query la = req.getParameter("la"); locale = la_locale(la); if (locale==null) // parses accept-language to determine initial locale // or will set to server's default locale. locale = req.getLocale(); if (locale==null) locale = Locale.ENGLISH; resp.setLocale(locale); la = locale_la(locale); // Now decide the character set for the output page. page_output_charset = la_charset(la); } protected String BORDER = ""; // Set to " border=1" for debugging tables /** * Display debugging information specific to the Servlet being debugged. * @see DebugServlet.SearchRequest#showDebugInformation */ protected void showDebugServletSpecific() { } /** * This method emits information designed for debugging * the SearchServlet. * @see DebugServlet.SearchRequest#showDebugInformation */ protected void showDebugInformation() { } /** * Methods for fetching style settings. * If this request has a style mapping, we'll use values * from there. * Otherwise we'll look in the "defaultStyleSettings" mapping. * Finally, we'll look in the "serverStyleSettings" mapping. * @see #parseStyleSetting * @see #showStyleSheet * @see #getStyleSettingObject */ protected String getStyleSetting(String key) { Object val = getStyleSettingObject(key); if (val==null) return ""; return val.toString(); } protected Object getStyleSettingObject(String key) { if (key==null) return null; if (styleSettings!=null) { if (styleSettings.containsKey(key)) return styleSettings.get(key); } if (defaultStyleSettings.containsKey(key)) return defaultStyleSettings.get(key); if (serverStyleSettings.containsKey(key)) return serverStyleSettings.get(key); return null; } protected int getStyleSettingInt(String key, int def) { Object o = getStyleSettingObject(key); if (o==null) return def; if (o instanceof Integer) return ((Integer) o).intValue(); if (!(o instanceof String)) return def; String s = (String) o; if ("".equals(s)) return def; try { return Integer.parseInt(s); } catch (NumberFormatException e) { return def; } } protected int getStyleSettingInt(String key) { return getStyleSettingInt(key,0); } protected boolean getStyleSettingBoolean(String key, boolean def) { int i = getStyleSettingInt(key,(def) ? 1 : 0); return !(i==0); } protected boolean getStyleSettingBoolean(String key) { return getStyleSettingBoolean(key,false); } /** * Query Parameters which differ from the current style's * default setting. These parameters are usually included * on any links back to the search page. */ protected Map query_nodef = new HashMap(); protected int getParameterOrStyleInt(String name, String def) { String value = getParameterOrStyleString(name,def); if (value!=null) try { return Integer.parseInt(value); } catch (Exception ignored) {}; return getStyleSettingInt(def,0); } protected int getParameterInt(String name, int def) { String value = getParameterString(name); if (value!=null) try { return Integer.parseInt(value); } catch (Exception e) {}; return def; } protected String getParameterOrStyleString(String name, String def) { String value = getParameterString(name); String style_value = getStyleSetting(def); if (value!=null) { if (!value.equals(style_value)) query_nodef.put(name,value); return value; } return style_value; } protected String getMultiParameterOrStyleString(String name, String def) { String value = getMultiParameterString(name); String style_value = getStyleSetting(def); if (value!=null) { if (!value.equals(style_value)) query_nodef.put(name,value); return value; } return style_value; } /** * Generate a URL that the browser client can use to access * the Ultraseek server. * Primarily this will resolve "localhost". */ protected String makeAbsoluteUltraseekURL(UltraseekServer server) { String host = server.getHost(); if (host.equalsIgnoreCase("localhost")) // Use the hostname the browser used to reach this servlet host = req.getServerName(); String rawURL = server.getProtocol() + "://" + host + ":" + server.getPort(); try { URL url = new URL(rawURL); return url.toString(); } catch (MalformedURLException e) { return rawURL; } } /** * This method is called immediately before the <TITLE> HTML tag is * output. It is a good place to output any references to * the display style sheet. * @see #parseStyleSetting * @see #getStyleSettingObject **/ protected void showStyleSheet() throws IOException, ServletException { DEBUG(resp,"showStyleSheet"); // We use the Ultraseek server to provide style settings. // Think of the LDAP search case .. still want the style, but server becomes // LDAPServer out.println("<link rel=stylesheet href=\"" + makeAbsoluteUltraseekURL(ultraseek) + "/default.css" + "\">"); if (!isEmpty(style)) { // Ultraseek version 5.1 supports multiple display styles out.println("<link rel=stylesheet href=\"" + makeAbsoluteUltraseekURL(ultraseek) + "/styles/" + style + ".css" + "\">"); } DEBUG(resp,"/showStyleSheet"); } /** * This method is called immediately after the <BODY> HTML tag is * output. It is a good place to output any customized HTML to * appear before the content. **/ protected void showBodyHeader() throws ServletException { DEBUG(resp,"showBodyHeader"); out.print( getStyleSetting("header_html") ); out.print("<div class=\"yourlogo\"></div>" ); DEBUG(resp,"/showBodyHeader"); } /** * This method is called immediately before the </BODY> HTML tag is * output. It is a good place to output any customized HTML to * appear after the content. **/ protected void showBodyFooter() throws ServletException { DEBUG(resp,"showBodyFooter"); out.print( getStyleSetting("footer_html") ); DEBUG(resp,"/showBodyFooter"); } /** * Internal exception used to force the response to a request to restart. * Should only be thrown if the response is not committed (eg: headers have not been sent) * (!resp.isCommitted()) * @serial exclude */ protected class RestartResponseException extends ServletException { public RestartResponseException(String msg) {}; } protected void maybeReinitialize() throws ServletException { // See if search menus should be reinitialized if (getParameterString("reinit")!=null) SearchServlet.this.configuration.update(); else SearchServlet.this.configuration.maybeUpdate(); } protected void handle() throws IOException, ServletException { maybeReinitialize(); queryArrivalTime = new Date(); parseQueryParameters(); boolean try_again = true; while (try_again) { try { try_again = false; try { setupSearch(); showHTMLHeader(); out.println("<body bgcolor=\"#ffffff\">"); showHTMLBody(); } catch (IOException e) { log( "Problem communicating with search server", e ); showTroubleReport(e); } catch (XPARuntimeException e) { log( "Problem communicating with search server", e ); showTroubleReport(e); } showDebugInformation(); out.println("</body>"); out.println("</html>"); out.flush(); // Finish sending result to client postResponseHandling(); } catch (RestartResponseException e) { // NOTE: to ensure the response is not already commited // by DEBUG code, set debug_flush to false. DEBUG(out,"caught RestartResponseException"); if (!resp.isCommitted()) { resp.reset(); try_again = true; } } } } /** * Perform processing after the HTTP response has been sent. * <p> * This method is invoked after the query has been processed, results sent, * and the HTTP response buffer has been flushed. * All time-critical actions have been performed, and now the * Servlet can spend time to log the query or perhaps pre-fetch * some more search results. * <p> * As of XPA 2.2, the default implemenation will log the * query to the Ultraseek server. * @see #getQueryLogger * @see QueryLog */ protected void postResponseHandling() throws ServletException { if (st==1 && query != null) { /* This search query is requesting first page of results, * so we will log it to the query log. */ QueryLog queryLogger = getQueryLogger(); if (queryLogger != null) { try { queryLogger.logQuery(query,searchResults,menuItem.ID,queryArrivalTime); } catch (QueryNotSupportedException exc) { log("Problem logging query", exc); } catch (ProtocolException exc) { if (!(queryLogger instanceof UltraseekServer)) { log("Problem logging query", exc); } else { try { UltraseekServer queryServer = (UltraseekServer) queryLogger; if (queryServer.versionAtLeast(5,3,3)) /* Version 5.3.3 supports query logging from XPA, so this * must be a real problem. */ log("Problem logging query", exc); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -