Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 483ad2cd26770984b536cc5a0a69158f94f6405e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.webapp.servlet;

import java.io.IOException;
import java.util.Map;
import java.util.WeakHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.help.IndexContribution;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.dynamic.NodeWriter;
import org.eclipse.help.internal.webapp.data.UrlUtil;

/*
 * Sends all available keyword index data in XML form. The data is sent as one
 * large index contribution that includes all merged contributions from the
 * system.
 * 
 * This is called on infocenters by client workbenches configured for remote
 * help in order to gather all the index keywords and assemble them into a
 * complete index.
 */
public class IndexServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private Map responseByLocale;
	private NodeWriter writer;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String locale = UrlUtil.getLocale(req, resp);
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
		
		if (responseByLocale == null) {
			responseByLocale = new WeakHashMap();
		}
		String response = (String)responseByLocale.get(locale);
		if (response == null) {
			IndexContribution[] contributions = HelpPlugin.getIndexManager().getIndexContributions(locale);
			response = serialize(contributions, locale);
			responseByLocale.put(locale, response);
		}
		resp.getWriter().write(response);
	}
		
	public String serialize(IndexContribution[] contributions, String locale) {
		StringBuffer buf = new StringBuffer();
		buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
		buf.append("<indexContributions>\n"); //$NON-NLS-1$
		for (int i=0;i<contributions.length;++i) {
			if (writer == null) {
				writer = new NodeWriter();
			}
			writer.write(contributions[i], buf, true, "   ", true); //$NON-NLS-1$
		}
		buf.append("</indexContributions>\n"); //$NON-NLS-1$
		return buf.toString();
	}
}

Back to the top