KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
15 import java.util.WeakHashMap JavaDoc;
16
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.http.HttpServlet JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.eclipse.help.internal.HelpPlugin;
24 import org.eclipse.help.internal.dynamic.DocumentWriter;
25 import org.eclipse.help.internal.index.Index;
26 import org.eclipse.help.internal.index.IndexContribution;
27 import org.eclipse.help.internal.webapp.data.UrlUtil;
28
29 /*
30  * Sends all available keyword index data in XML form. The data is sent as one
31  * large index contribution that includes all merged contributions from the
32  * system.
33  *
34  * This is called on infocenters by client workbenches configured for remote
35  * help in order to gather all the index keywords and assemble them into a
36  * complete index.
37  */

38 public class IndexServlet extends HttpServlet JavaDoc {
39     
40     private static final long serialVersionUID = 1L;
41     private Map JavaDoc responseByLocale;
42     private DocumentWriter writer;
43
44     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
45             throws ServletException JavaDoc, IOException JavaDoc {
46         String JavaDoc locale = UrlUtil.getLocale(req, resp);
47         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
48
resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
49

50         if (responseByLocale == null) {
51             responseByLocale = new WeakHashMap JavaDoc();
52         }
53         String JavaDoc response = (String JavaDoc)responseByLocale.get(locale);
54         if (response == null) {
55             IndexContribution[] contributions = HelpPlugin.getIndexManager().getIndexContributions(locale);
56             try {
57                 response = serialize(contributions, locale);
58             }
59             catch (TransformerException JavaDoc e) {
60                 throw new ServletException JavaDoc(e);
61             }
62             responseByLocale.put(locale, response);
63         }
64         resp.getWriter().write(response);
65     }
66         
67     public String JavaDoc serialize(IndexContribution[] contributions, String JavaDoc locale) throws TransformerException JavaDoc {
68         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
69         buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
70
buf.append("<indexContributions>\n"); //$NON-NLS-1$
71
if (writer == null) {
72             writer = new DocumentWriter();
73         }
74         for (int i=0;i<contributions.length;++i) {
75             IndexContribution contrib = contributions[i];
76             buf.append("<indexContribution\n"); //$NON-NLS-1$
77
buf.append(" id=\"" + contrib.getId() + '"'); //$NON-NLS-1$
78
buf.append(" locale=\"" + contrib.getLocale() + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
79
buf.append(writer.writeString((Index)contrib.getIndex(), false));
80             buf.append("</indexContribution>\n"); //$NON-NLS-1$
81
}
82         buf.append("</indexContributions>\n"); //$NON-NLS-1$
83
return buf.toString();
84     }
85 }
86
Popular Tags