Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Goldthorpe2011-07-25 21:15:46 +0000
committerChris Goldthorpe2011-07-25 21:15:46 +0000
commit06e297c276381ecdd4f4f93ec3582bd0376a9c4d (patch)
treeeb4f3e8e38e7d89c4fd9953517ced96db918132d
parent8a68868aed61ffd9f1e09ce8b0dc582a5a26db9c (diff)
downloadeclipse.platform.ua-20110725.tar.gz
eclipse.platform.ua-20110725.tar.xz
eclipse.platform.ua-20110725.zip
Bug 347117 - [Help] Enhancement in Web API for About and Nav servicesv20110725
-rw-r--r--org.eclipse.help.webapp/plugin.xml4
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/AboutParser.java121
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/NavParser.java80
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ParseElement.java57
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ResultParser.java18
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/AboutService.java203
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/NavService.java130
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/AboutServlet.java30
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/NavServlet.java4
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PreferenceWriter.java49
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/JSonHelper.java4
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/Utils.java6
-rw-r--r--org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/XMLHelper.java3
13 files changed, 662 insertions, 47 deletions
diff --git a/org.eclipse.help.webapp/plugin.xml b/org.eclipse.help.webapp/plugin.xml
index 187f3c94e..c511da422 100644
--- a/org.eclipse.help.webapp/plugin.xml
+++ b/org.eclipse.help.webapp/plugin.xml
@@ -238,6 +238,10 @@
class="org.eclipse.help.internal.webapp.service.ExtensionService">
</servlet>
<servlet
+ alias="/service/about"
+ class="org.eclipse.help.internal.webapp.service.AboutService">
+ </servlet>
+ <servlet
alias="/service/about.html"
class="org.eclipse.help.internal.webapp.service.AboutService">
</servlet>
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/AboutParser.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/AboutParser.java
new file mode 100644
index 000000000..766b422ee
--- /dev/null
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/AboutParser.java
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.parser;
+
+import java.util.Properties;
+
+import org.eclipse.help.internal.webapp.utils.JSonHelper;
+import org.eclipse.help.internal.webapp.utils.Utils;
+import org.eclipse.help.internal.webapp.utils.XMLHelper;
+import org.xml.sax.Attributes;
+
+public class AboutParser extends ResultParser {
+
+ private ParseElement element = null;
+ private String currentTag;
+ private long service;
+
+ public AboutParser(long service) {
+ super(JSonHelper.TITLE);
+ this.service = service;
+ if (this.service == Utils.ABOUT_PLUGIN)
+ this.setIdentifier(JSonHelper.PLUGIN_ID);
+ }
+
+ public void startElement(String uri, String lname, String name, Attributes attrs) {
+
+ currentTag = name;
+
+ Properties properties = new Properties();
+ properties.put(JSonHelper.PROPERTY_NAME, name);
+ if (!((service == Utils.PREFERENCE
+ && (name.equalsIgnoreCase(XMLHelper.ELEMENT_PREFERENCES)
+ || name.equalsIgnoreCase(XMLHelper.ELEMENT_PLUGIN)))
+ || service == Utils.ABOUT_PLUGIN))
+ properties.put(JSonHelper.PROPERTY_VALUE, ""); //$NON-NLS-1$
+
+ for (int i = 0; i < attrs.getLength(); i++) {
+ String qname = attrs.getQName(i);
+ String val = attrs.getValue(i);
+ properties.put(qname, val);
+ }
+
+ ParseElement elem = new ParseElement(properties, element);
+ if (element != null)
+ element.addChild(elem);
+ else
+ items.add(elem);
+
+ element = elem;
+ }
+
+ public void characters(char[] ch, int start, int length) {
+ if ((service == Utils.PREFERENCE
+ && (currentTag.equalsIgnoreCase(XMLHelper.ELEMENT_PREFERENCES)
+ || currentTag.equalsIgnoreCase(XMLHelper.ELEMENT_PLUGIN)))
+ || service == Utils.ABOUT_PLUGIN)
+ return;
+
+ if (element != null && !currentTag.equals("")) { //$NON-NLS-1$
+
+ Properties properties = element.getProps();
+ if (properties != null) {
+
+ String content = new String(ch, start, length);
+
+ String existing = (String) properties.get(currentTag);
+ if (existing == null)
+ existing = ""; //$NON-NLS-1$
+
+ content = content.replaceAll("[\\n\\t]", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$
+
+ properties.put(JSonHelper.PROPERTY_VALUE, existing + content);
+ element.updateParseElement(properties);
+ }
+ }
+ }
+
+ public void endElement(String uri, String lname, String name) {
+
+ if (element != null) {
+ element = element.getParent();
+ }
+ currentTag = ""; //$NON-NLS-1$
+ }
+
+ public String toJSON() {
+
+ StringBuffer buf = new StringBuffer();
+
+ buf.append(JSonHelper.BEGIN_BRACE);
+ buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+
+ buf.append(JSonHelper.IDENTIFIER);
+ buf.append(JSonHelper.COLON);
+ buf.append(JSonHelper.getQuotes(id));
+ buf.append(JSonHelper.COMMA);
+
+ buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+ buf.append(JSonHelper.LABEL);
+ buf.append(JSonHelper.COLON);
+ buf.append(JSonHelper.getQuotes(label));
+ buf.append(JSonHelper.COMMA);
+
+ ParseElement elem = (ParseElement) items.get(0);
+ if (elem != null)
+ buf.append(elem.toJSON());
+
+ buf.append(JSonHelper.END_BRACE);
+
+ return buf.toString();
+ }
+
+}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/NavParser.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/NavParser.java
new file mode 100644
index 000000000..4dc2e38cb
--- /dev/null
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/NavParser.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.parser;
+
+import java.util.Properties;
+
+import org.eclipse.help.internal.webapp.utils.JSonHelper;
+import org.xml.sax.Attributes;
+
+public class NavParser extends ResultParser {
+
+ private ParseElement element = null;
+
+ public NavParser() {
+ super(JSonHelper.TITLE);
+ }
+
+ public void startElement(String uri, String lname, String name, Attributes attrs) {
+
+ Properties properties = new Properties();
+ properties.put(JSonHelper.PROPERTY_NAME, name);
+
+ for (int i = 0; i < attrs.getLength(); i++) {
+ String qname = attrs.getQName(i);
+ String val = attrs.getValue(i);
+ properties.put(qname, val);
+ }
+
+ ParseElement elem = new ParseElement(properties, element);
+ if (element != null)
+ element.addChild(elem);
+ else
+ items.add(elem);
+
+ element = elem;
+ }
+
+ public void endElement(String uri, String lname, String name) {
+
+ if (element != null) {
+ element = element.getParent();
+ }
+ }
+
+ public String toJSON() {
+
+ StringBuffer buf = new StringBuffer();
+
+ buf.append(JSonHelper.BEGIN_BRACE);
+ buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+
+ buf.append(JSonHelper.IDENTIFIER);
+ buf.append(JSonHelper.COLON);
+ buf.append(JSonHelper.getQuotes(id));
+ buf.append(JSonHelper.COMMA);
+
+ buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+ buf.append(JSonHelper.LABEL);
+ buf.append(JSonHelper.COLON);
+ buf.append(JSonHelper.getQuotes(label));
+ buf.append(JSonHelper.COMMA);
+
+ ParseElement elem = (ParseElement) items.get(0);
+ if (elem != null)
+ buf.append(elem.toJSON());
+
+ buf.append(JSonHelper.END_BRACE);
+
+ return buf.toString();
+ }
+
+}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ParseElement.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ParseElement.java
index 152b714a4..e22f72a60 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ParseElement.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ParseElement.java
@@ -127,4 +127,61 @@ public class ParseElement {
return buff.toString();
}
+
+ public String toJSON() {
+
+ StringBuffer buff = new StringBuffer();
+
+ if (props != null) {
+ Enumeration enumObj = props.keys();
+ while (enumObj.hasMoreElements()) {
+
+ String key = (String) enumObj.nextElement();
+ String val = props.getProperty(key);
+
+ buff.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+ buff.append(key);
+ buff.append(JSonHelper.COLON);
+ try {
+ val = URLEncoder.encode(val, "UTF-8"); //$NON-NLS-1$
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ buff.append(JSonHelper.getQuotes(val));
+ buff.append(JSonHelper.COMMA);
+ }
+ }
+
+ if (children.size() <= 0) {
+ int len = buff.length();
+ char ch = buff.charAt(len - 1);
+ if (ch == ',') {
+ buff.deleteCharAt(len - 1);
+ buff.append(JSonHelper.NEWLINE);
+ }
+
+ } else {
+
+ buff.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+ buff.append(JSonHelper.ITEMS);
+ buff.append(JSonHelper.COLON);
+ buff.append(JSonHelper.BEGIN_BRACKET);
+
+ for (int i = 0; i < children.size(); i++) {
+
+ if (i > 0)
+ buff.append(JSonHelper.COMMA);
+
+ ParseElement child = (ParseElement) children.get(i);
+ buff.append(child.toJSON(1));
+ }
+
+ buff.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
+
+ buff.append(JSonHelper.END_BRACKET);
+ buff.append(JSonHelper.NEWLINE);
+ }
+
+ return buff.toString();
+ }
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ResultParser.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ResultParser.java
index 08c7e866e..c078a14b6 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ResultParser.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/parser/ResultParser.java
@@ -25,17 +25,23 @@ import org.xml.sax.helpers.DefaultHandler;
public class ResultParser extends DefaultHandler {
- private String label = ""; //$NON-NLS-1$
+ protected String id;
+ protected String label;
protected ArrayList items = new ArrayList(); //parser populates the items arrayList withe parsed data.
public ResultParser(String label) {
+ this(label, JSonHelper.ID);
+ }
+
+ public ResultParser(String label, String id) {
this.label = label;
+ this.id = id;
}
- public void parse(URL tocURL)
+ public void parse(URL url)
throws ParserConfigurationException, SAXException, IOException
{
- parse(tocURL.openStream());
+ parse(url.openStream());
}
public void parse(InputStream in)
@@ -49,6 +55,10 @@ public class ResultParser extends DefaultHandler {
this.label = label;
}
+ public void setIdentifier(String id) {
+ this.id = id;
+ }
+
public ArrayList getItems()
{
return items;
@@ -68,7 +78,7 @@ public class ResultParser extends DefaultHandler {
buf.append(JSonHelper.IDENTIFIER);
buf.append(JSonHelper.COLON);
- buf.append(JSonHelper.getQuotes(JSonHelper.ID));
+ buf.append(JSonHelper.getQuotes(id));
buf.append(JSonHelper.COMMA);
buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/AboutService.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/AboutService.java
index 7365b1c80..45a302358 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/AboutService.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/AboutService.java
@@ -10,12 +10,33 @@
*******************************************************************************/
package org.eclipse.help.internal.webapp.service;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.help.internal.webapp.HelpWebappPlugin;
+import org.eclipse.help.internal.webapp.WebappResources;
+import org.eclipse.help.internal.webapp.data.UrlUtil;
+import org.eclipse.help.internal.webapp.parser.AboutParser;
import org.eclipse.help.internal.webapp.servlet.AboutServlet;
+import org.eclipse.help.internal.webapp.servlet.PreferenceWriter;
+import org.eclipse.help.internal.webapp.servlet.XMLGenerator;
+import org.eclipse.help.internal.webapp.utils.Utils;
+import org.osgi.framework.Bundle;
/**
- * Generates an html page having informations about either <code>User-Agent</code>,
- * Help system <code>preferences</code> or the available plug-ins in Help system
- * like Provider, Plugin name, Version and PluginId.
+ * Generates either xml, json or html page having informations about either
+ * <code>User-Agent</code>, Help system <code>preferences</code> or the available
+ * plug-ins in Help system like Provider, Plugin name, Version and PluginId.
*
* <p>Extends the {@link org.eclipse.help.internal.webapp.servlet.AboutServlet}
* servlet.
@@ -25,12 +46,17 @@ import org.eclipse.help.internal.webapp.servlet.AboutServlet;
* <code>preferences</code> to view the Help system preferences.
* Do not specify any value to show the available plugins in
* Help web application.
- * @param sortColumn - (Optional) specifying the column number over
- * which displayed output needs to be sorted. Applicable only if
- * <code>show</code> parameter is <code>null</code>.
+ * @param sortColumn - (Optional) specifying the column number of the available
+ * plug-ins in Help system over which displayed output needs
+ * to be sorted. Applicable only if <code>show</code> parameter
+ * is <code>null</code>.
+ * @param returnType - (Optional) specifies the return type of the servlet.
+ * Accepts either <code>xml</code> (default) or <code>html</code>
+ * or <code>json</code>
*
- * @return Html page having informations about either <code>User-Agent</code>,
- * <code>preferences</code> or the available plug-ins.
+ * @return Informations about either <code>User-Agent</code>, <code>preferences</code>
+ * or the available plug-ins, either as <code>xml</code> (default) or
+ * <code>html</code> or <code>json</code>
*
* @version $Version$
*
@@ -38,5 +64,166 @@ import org.eclipse.help.internal.webapp.servlet.AboutServlet;
public class AboutService extends AboutServlet {
private static final long serialVersionUID = 1L;
+
+ private long service;
+
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+
+ req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
+
+ String returnType = req.getParameter(Utils.RETURN_TYPE);
+ boolean boolIsHTML = (returnType != null
+ && returnType.equalsIgnoreCase(Utils.HTML));
+ // If HTML output is required, call AboutServlet class
+ if (boolIsHTML) {
+ super.doGet(req, resp);
+ return;
+ }
+
+ // Set standard HTTP/1.1 no-cache headers.
+ resp.setHeader("Cache-Control", //$NON-NLS-1$
+ "no-store, no-cache, must-revalidate"); //$NON-NLS-1$
+ resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
+ locale = UrlUtil.getLocaleObj(req, resp);
+
+ // create XML response
+ String response = processRequest(req, resp);
+
+ boolean boolIsJSON = (returnType != null
+ && returnType.equalsIgnoreCase(Utils.JSON));
+
+ // If JSON output is required
+ if (boolIsJSON) {
+ resp.setContentType("text/plain"); //$NON-NLS-1$
+ response = getJSONResponse(response);
+ }
+
+ resp.getWriter().write(response);
+ }
+
+ protected String getJSONResponse(String response)
+ throws IOException {
+ AboutParser aboutParser = new AboutParser(service);
+ InputStream is = null;
+ try {
+ if (response != null) {
+ is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
+ aboutParser.parse(is);
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ if (is != null)
+ is.close();
+
+ // Call after the catch.
+ // An empty JSON is created if any Exception is thrown
+ // Else returns the complete JSON
+ return aboutParser.toJSON();
+ }
+
+ private String processRequest(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ StringBuffer buf = new StringBuffer();
+ buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
+
+ String showParam = req.getParameter("show"); //$NON-NLS-1$
+ if ("agent".equalsIgnoreCase(showParam)) { //$NON-NLS-1$
+ getAgent(buf, req, resp);
+ } else if ("preferences".equalsIgnoreCase(showParam)) { //$NON-NLS-1$
+ getPreferences(buf, resp);
+ } else {
+ getAboutPlugins(buf, req, resp);
+ }
+
+ return buf.toString();
+ }
+
+ private void getAgent(StringBuffer buf, HttpServletRequest req, HttpServletResponse resp)
+ throws IOException {
+ service = Utils.AGENT;
+
+ String title = WebappResources.getString("userAgent", locale); //$NON-NLS-1$
+ String agent = req.getHeader("User-Agent"); //$NON-NLS-1$
+ buf.append("<userAgent\n title=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(title));
+ buf.append("\">"); //$NON-NLS-1$
+ buf.append(agent);
+ buf.append("</userAgent>"); //$NON-NLS-1$
+ }
+
+ private void getPreferences(StringBuffer buf, HttpServletResponse resp)
+ throws IOException {
+ service = Utils.PREFERENCE;
+
+ String title = WebappResources.getString("preferences", locale); //$NON-NLS-1$
+ buf.append("<preferences\n title=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(title));
+ buf.append("\">"); //$NON-NLS-1$
+ PreferenceWriter writer = new PreferenceWriter(buf, locale, true);
+ writer.writePreferences();
+ buf.append("\n</preferences>"); //$NON-NLS-1$
+ }
+
+ private void getAboutPlugins(StringBuffer buf, HttpServletRequest req, HttpServletResponse resp)
+ throws IOException {
+ service = Utils.ABOUT_PLUGIN;
+
+ String sortParam = req.getParameter("sortColumn"); //$NON-NLS-1$
+ int sortColumn = 3;
+ if (sortParam != null) {
+ try {
+ sortColumn = Integer.parseInt(sortParam);
+ } catch (NumberFormatException e) {}
+ }
+
+ String title = WebappResources.getString("aboutPlugins", locale); //$NON-NLS-1$
+ buf.append("<aboutPlugins\n title=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(title));
+ buf.append("\""); //$NON-NLS-1$
+
+ List plugins = new ArrayList();
+
+ Bundle[] bundles = HelpWebappPlugin.getContext().getBundles();
+ for (int k = 0; k < bundles.length; k++) {
+ plugins.add(pluginDetails(bundles[k]));
+ }
+
+ Comparator pluginComparator = new PluginComparator(sortColumn);
+ Collections.sort(plugins, pluginComparator );
+
+ String[] headerColumns = new String[]{
+ "provider", //$NON-NLS-1$
+ "pluginName", //$NON-NLS-1$
+ "version", //$NON-NLS-1$
+ "pluginId" //$NON-NLS-1$
+ };
+
+ for (int i = 0; i < headerColumns.length; i++) {
+ buf.append("\n "); //$NON-NLS-1$
+ buf.append(headerColumns[i]);
+ buf.append("=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(WebappResources.getString(headerColumns[i], locale)));
+ buf.append("\""); //$NON-NLS-1$
+ }
+ buf.append(">"); //$NON-NLS-1$
+
+ for (Iterator iter = plugins.iterator(); iter.hasNext();) {
+ PluginDetails details = (PluginDetails) iter.next();
+ buf.append("\n <plugin"); //$NON-NLS-1$
+ for (int i = 0; i < headerColumns.length; i++) {
+ buf.append("\n "); //$NON-NLS-1$
+ buf.append(headerColumns[i]);
+ buf.append("=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(details.columns[i]));
+ buf.append("\""); //$NON-NLS-1$
+ }
+ buf.append(">\n </plugin>"); //$NON-NLS-1$
+ }
+ buf.append("\n</aboutPlugins>"); //$NON-NLS-1$
+ }
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/NavService.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/NavService.java
index a6ac5eb46..987b2392f 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/NavService.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/service/NavService.java
@@ -10,23 +10,33 @@
*******************************************************************************/
package org.eclipse.help.internal.webapp.service;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
+import java.util.Locale;
import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.eclipse.help.ITopic;
+import org.eclipse.help.base.AbstractHelpScope;
+import org.eclipse.help.internal.base.scope.ScopeUtils;
+import org.eclipse.help.internal.webapp.data.RequestScope;
+import org.eclipse.help.internal.webapp.data.UrlUtil;
+import org.eclipse.help.internal.webapp.parser.NavParser;
+import org.eclipse.help.internal.webapp.servlet.NavServlet;
+import org.eclipse.help.internal.webapp.servlet.XMLGenerator;
import org.eclipse.help.internal.webapp.utils.Utils;
/**
- * Generates navigation HTML page where topic is not present in the table
- * of contents for the selected toc passed as request path info. Displays
- * links to the direct child topics.
+ * Generates either xml, json or html page having navigation informations where topic
+ * is not present in the table of contents for the selected toc passed as request
+ * path info. Displays links to the direct child topics.
*
* <p>This servlet is called on infocenters by client workbenches
* configured for remote help in order to generate the navigation pages.
@@ -34,13 +44,18 @@ import org.eclipse.help.internal.webapp.utils.Utils;
* <p>Passes the request to {@link org.eclipse.help.internal.webapp.servlet.NavServlet}
* servlet.
*
- * @return An html page having the links to the direct child topics for
- * the selected toc
+ * @param returnType - (Optional) specifies the return type of the servlet.
+ * Accepts either <code>xml</code> (default) or <code>html</code>
+ * or <code>json</code>
+ *
+ * @return A navigation information having the links to the direct child topics for
+ * the selected toc, either as <code>xml</code> (default) or
+ * <code>html</code> or <code>json</code>
*
* @version $Version$
*
**/
-public class NavService extends HttpServlet {
+public class NavService extends NavServlet {
private static final long serialVersionUID = 1L;
@@ -48,6 +63,39 @@ public class NavService extends HttpServlet {
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
+
+ String returnType = req.getParameter(Utils.RETURN_TYPE);
+ boolean boolIsHTML = (returnType != null
+ && returnType.equalsIgnoreCase(Utils.HTML));
+ // If HTML output is required, call AboutServlet class
+ if (boolIsHTML) {
+ processHTMLOutputRequest(req, resp);
+ return;
+ }
+
+ // Set standard HTTP/1.1 no-cache headers.
+ resp.setHeader("Cache-Control", //$NON-NLS-1$
+ "no-store, no-cache, must-revalidate"); //$NON-NLS-1$
+ resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
+
+ // create XML response
+ String response = processRequest(req, resp);
+
+ boolean boolIsJSON = (returnType != null
+ && returnType.equalsIgnoreCase(Utils.JSON));
+
+ // If JSON output is required
+ if (boolIsJSON) {
+ resp.setContentType("text/plain"); //$NON-NLS-1$
+ response = getJSONResponse(response);
+ }
+
+ resp.getWriter().write(response);
+ }
+
+ private void processHTMLOutputRequest(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+
resp.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
String baseURL = req.getRequestURL().toString();
@@ -65,4 +113,72 @@ public class NavService extends HttpServlet {
writer.close();
}
+ protected String getJSONResponse(String response)
+ throws IOException {
+ NavParser navParser = new NavParser();
+ InputStream is = null;
+ try {
+ if (response != null) {
+ is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
+ navParser.parse(is);
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ if (is != null)
+ is.close();
+
+ // Call after the catch.
+ // An empty JSON is created if any Exception is thrown
+ // Else returns the complete JSON
+ return navParser.toJSON();
+ }
+
+ private String processRequest(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ Locale locale = getLocale(req, resp);
+
+ String path = req.getPathInfo().substring(1);
+ int index = path.indexOf("service/nav/"); //$NON-NLS-1$
+ if (index > -1)
+ path = path.substring(index+12);
+ ITopic topic = getTopic(path, locale);
+
+ AbstractHelpScope scope = RequestScope.getScope(req, resp, false);
+ return "" + writeContent(topic, path, locale, scope); //$NON-NLS-1$
+ }
+
+ private String writeContent(ITopic topic, String path, Locale locale,
+ AbstractHelpScope scope) {
+ StringBuffer buff = new StringBuffer();
+ buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
+
+ buff.append("<nav\n title=\""); //$NON-NLS-1$
+ buff.append(XMLGenerator.xmlEscape(topic.getLabel()));
+ buff.append("\">"); //$NON-NLS-1$
+
+ ITopic[] subtopics = topic.getSubtopics();
+ for (int i=0;i<subtopics.length;++i) {
+ if (ScopeUtils.showInTree(subtopics[i], scope)) {
+ buff.append("\n <topic\n href=\""); //$NON-NLS-1$
+ String href = subtopics[i].getHref();
+ if (href == null) {
+ href = path + '_' + i;
+ }
+ else {
+ href = XMLGenerator.xmlEscape(UrlUtil.getHelpURL(href));
+ }
+ buff.append(href);
+ buff.append("\"\n title=\""); //$NON-NLS-1$
+ buff.append(XMLGenerator.xmlEscape(subtopics[i].getLabel()));
+ buff.append("\">\n </topic>"); //$NON-NLS-1$
+ }
+ }
+ buff.append("\n</nav>"); //$NON-NLS-1$
+
+ return buff.toString();
+ }
+
}
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/AboutServlet.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/AboutServlet.java
index 2d2ae2073..40dc78956 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/AboutServlet.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/AboutServlet.java
@@ -35,12 +35,14 @@ import com.ibm.icu.text.Collator;
public class AboutServlet extends HttpServlet {
- private static final int NUMBER_OF_COLUMNS = 4;
+ protected static final int NUMBER_OF_COLUMNS = 4;
- private class PluginDetails {
- private String[] columns = new String[4];
+ protected Locale locale;
+
+ protected class PluginDetails {
+ public String[] columns = new String[NUMBER_OF_COLUMNS];
- PluginDetails(String[] columns) {
+ public PluginDetails(String[] columns) {
this.columns = columns;
for (int i = 0 ; i < NUMBER_OF_COLUMNS; i++) {
if (columns[i] == null) {
@@ -50,7 +52,7 @@ public class AboutServlet extends HttpServlet {
}
}
- private class PluginComparator implements Comparator {
+ protected class PluginComparator implements Comparator {
public PluginComparator(int column) {
this.column = column;
@@ -80,7 +82,7 @@ public class AboutServlet extends HttpServlet {
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
resp.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
- Locale locale = req.getLocale();
+ locale = UrlUtil.getLocaleObj(req, resp);
StringBuffer buf = new StringBuffer();
buf.append(XHTML_1);
String showParam = req.getParameter("show"); //$NON-NLS-1$
@@ -89,7 +91,7 @@ public class AboutServlet extends HttpServlet {
return;
}
if ("preferences".equalsIgnoreCase(showParam)) { //$NON-NLS-1$
- getPreferences(req, resp);
+ getPreferences(resp);
return;
}
String sortParam = req.getParameter("sortColumn"); //$NON-NLS-1$
@@ -132,16 +134,16 @@ public class AboutServlet extends HttpServlet {
resp.getWriter().write(response);
}
- private void getPreferences(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ private void getPreferences(HttpServletResponse resp) throws IOException {
StringBuffer buf = new StringBuffer();
buf.append(XHTML_1);
- String title = WebappResources.getString("preferences", req.getLocale()); //$NON-NLS-1$
+ String title = WebappResources.getString("preferences", locale); //$NON-NLS-1$
buf.append(UrlUtil.htmlEncode(title));
buf.append(XHTML_2);
buf.append("<h1>"); //$NON-NLS-1$
buf.append(title);
buf.append("</h1>"); //$NON-NLS-1$
- PreferenceWriter writer = new PreferenceWriter(buf, req.getLocale());
+ PreferenceWriter writer = new PreferenceWriter(buf, locale);
writer.writePreferences();
buf.append(XHTML_3);
String response = buf.toString();
@@ -151,7 +153,7 @@ public class AboutServlet extends HttpServlet {
private void getAgent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
StringBuffer buf = new StringBuffer();
buf.append(XHTML_1);
- String title = WebappResources.getString("userAgent", req.getLocale()); //$NON-NLS-1$
+ String title = WebappResources.getString("userAgent", locale); //$NON-NLS-1$
buf.append(UrlUtil.htmlEncode(title));
buf.append(XHTML_2);
buf.append("<h1>"); //$NON-NLS-1$
@@ -166,7 +168,7 @@ public class AboutServlet extends HttpServlet {
private String headerRowFor(PluginDetails details) {
String row = "<tr>\n"; //$NON-NLS-1$
- for (int i = 0; i < 4; i++) {
+ for (int i = 0; i < NUMBER_OF_COLUMNS; i++) {
row += ("<td><a href = \"about.html?sortColumn="); //$NON-NLS-1$
row += i;
row += "\">"; //$NON-NLS-1$
@@ -179,7 +181,7 @@ public class AboutServlet extends HttpServlet {
private String tableRowFor(PluginDetails details) {
String row = "<tr>\n"; //$NON-NLS-1$
- for (int i = 0; i < 4; i++) {
+ for (int i = 0; i < NUMBER_OF_COLUMNS; i++) {
row += ("<td>"); //$NON-NLS-1$
row += UrlUtil.htmlEncode(details.columns[i]);
row += "</td>\n"; //$NON-NLS-1$
@@ -188,7 +190,7 @@ public class AboutServlet extends HttpServlet {
return row;
}
- private Object pluginDetails(Bundle bundle) {
+ protected Object pluginDetails(Bundle bundle) {
String[] values = new String[] {
getResourceString(bundle, Constants.BUNDLE_VENDOR),
getResourceString(bundle, Constants.BUNDLE_NAME),
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/NavServlet.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/NavServlet.java
index fc3d1cc9c..b45853898 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/NavServlet.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/NavServlet.java
@@ -68,7 +68,7 @@ public class NavServlet extends HttpServlet {
writer.close();
}
- private Locale getLocale (HttpServletRequest req, HttpServletResponse resp) {
+ protected Locale getLocale (HttpServletRequest req, HttpServletResponse resp) {
Locale locale;
String nl = UrlUtil.getLocale(req, resp);
// break the string into tokens to get the Locale object
@@ -85,7 +85,7 @@ public class NavServlet extends HttpServlet {
return locale;
}
- private ITopic getTopic(String topicPath, Locale locale) {
+ protected ITopic getTopic(String topicPath, Locale locale) {
StringTokenizer tok = new StringTokenizer(topicPath, "_"); //$NON-NLS-1$
int index = Integer.parseInt(tok.nextToken());
ITopic topic = HelpPlugin.getTocManager().getTocs(locale.toString())[index].getTopic(null);
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PreferenceWriter.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PreferenceWriter.java
index 0cf320713..cfa9003bf 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PreferenceWriter.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/servlet/PreferenceWriter.java
@@ -31,10 +31,16 @@ import org.osgi.service.prefs.BackingStoreException;
public class PreferenceWriter {
private StringBuffer buf;
private Locale locale;
+ private boolean isXML;
public PreferenceWriter(StringBuffer buf, Locale locale) {
+ this(buf, locale, false);
+ }
+
+ public PreferenceWriter(StringBuffer buf, Locale locale, boolean isXML) {
this.buf = buf;
this.locale = locale;
+ this.isXML = isXML;
}
public void writePreferences() {
@@ -54,22 +60,43 @@ public class PreferenceWriter {
String[] allKeys = (String[]) keySet.toArray(new String[keySet.size()]);
if (allKeys.length > 0) {
Arrays.sort(allKeys);
- buf.append("<h3>"); //$NON-NLS-1$
- buf.append(plugin);
- buf.append("</h3>"); //$NON-NLS-1$
- buf.append("<table>"); //$NON-NLS-1$
+
+ if (!isXML) {
+ buf.append("\n<h3>"); //$NON-NLS-1$
+ buf.append(plugin);
+ buf.append("</h3>\n"); //$NON-NLS-1$
+ buf.append("<table>"); //$NON-NLS-1$
+ } else {
+ buf.append("\n <plugin\n title=\""); //$NON-NLS-1$
+ buf.append(XMLGenerator.xmlEscape(plugin));
+ buf.append("\">"); //$NON-NLS-1$
+ }
for (int i = 0; i < allKeys.length; i++) {
- buf.append("<tr>"); //$NON-NLS-1$
String key = allKeys[i];
- buf.append("<td>"); //$NON-NLS-1$
- buf.append(UrlUtil.htmlEncode(key));
- buf.append("</td><td>"); //$NON-NLS-1$
String value = Platform.getPreferencesService().getString
(plugin, key, "", null); //$NON-NLS-1$
- buf.append(UrlUtil.htmlEncode(value));
- buf.append("</td></tr>"); //$NON-NLS-1$
+ if (!isXML) {
+ buf.append("\n <tr>\n"); //$NON-NLS-1$
+ buf.append(" <td>"); //$NON-NLS-1$
+ buf.append(UrlUtil.htmlEncode(key));
+ buf.append("</td>\n <td>"); //$NON-NLS-1$
+ buf.append(UrlUtil.htmlEncode(value));
+ buf.append("</td>\n </tr>"); //$NON-NLS-1$
+ } else {
+ buf.append("\n <"); //$NON-NLS-1$
+ buf.append(key);
+ buf.append(">"); //$NON-NLS-1$
+ buf.append(value);
+ buf.append("</"); //$NON-NLS-1$
+ buf.append(key);
+ buf.append(">"); //$NON-NLS-1$
+ }
+ }
+ if (!isXML) {
+ buf.append("\n</table>"); //$NON-NLS-1$
+ } else {
+ buf.append("\n </plugin>"); //$NON-NLS-1$
}
- buf.append("</table>"); //$NON-NLS-1$
}
} catch (BackingStoreException e) {
buf.append(WebappResources.getString("badPreferences", locale)); //$NON-NLS-1$
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/JSonHelper.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/JSonHelper.java
index a59ff8bc9..fd829c450 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/JSonHelper.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/JSonHelper.java
@@ -32,6 +32,7 @@ public final class JSonHelper {
public static final String NAME = "name"; //$NON-NLS-1$
public static final String TITLE = "title"; //$NON-NLS-1$
public static final String ID = "id"; //$NON-NLS-1$
+ public static final String PLUGIN_ID = "pluginId"; //$NON-NLS-1$
public static final String HREF = "href"; //$NON-NLS-1$
public static final String TYPE = "type"; //$NON-NLS-1$
public static final String CHECKED = "checked"; //$NON-NLS-1$
@@ -45,7 +46,8 @@ public final class JSonHelper {
public static final String CATEGORY = "category"; //$NON-NLS-1$
public static final String DESCRIPTION = "description"; //$NON-NLS-1$
public static final String CATEGORY_HREF = CATEGORY+"_"+HREF; //$NON-NLS-1$
- public static final String PROPERTY_NAME = "propertyName"; //$NON-NLS-1$
+ public static final String PROPERTY_NAME = "tagName"; //$NON-NLS-1$
+ public static final String PROPERTY_VALUE = "value"; //$NON-NLS-1$
public static final String INDEX = "Index"; //$NON-NLS-1$
public static final String TOPIC = "Topic"; //$NON-NLS-1$
public static final String NUMERIC_PATH = "NumericPath"; //$NON-NLS-1$
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/Utils.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/Utils.java
index f07d767ed..01ccc51ef 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/Utils.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/Utils.java
@@ -30,6 +30,12 @@ public class Utils {
// returnType values: xml (default) | json
public static final String XML = "xml"; //$NON-NLS-1$
public static final String JSON = "json"; //$NON-NLS-1$
+ public static final String HTML = "html"; //$NON-NLS-1$
+
+ // Constants for About service
+ public static final long AGENT = 1L;
+ public static final long PREFERENCE = 2L;
+ public static final long ABOUT_PLUGIN = 3L;
public static String convertStreamToString(InputStream is)
throws IOException {
diff --git a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/XMLHelper.java b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/XMLHelper.java
index b8513a400..d50eb498d 100644
--- a/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/XMLHelper.java
+++ b/org.eclipse.help.webapp/src/org/eclipse/help/internal/webapp/utils/XMLHelper.java
@@ -34,6 +34,9 @@ public final class XMLHelper {
public static final String ELEMENT_SUMMARY = "summary"; //$NON-NLS-1$
public static final String ELEMENT_DESCRIPTION = "description"; //$NON-NLS-1$
public static final String ELEMENT_INDEX = "index"; //$NON-NLS-1$
+ public static final String ELEMENT_PREFERENCES = "preferences"; //$NON-NLS-1$
+ public static final String ELEMENT_PLUGIN = "plugin"; //$NON-NLS-1$
+ public static final String ELEMENT_ABOUT_PLUGINS = "aboutPlugins"; //$NON-NLS-1$
public static final String ELEMENT_INDEX_CONTRIBUTIONS = "indexContributions"; //$NON-NLS-1$
public static final String ELEMENT_TOC_CONTRIBUTIONS = "tocContributions"; //$NON-NLS-1$
public static final String ELEMENT_NUMERIC_PATH = "numeric_path"; //$NON-NLS-1$

Back to the top