KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Writer JavaDoc;
15
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.http.HttpServlet JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.eclipse.help.IContext;
22 import org.eclipse.help.IHelpResource;
23 import org.eclipse.help.internal.HelpPlugin;
24 import org.eclipse.help.internal.Topic;
25 import org.eclipse.help.internal.context.Context;
26 import org.eclipse.help.internal.webapp.data.UrlUtil;
27
28 /*
29  * Returns a context help entry with the id specified in the id parameter.
30  *
31  * This is called on infocenters by client workbenches configured for remote
32  * help in order to retrieve context help stored on the remote help server.
33  */

34 public class ContextServlet extends HttpServlet JavaDoc {
35     
36     private static final long serialVersionUID = 1L;
37     private static final String JavaDoc PARAMETER_ID = "id"; //$NON-NLS-1$
38

39     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
40             throws ServletException JavaDoc, IOException JavaDoc {
41         String JavaDoc locale = UrlUtil.getLocale(req, resp);
42         req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
43
resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
44
String JavaDoc id = req.getParameter(PARAMETER_ID);
45         if (id != null) {
46             IContext context = HelpPlugin.getContextManager().getContext(id, locale);
47             if (context != null) {
48                 serialize(context, resp.getWriter());
49             }
50             else {
51                 resp.sendError(404);
52             }
53         }
54         else {
55             resp.sendError(400); // bad request; missing parameter
56
}
57     }
58     
59     private void serialize(IContext context, Writer JavaDoc out) throws IOException JavaDoc {
60         out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
61
out.write('<' + Context.NAME + ">\n"); //$NON-NLS-1$
62
out.write(" <description>" + context.getText() + "</description>\n"); //$NON-NLS-1$ //$NON-NLS-2$
63
IHelpResource[] topics = context.getRelatedTopics();
64         for (int i=0;i<topics.length;++i) {
65             out.write(" <" + Topic.NAME); //$NON-NLS-1$
66
if (topics[i].getLabel() != null) {
67                 out.write("\n " + Topic.ATTRIBUTE_LABEL + "=\"" + topics[i].getLabel() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
68
}
69             if (topics[i].getHref() != null) {
70                 out.write("\n " + Topic.ATTRIBUTE_HREF + "=\"" + topics[i].getHref() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
71
}
72             out.write(">\n </topic>"); //$NON-NLS-1$
73
}
74         out.write("</" + Context.NAME + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
75
}
76 }
77
Popular Tags