KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.extension.ContentExtension;
26 import org.eclipse.help.internal.webapp.data.UrlUtil;
27
28 /*
29  * Sends all topic extensions available on this host in XML form.
30  *
31  * This is called on infocenters by client workbenches configured for remote
32  * help in order to gather all the pieces of a document.
33  */

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

46         if (responseByLocale == null) {
47             responseByLocale = new WeakHashMap JavaDoc();
48         }
49         String JavaDoc response = (String JavaDoc)responseByLocale.get(locale);
50         if (response == null) {
51             ContentExtension[] extensions = HelpPlugin.getContentExtensionManager().getExtensions(locale);
52             try {
53                 response = serialize(extensions);
54             }
55             catch (TransformerException JavaDoc e) {
56                 throw new ServletException JavaDoc(e);
57             }
58             responseByLocale.put(locale, response);
59         }
60         resp.getWriter().write(response);
61     }
62     
63     private String JavaDoc serialize(ContentExtension[] extensions) throws TransformerException JavaDoc {
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
65         buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
66
buf.append("<contentExtensions>\n"); //$NON-NLS-1$
67
for (int i = 0; i < extensions.length; ++i) {
68             if (writer == null) {
69                 writer = new DocumentWriter();
70             }
71             buf.append(writer.writeString(extensions[i], false));
72         }
73         buf.append("</contentExtensions>\n"); //$NON-NLS-1$
74
return buf.toString();
75     }
76 }
77
Popular Tags