📄 proxyservlet.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.10 $ $Date: 2006/02/06 21:26:47 $ * * Copyright (c) 2000-2004 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.*;import java.net.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.*;/** * The server side of the ProxyServer class. * @serial exclude */public class ProxyServlet extends HttpServlet { Collection ids; Collection servers; Map id_server; Map id_searchable; Map id_name; Map id_defaultSearch; Map id_defaultShow; Map collection_id; long configurationTimestamp; long topicsTimestamp; /** * Here is where you define your SearchCollections for this * SearchServer. To configure this Servlet for your installation, * replace the given sample code with your own. * **/ private void initCollections() throws IOException { SearchServer s = new UltraseekServer("software-search.ultraseek.com",80); initCollection( "software", // ID "Ultraseek Website", // name true, // defaultSearch true, // defaultShow s, // a SearchServer s.getSearchCollection("software") // a Searchable ); initCollection( "ultrafaq", // ID "Ultraseek Documentation", // name true, // defaultSearch true, // defaultShow s, // a SearchServer s.getSearchCollection("ultrafaq") // a Searchable ); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { long currentTimeMillis = System.currentTimeMillis(); configurationTimestamp = (currentTimeMillis/configurationRefreshInterval)* configurationRefreshInterval; topicsTimestamp = (currentTimeMillis/topicsRefreshInterval)* topicsRefreshInterval; try { if (req.getParameter("collections")!=null) doCollections(req,resp); else if (req.getParameter("topics")!=null) doTopics(req,resp); else doSearch(req,resp); } catch (ServletException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw new ServletException(e.getLocalizedMessage(),e); } } private void doCollections(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { resp.setContentType("application/byte_stream"); DataOutputStream dos = new DataOutputStream(resp.getOutputStream()); dos.writeInt(1); dos.writeLong(configurationTimestamp); writeLocales(dos); writeCollections(dos); dos.close(); } private void doTopics(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { resp.setContentType("application/byte_stream"); DataOutputStream dos = new DataOutputStream(resp.getOutputStream()); dos.writeInt(1); dos.writeLong(topicsTimestamp); writeTopics(dos); dos.close(); } private void doSearch(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String la = req.getParameter("la"); Locale locale = la_locale(la); String col = req.getParameter("col"); if (col==null) { Iterator it = id_searchable.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry)it.next(); String id = (String)e.getKey(); Searchable s = (Searchable)e.getValue(); if (locale==null || s.getLocales().contains(locale)) { col = id; break; } } } Searchable searchable = getSearchable(col); if (locale==null) locale = searchable.getLocale(); if (la==null) la = locale_la(locale); String charset = req.getParameter("charset"); if (charset==null) charset = "UTF8"; String qt = decode(req.getParameter("qt"),charset); String ct = req.getParameter("ct"); long after = getLongParameter(req,"after",Long.MAX_VALUE); long before = getLongParameter(req,"before",Long.MIN_VALUE); int st = getIntParameter(req,"st",1); int nh = getIntParameter(req,"nh",25); List showList = emptyList; int docCount = 0; int resultCount = 0; int size = 0; Collection relatedTopics = emptyCollection; Map termDFs = emptyMap; Query query = getQuery(req,locale,qt,ct,after,before); if (query!=null) { int numErrorRetries = 0; boolean retrying = false; do { try { retrying = false; showList = emptyList; SearchResultList results = searchable.search(query); resultCount = results.getResultCount(); size = results.size(); docCount = results.getDocCount(); relatedTopics = results.getRelatedTopics(); Iterator it = results.iterator(); int resultNumber=0, showNumber=1; while (!retrying && it.hasNext() && showNumber<st+nh) { try { resultNumber++; SearchResult result = (SearchResult)it.next(); if (showNumber++>=st) { if (showList==emptyList) showList = new ArrayList(nh); showList.add(result); } } catch (NoSuchElementException e) { resultCount = showNumber; size = showNumber; break; } } } catch (IOException e) { if (numErrorRetries++ > 0) throw e; retrying = true; } catch (RuntimeException e) { if (numErrorRetries++ > 0) throw e; retrying = true; } catch (QueryNotSupportedException e) { throw new ServletException(e.getLocalizedMessage(),e); } } while (retrying); } resp.setContentType("application/byte_stream"); DataOutputStream dos = new DataOutputStream(resp.getOutputStream()); writeHeader(dos,resultCount,size,docCount,locale); writeTopicIDs(dos,relatedTopics); writeTermFrequencies(dos,termDFs); writeResults(dos,showList); dos.close(); } private static Collection getCollectionsFromSearchable(SearchCollection s) { Collection c = new ArrayList(1); c.add(s); return c; } private static Collection getCollectionsFromSearchable(SearchServer s) throws IOException { Collection c = new ArrayList(); Iterator it = s.getSearchCollections().iterator(); while (it.hasNext()) { SearchCollection sc = (SearchCollection)it.next(); if (sc.getDefaultSearch()) c.add(sc); } return c; } private static Collection getCollectionsFromSearchable(CachingSearchable s) throws IOException { return getCollectionsFromSearchable(s.getSearchable()); } private static Collection getCollectionsFromSearchable(CompositeSearchable s) throws IOException { Collection c = new ArrayList(); Searchable[] searchables = s.getSearchables(); for (int i=0; i<searchables.length; i++) { c.addAll(getCollectionsFromSearchable(searchables[i])); } return c; } private static Collection getCollectionsFromSearchable(DateSortingSearchable s) throws IOException { return getCollectionsFromSearchable(s.getSearchable()); } private static Collection getCollectionsFromSearchable(GuardingSearchable s) throws IOException { return getCollectionsFromSearchable(s.getSearchable()); } private static Collection getCollectionsFromSearchable(SubsetSearchable s) throws IOException { return getCollectionsFromSearchable(s.getSearchable()); } private static Collection getCollectionsFromSearchable(TitleSortingSearchable s) throws IOException { return getCollectionsFromSearchable(s.getSearchable()); } private static Collection getCollectionsFromSearchable(Searchable s) throws IOException { if (s instanceof SearchCollection) return getCollectionsFromSearchable((SearchCollection)s); else if (s instanceof SearchServer) return getCollectionsFromSearchable((SearchServer)s); else if (s instanceof CachingSearchable) return getCollectionsFromSearchable((CachingSearchable)s); else if (s instanceof CompositeSearchable) return getCollectionsFromSearchable((CompositeSearchable)s); else if (s instanceof DateSortingSearchable) return getCollectionsFromSearchable((DateSortingSearchable)s); else if (s instanceof GuardingSearchable) return getCollectionsFromSearchable((GuardingSearchable)s); else if (s instanceof SubsetSearchable) return getCollectionsFromSearchable((SubsetSearchable)s); else if (s instanceof TitleSortingSearchable) return getCollectionsFromSearchable((TitleSortingSearchable)s); else return emptyCollection; } private void initCollection( String id, String name, boolean defaultSearch, boolean defaultShow, SearchServer server, Searchable searchable) throws IOException { ids.add(id); servers.add(server); id_name.put(id,name); id_defaultSearch.put(id,new Boolean(defaultSearch)); id_defaultShow.put(id,new Boolean(defaultShow)); id_server.put(id,server); id_searchable.put(id,searchable); Collection c = getCollectionsFromSearchable(searchable); Iterator it = c.iterator(); while (it.hasNext()) { SearchCollection sc = (SearchCollection)it.next(); if (!collection_id.containsKey(sc) || c.size()==1) { collection_id.put(sc,id); } } } public void init(ServletConfig config) throws ServletException { super.init(config); ids = new HashSet(); servers = new HashSet(); id_name = new TreeMap(); id_server = new HashMap(); id_searchable = new HashMap(); id_defaultSearch = new HashMap(); id_defaultShow = new HashMap(); collection_id = new HashMap(); try { initCollections(); } catch (IOException e) { throw new ServletException(e.getLocalizedMessage(),e); } } private void writeLocales(DataOutputStream dos, Collection locales) throws IOException { Iterator it = locales.iterator(); int numLocales = locales.size(); dos.writeInt(numLocales); for (int i=0; i<numLocales; i++) { Locale locale = (Locale)it.next(); dos.writeUTF(locale_la(locale)); } } private void writeLocales(DataOutputStream dos) throws IOException { Set locales = new HashSet(); Iterator it1 = ids.iterator(); while (it1.hasNext()) { String id = (String)it1.next(); Searchable searchable = (Searchable)id_searchable.get(id); Iterator it2 = searchable.getLocales().iterator(); while (it2.hasNext()) { Locale locale = (Locale)it2.next(); locales.add(locale); } } writeLocales(dos,locales); } private void writeCollection(DataOutputStream dos, String id) throws IOException { Searchable searchable = (Searchable)id_searchable.get(id); String name = (String)id_name.get(id); boolean defaultSearch = ((Boolean)id_defaultSearch.get(id)).booleanValue(); boolean defaultShow = ((Boolean)id_defaultShow.get(id)).booleanValue(); Locale locale = searchable.getLocale(); Collection locales = searchable.getLocales(); dos.writeUTF(id); dos.writeUTF(name); dos.writeBoolean(defaultSearch); dos.writeBoolean(defaultShow); dos.writeUTF(locale_la(locale)); writeLocales(dos,locales); } private void writeCollections(DataOutputStream dos) throws IOException { Iterator it = ids.iterator(); int numCollections = ids.size(); dos.writeInt(numCollections); for (int i=0; i<numCollections; i++) { String id = (String)it.next(); writeCollection(dos,id); } } private void writeTopic(DataOutputStream dos, StandardSearchTopic sst) throws IOException { String id = sst.getID(); String name = sst.getName();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -