KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > SearchServlet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.webapp.servlet;
12
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServlet JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.help.internal.base.BaseHelpSystem;
27 import org.eclipse.help.internal.search.ISearchHitCollector;
28 import org.eclipse.help.internal.search.ISearchQuery;
29 import org.eclipse.help.internal.search.SearchHit;
30 import org.eclipse.help.internal.search.SearchQuery;
31 import org.eclipse.help.internal.util.URLCoder;
32 import org.eclipse.help.internal.webapp.data.UrlUtil;
33
34 /*
35  * Returns the search hits for the query provided in the phrase parameter.
36  *
37  * This is called on infocenters by client workbenches configured for remote
38  * help in order to retrieve search hits from the remote help server.
39  */

40 public class SearchServlet extends HttpServlet JavaDoc {
41     
42     private static final long serialVersionUID = 1L;
43     private static final String JavaDoc PARAMETER_PHRASE = "phrase"; //$NON-NLS-1$
44
private Collection JavaDoc results = new ArrayList JavaDoc();
45     private ISearchHitCollector collector = new ISearchHitCollector() {
46         public void addHits(List JavaDoc hits, String JavaDoc wordsSearched) {
47             if (results != null) {
48                 results.addAll(hits);
49             }
50         }
51     };
52     
53     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
54             throws ServletException JavaDoc, IOException JavaDoc {
55         String JavaDoc locale = UrlUtil.getLocale(req, resp);
56         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
57
resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
58
String JavaDoc phrase = URLCoder.decode(req.getParameter(PARAMETER_PHRASE));
59         if (phrase != null) {
60             ISearchQuery query = new SearchQuery(phrase, false, Collections.EMPTY_LIST, locale);
61             results.clear();
62             BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
63             String JavaDoc response = serialize(results);
64             resp.getWriter().write(response);
65         }
66         else {
67             resp.sendError(400); // bad request; missing parameter
68
}
69     }
70     
71     public static String JavaDoc serialize(Collection JavaDoc results) {
72         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
73         buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
74
buf.append("<searchHits>\n"); //$NON-NLS-1$
75
Iterator JavaDoc iter = results.iterator();
76         while (iter.hasNext()) {
77             SearchHit hit = (SearchHit)iter.next();
78             serialize(hit, buf, " "); //$NON-NLS-1$
79
}
80         buf.append("</searchHits>\n"); //$NON-NLS-1$
81
return buf.toString();
82     }
83     
84     private static void serialize(SearchHit hit, StringBuffer JavaDoc buf, String JavaDoc indent) {
85         buf.append(indent + "<hit"); //$NON-NLS-1$
86
if (hit.getHref() != null) {
87             buf.append('\n' + indent + " HREF=\"" + XMLGenerator.xmlEscape(hit.getHref()) + '"'); //$NON-NLS-1$
88
}
89         if (hit.getLabel() != null) {
90             buf.append('\n' + indent + " label=\"" + XMLGenerator.xmlEscape(hit.getLabel()) + '"'); //$NON-NLS-1$
91
}
92         if (hit.isPotentialHit()) {
93             buf.append('\n' + indent + " isPotentialHit=\"true\""); //$NON-NLS-1$
94
}
95         buf.append('\n' + indent + " score=\"" + hit.getScore() + '"'); //$NON-NLS-1$
96
buf.append(">\n"); //$NON-NLS-1$
97

98         String JavaDoc summary = hit.getSummary();
99         if (summary != null) {
100             serialize(summary, buf, indent + " "); //$NON-NLS-1$
101
}
102         buf.append(indent + "</hit>\n"); //$NON-NLS-1$
103
}
104
105     private static void serialize(String JavaDoc summary, StringBuffer JavaDoc buf, String JavaDoc indent) {
106         buf.append(indent + "<summary>"); //$NON-NLS-1$
107
buf.append(XMLGenerator.xmlEscape(summary));
108         buf.append("</summary>\n"); //$NON-NLS-1$
109
}
110 }
111
Popular Tags