Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 319907 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (+1 lines)
Lines 14-19 Link Here
14
 org.eclipse.core.expressions;bundle-version="3.4.200"
14
 org.eclipse.core.expressions;bundle-version="3.4.200"
15
Export-Package: org.eclipse.help.internal.webapp;x-friends:="org.eclipse.ua.tests",
15
Export-Package: org.eclipse.help.internal.webapp;x-friends:="org.eclipse.ua.tests",
16
 org.eclipse.help.internal.webapp.data;x-friends:="org.eclipse.ua.tests",
16
 org.eclipse.help.internal.webapp.data;x-friends:="org.eclipse.ua.tests",
17
 org.eclipse.help.internal.webapp.service;x-friends:="org.eclipse.ua.tests,org.eclipse.ua.tests.doc",
17
 org.eclipse.help.internal.webapp.servlet;x-friends:="org.eclipse.ua.tests,org.eclipse.ua.tests.doc",
18
 org.eclipse.help.internal.webapp.servlet;x-friends:="org.eclipse.ua.tests,org.eclipse.ua.tests.doc",
18
 org.eclipse.help.webapp
19
 org.eclipse.help.webapp
19
Bundle-RequiredExecutionEnvironment: J2SE-1.4
20
Bundle-RequiredExecutionEnvironment: J2SE-1.4
(-)plugin.xml (-1 / +82 lines)
Lines 5-11 Link Here
5
    are made available under the terms of the Eclipse Public License v1.0
5
    are made available under the terms of the Eclipse Public License v1.0
6
    which accompanies this distribution, and is available at
6
    which accompanies this distribution, and is available at
7
    http://www.eclipse.org/legal/epl-v10.html
7
    http://www.eclipse.org/legal/epl-v10.html
8
   
8
    
9
    Contributors:
9
    Contributors:
10
         IBM Corporation - initial API and implementation
10
         IBM Corporation - initial API and implementation
11
 -->
11
 -->
Lines 15-20 Link Here
15
   <extension-point id="view" name="View" schema="schema/view.exsd"/>
15
   <extension-point id="view" name="View" schema="schema/view.exsd"/>
16
   <extension-point id="frame" name="Webapp Frame" schema="schema/frame.exsd"/> 
16
   <extension-point id="frame" name="Webapp Frame" schema="schema/frame.exsd"/> 
17
   <extension-point id="toolbarButton" name="Toolbar Button" schema="schema/toolbarButton.exsd"/>
17
   <extension-point id="toolbarButton" name="Toolbar Button" schema="schema/toolbarButton.exsd"/>
18
   <extension-point id="secureServlet" name="Security Enhanced Servlet" schema="schema/secureServlet.exsd"/>
18
  <extension
19
  <extension
19
         point="org.eclipse.equinox.http.registry.httpcontexts">
20
         point="org.eclipse.equinox.http.registry.httpcontexts">
20
      <httpcontext
21
      <httpcontext
Lines 125-130 Link Here
125
      <serviceSelector
126
      <serviceSelector
126
            filter="(other.info=org.eclipse.help)">
127
            filter="(other.info=org.eclipse.help)">
127
      </serviceSelector>
128
      </serviceSelector>
129
      <servlet
130
            alias="/ss"
131
            class="org.eclipse.help.internal.webapp.servlet.SecureServlet"
132
            httpcontextId="help">
133
      </servlet>
128
   </extension>
134
   </extension>
129
   <extension
135
   <extension
130
         id="org.eclipse.help.webapp.remoteStatusProducer"
136
         id="org.eclipse.help.webapp.remoteStatusProducer"
Lines 134-137 Link Here
134
            producer="org.eclipse.help.internal.webapp.StatusProducer">
140
            producer="org.eclipse.help.internal.webapp.StatusProducer">
135
      </contentProducer>
141
      </contentProducer>
136
   </extension>
142
   </extension>
143
   <extension
144
         point="org.eclipse.help.webapp.secureServlet">
145
      <servlet
146
            alias="/service/control"
147
            class="org.eclipse.help.internal.webapp.service.ControlService">
148
      </servlet>
149
      <servlet
150
            alias="/service/content"
151
            class="org.eclipse.help.internal.webapp.service.ContentService">
152
      </servlet>
153
      <servlet
154
            alias="/service/topic"
155
            class="org.eclipse.help.internal.webapp.service.ContentService">
156
      </servlet>
157
      <servlet
158
            alias="/service/nftopic"
159
            class="org.eclipse.help.internal.webapp.service.ContentService">
160
      </servlet>
161
      <servlet
162
            alias="/service/ntopic"
163
            class="org.eclipse.help.internal.webapp.service.ContentService">
164
      </servlet>
165
      <servlet
166
            alias="/service/rtopic"
167
            class="org.eclipse.help.internal.webapp.service.ContentService">
168
      </servlet>
169
      <servlet
170
            alias="/service/nav"
171
            class="org.eclipse.help.internal.webapp.service.NavService">
172
      </servlet>
173
      <servlet
174
            alias="/service/livehelp"
175
            class="org.eclipse.help.internal.webapp.service.LiveHelpService">
176
      </servlet>
177
      <servlet
178
            alias="/service/toc"
179
            class="org.eclipse.help.internal.webapp.service.TocService">
180
      </servlet>
181
      <servlet
182
            alias="/service/tocfragment"
183
            class="org.eclipse.help.internal.webapp.service.TocFragmentService">
184
      </servlet>
185
      <servlet
186
            alias="/service/index"
187
            class="org.eclipse.help.internal.webapp.service.IndexService">
188
      </servlet>
189
      <servlet
190
            alias="/service/indexfragment"
191
            class="org.eclipse.help.internal.webapp.service.IndexFragmentService">
192
      </servlet>
193
      <servlet
194
            alias="/service/search"
195
            class="org.eclipse.help.internal.webapp.service.SearchService">
196
      </servlet>
197
      <servlet
198
            alias="/service/simplesearch"
199
            class="org.eclipse.help.internal.webapp.service.SimpleSearchService">
200
      </servlet>
201
      <servlet
202
            alias="/service/context"
203
            class="org.eclipse.help.internal.webapp.service.ContextService">
204
      </servlet>
205
      <servlet
206
            alias="/service/extension"
207
            class="org.eclipse.help.internal.webapp.service.ExtensionService">
208
      </servlet>
209
      <servlet
210
            alias="/service/about.html"
211
            class="org.eclipse.help.internal.webapp.service.AboutService">
212
      </servlet>
213
      <servlet
214
            alias="/service/state"
215
            class="org.eclipse.help.internal.webapp.service.SearchStateService">
216
      </servlet>
217
   </extension>
137
</plugin>
218
</plugin>
(-)schema/secureServlet.exsd (+109 lines)
Added Link Here
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!-- Schema file written by PDE -->
3
<schema targetNamespace="org.eclipse.help.webapp" xmlns="http://www.w3.org/2001/XMLSchema">
4
<annotation>
5
      <appInfo>
6
         <meta.schema plugin="org.eclipse.help.webapp" id="secureServlet" name="Security Enhanced Servlet"/>
7
      </appInfo>
8
      <documentation>
9
         Allows definition of Servlets and their aliases, with a focus on preventing known security vulernabilites.
10
      </documentation>
11
   </annotation>
12
13
   <element name="extension">
14
      <annotation>
15
         <appInfo>
16
            <meta.element />
17
         </appInfo>
18
      </annotation>
19
      <complexType>
20
         <sequence minOccurs="0" maxOccurs="unbounded">
21
            <element ref="servlet"/>
22
         </sequence>
23
         <attribute name="point" type="string" use="required">
24
            <annotation>
25
               <documentation>
26
                  
27
               </documentation>
28
            </annotation>
29
         </attribute>
30
         <attribute name="id" type="string">
31
            <annotation>
32
               <documentation>
33
                  
34
               </documentation>
35
            </annotation>
36
         </attribute>
37
         <attribute name="name" type="string">
38
            <annotation>
39
               <documentation>
40
                  
41
               </documentation>
42
               <appInfo>
43
                  <meta.attribute translatable="true"/>
44
               </appInfo>
45
            </annotation>
46
         </attribute>
47
      </complexType>
48
   </element>
49
50
   <element name="servlet">
51
      <complexType>
52
         <attribute name="class" type="string" use="required">
53
            <annotation>
54
               <documentation>
55
                  
56
               </documentation>
57
               <appInfo>
58
                  <meta.attribute kind="java" basedOn="javax.servlet.http.HttpServlet:"/>
59
               </appInfo>
60
            </annotation>
61
         </attribute>
62
         <attribute name="alias" type="string" use="required">
63
            <annotation>
64
               <documentation>
65
                  
66
               </documentation>
67
            </annotation>
68
         </attribute>
69
      </complexType>
70
   </element>
71
72
   <annotation>
73
      <appInfo>
74
         <meta.section type="since"/>
75
      </appInfo>
76
      <documentation>
77
         [Enter the first release in which this extension point appears.]
78
      </documentation>
79
   </annotation>
80
81
   <annotation>
82
      <appInfo>
83
         <meta.section type="examples"/>
84
      </appInfo>
85
      <documentation>
86
         [Enter extension point usage example here.]
87
      </documentation>
88
   </annotation>
89
90
   <annotation>
91
      <appInfo>
92
         <meta.section type="apiinfo"/>
93
      </appInfo>
94
      <documentation>
95
         [Enter API information here.]
96
      </documentation>
97
   </annotation>
98
99
   <annotation>
100
      <appInfo>
101
         <meta.section type="implementation"/>
102
      </appInfo>
103
      <documentation>
104
         [Enter information about supplied implementation of this extension point.]
105
      </documentation>
106
   </annotation>
107
108
109
</schema>
(-)src/org/eclipse/help/internal/webapp/WebappResources.properties (+7 lines)
Lines 211-213 Link Here
211
criterionClosed=Criterion closed
211
criterionClosed=Criterion closed
212
criterionOpen=Criterion open
212
criterionOpen=Criterion open
213
Criteria=Criteria
213
Criteria=Criteria
214
215
# Secure Servlet
216
cantCreateServlet=Unable to create servlet: {0}
217
218
# Service API
219
StatusServlet_PARAM_MISSING=The "{0}" parameter is missing from the request.
220
(-)src/org/eclipse/help/internal/webapp/data/SearchData.java (-4 / +9 lines)
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Sebastian Davids <sdavids@gmx.de> - fix for Bug 182466 
10
 *     Sebastian Davids <sdavids@gmx.de> - fix for Bug 182466 
11
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.data;
13
package org.eclipse.help.internal.webapp.data;
13
14
Lines 177-182 Link Here
177
	public int getResultsCount() {
178
	public int getResultsCount() {
178
		return hits.length;
179
		return hits.length;
179
	}
180
	}
181
	
182
	public SearchHit[] getResults() {
183
		return hits;
184
	}
180
185
181
	public String getSelectedTopicId() {
186
	public String getSelectedTopicId() {
182
		return selectedTopicId;
187
		return selectedTopicId;
Lines 554-565 Link Here
554
559
555
	public String getScopeActiveMessage() {
560
	public String getScopeActiveMessage() {
556
	    String scope = getScope(); 
561
	    String scope = getScope(); 
557
		return NLS.bind(ServletResources.getString("activeScope", request), scope); //$NON-NLS-1$ 
562
		return NLS.bind(ServletResources.getString("activeScope", request), scope); //$NON-NLS-1$
558
	}
563
	}
559
		
564
	
560
	public String getMatchesInScopeMessage() {
565
	public String getMatchesInScopeMessage() {
561
	    String scope = getScope(); 
566
	    String scope = getScope();
562
	    return NLS.bind(ServletResources.getString("matchesInScope", request), "" + getResultsCount(), scope); //$NON-NLS-1$ //$NON-NLS-2$		
567
	    return NLS.bind(ServletResources.getString("matchesInScope", request), "" + getResultsCount(), scope); //$NON-NLS-1$ //$NON-NLS-2$
563
	}
568
	}
564
569
565
	/*
570
	/*
(-)src/org/eclipse/help/internal/webapp/parser/ContextParser.java (+86 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.eclipse.help.internal.webapp.utils.XMLHelper;
18
import org.xml.sax.Attributes;
19
20
public class ContextParser extends ResultParser {
21
22
	private ParseElement element = null;
23
	private String currentTag;
24
25
	public ContextParser() {
26
		super(JSonHelper.TITLE);
27
	}
28
	
29
	public void startElement(String uri, String lname, String name, Attributes attrs) {
30
		
31
		currentTag = name;
32
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_DESCRIPTION))
33
			return;
34
		
35
		Properties properties = new Properties();
36
		properties.put(JSonHelper.PROPERTY_NAME, name);
37
		for (int i = 0; i < attrs.getLength(); i++) {
38
			String qname = attrs.getQName(i);
39
			String val = attrs.getValue(i);
40
			properties.put(qname, val);
41
		}
42
		
43
		ParseElement elem = new ParseElement(properties, element);
44
		if (element != null)
45
			element.addChild(elem);
46
		else
47
			items.add(elem);
48
		
49
		element = elem;
50
		
51
	}
52
	
53
	public void characters(char[] ch, int start, int length) {
54
		
55
		if (element != null 
56
				&& currentTag.equalsIgnoreCase(
57
						XMLHelper.ELEMENT_DESCRIPTION)) {
58
			
59
			Properties properties = element.getProps();
60
			if (properties != null) {
61
				
62
				String content = new String(ch, start, length);
63
				
64
				String existing = (String) properties.get(currentTag);
65
				if (existing == null)
66
					existing = ""; //$NON-NLS-1$
67
				
68
				content = content.replaceAll("[\\n\\t]", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$
69
				
70
				properties.put(currentTag, existing + content);
71
				element.updateParseElement(properties);
72
			}
73
		}
74
	}
75
	
76
	public void endElement(String uri, String lname, String name) {
77
		
78
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_DESCRIPTION))
79
			return;
80
		
81
		if (element != null) {
82
			element = element.getParent();
83
		}		
84
	}
85
86
}
(-)src/org/eclipse/help/internal/webapp/parser/ExtensionParser.java (+78 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.xml.sax.Attributes;
18
19
public class ExtensionParser extends ResultParser {
20
21
	private ParseElement element = null;
22
	private String currentTag;
23
24
	public ExtensionParser() {
25
		super(JSonHelper.TITLE);
26
	}
27
	
28
	public void startElement(String uri, String lname, String name, Attributes attrs) {
29
		
30
		currentTag = name;
31
		
32
		Properties properties = new Properties();
33
		properties.put(JSonHelper.PROPERTY_NAME, name);
34
		for (int i = 0; i < attrs.getLength(); i++) {
35
			String qname = attrs.getQName(i);
36
			String val = attrs.getValue(i);
37
			properties.put(qname, val);
38
		}
39
		
40
		ParseElement elem = new ParseElement(properties, element);
41
		if (element != null)
42
			element.addChild(elem);
43
		else
44
			items.add(elem);
45
		
46
		element = elem;
47
		
48
	}
49
	
50
	public void characters(char[] ch, int start, int length) {
51
		
52
		if (element != null) {
53
			
54
			Properties properties = element.getProps();
55
			if (properties != null) {
56
				
57
				String content = new String(ch, start, length);
58
				
59
				String existing = (String) properties.get(currentTag);
60
				if (existing == null)
61
					existing = ""; //$NON-NLS-1$
62
				
63
				content = content.replaceAll("[\\n\\t]", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$
64
				
65
				properties.put(currentTag, existing + content);
66
				element.updateParseElement(properties);
67
			}
68
		}
69
	}
70
	
71
	public void endElement(String uri, String lname, String name) {
72
		
73
		if (element != null) {
74
			element = element.getParent();
75
		}		
76
	}
77
78
}
(-)src/org/eclipse/help/internal/webapp/parser/IndexFragmentParser.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.eclipse.help.internal.webapp.utils.XMLHelper;
18
import org.xml.sax.Attributes;
19
20
public class IndexFragmentParser extends ResultParser{
21
22
	private ParseElement element = null;
23
	
24
	public IndexFragmentParser() {
25
		super(JSonHelper.TITLE);
26
	}
27
	
28
	public void startElement(String uri, String lname, String name, Attributes attrs) {
29
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE))
30
		{
31
			Properties properties = new Properties();
32
			properties.put(JSonHelper.PROPERTY_NAME, JSonHelper.INDEX);
33
			for (int i = 0; i < attrs.getLength(); i++) {
34
				String qname = attrs.getQName(i);
35
				String val = attrs.getValue(i);
36
				properties.put(qname, val);
37
			}
38
			
39
			ParseElement elem = new ParseElement(properties, element);
40
			if (element != null)
41
				element.addChild(elem);
42
			else
43
				items.add(elem);
44
			
45
			element = elem;
46
		}
47
		
48
	}
49
	
50
	public void endElement(String uri, String lname, String name) {
51
		if (element != null 
52
				&& name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE)) {
53
			element = element.getParent();
54
		}		
55
	}
56
}
(-)src/org/eclipse/help/internal/webapp/parser/IndexParser.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.eclipse.help.internal.webapp.utils.XMLHelper;
18
import org.xml.sax.Attributes;
19
20
public class IndexParser extends ResultParser {
21
22
	private ParseElement element = null;
23
24
	public IndexParser() {
25
		super(JSonHelper.TITLE);
26
	}
27
	
28
	public void startElement(String uri, String lname, String name, Attributes attrs) {
29
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_INDEX_CONTRIBUTIONS) 
30
				|| name.equalsIgnoreCase(XMLHelper.ELEMENT_INDEX))
31
			return;
32
		
33
		Properties properties = new Properties();
34
		properties.put(JSonHelper.PROPERTY_NAME, name);
35
		for (int i = 0; i < attrs.getLength(); i++) {
36
			String qname = attrs.getQName(i);
37
			String val = attrs.getValue(i);
38
			properties.put(qname, val);
39
		}
40
		
41
		ParseElement elem = new ParseElement(properties, element);
42
		if (element != null)
43
			element.addChild(elem);
44
		else
45
			items.add(elem);
46
		
47
		element = elem;
48
		
49
	}
50
	
51
	public void endElement(String uri, String lname, String name) {
52
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_INDEX_CONTRIBUTIONS) 
53
				|| name.equalsIgnoreCase(XMLHelper.ELEMENT_INDEX))
54
			return;
55
		
56
		if (element != null) {
57
			element = element.getParent();
58
		}		
59
	}
60
61
}
(-)src/org/eclipse/help/internal/webapp/parser/ParseElement.java (+124 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.ArrayList;
15
import java.util.Enumeration;
16
import java.util.Properties;
17
18
import org.eclipse.help.internal.webapp.utils.JSonHelper;
19
20
public class ParseElement {
21
22
	private Properties props;
23
	private ArrayList children = new ArrayList();
24
	private ParseElement parent;
25
26
	public ParseElement(Properties props, ParseElement parent) {
27
		this.props = props;
28
		this.parent = parent;
29
	}
30
31
	public ParseElement(Properties props) {
32
		this(props, null);
33
	}
34
	
35
	public void updateParseElement(Properties props) {
36
		this.props = props;
37
	}
38
	
39
	public Properties getProps() {
40
		return props;
41
	}
42
	
43
	public ParseElement getParent() {
44
		return parent;
45
	}
46
	
47
	public String getProperty(String key) {
48
		return (props != null) ? props.getProperty(key) : ""; //$NON-NLS-1$
49
	}
50
	
51
	public String toString() {
52
		return (props != null) ? props.toString() : ""; //$NON-NLS-1$
53
	}
54
	
55
	public void addChild(ParseElement elem) {
56
		children.add(elem);
57
	}
58
	
59
	public int getChildrenCount() {
60
		return children.size();
61
	}
62
	
63
	public String toJSON(int level) {
64
		
65
		StringBuffer buff = new StringBuffer();
66
		
67
		String space = JSonHelper.SPACE;
68
		for (int s = 0; s < level; s++) {
69
			space += JSonHelper.SPACE;
70
		}
71
		
72
		buff.append(JSonHelper.NEWLINE + space);
73
		buff.append(JSonHelper.BEGIN_BRACE);
74
		
75
		if (props != null) {
76
			Enumeration enumObj = props.keys();
77
			while (enumObj.hasMoreElements()) {
78
				
79
				String key = (String) enumObj.nextElement();
80
				String val = props.getProperty(key);
81
				
82
				buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
83
				buff.append(key);
84
				buff.append(JSonHelper.COLON);
85
				buff.append(JSonHelper.getQuotes(val));
86
				buff.append(JSonHelper.COMMA);
87
			}
88
		}
89
		
90
		if (children.size() <= 0) {
91
			int len = buff.length();
92
			char ch = buff.charAt(len - 1);
93
			if (ch == ',') {
94
				buff.deleteCharAt(len - 1);
95
				buff.append(JSonHelper.NEWLINE + space);
96
			}
97
			
98
		} else {
99
			
100
			buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
101
			buff.append(JSonHelper.CHILDREN);
102
			buff.append(JSonHelper.COLON);
103
			buff.append(JSonHelper.BEGIN_BRACKET);
104
			
105
			for (int i = 0; i < children.size(); i++) {
106
				
107
				if (i > 0)
108
					buff.append(JSonHelper.COMMA);
109
				
110
				ParseElement element = (ParseElement) children.get(i);
111
				buff.append(element.toJSON(level + 2));
112
			}
113
			
114
			buff.append(JSonHelper.NEWLINE + space + JSonHelper.SPACE);
115
			
116
			buff.append(JSonHelper.END_BRACKET);
117
			buff.append(JSonHelper.NEWLINE + space);
118
		}
119
		
120
		buff.append(JSonHelper.END_BRACE);
121
		
122
		return buff.toString();
123
	}
124
}
(-)src/org/eclipse/help/internal/webapp/parser/ResultParser.java (+100 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.net.URL;
17
import java.util.ArrayList;
18
19
import javax.xml.parsers.ParserConfigurationException;
20
import javax.xml.parsers.SAXParser;
21
import javax.xml.parsers.SAXParserFactory;
22
23
import org.eclipse.help.internal.webapp.utils.JSonHelper;
24
import org.xml.sax.SAXException;
25
import org.xml.sax.helpers.DefaultHandler;
26
27
public class ResultParser extends DefaultHandler {
28
29
	private String label = ""; //$NON-NLS-1$
30
	protected ArrayList items = new ArrayList(); //parser populates the items arrayList withe parsed data.
31
32
	public ResultParser(String label) {
33
		this.label = label;
34
	}
35
	
36
	public void parse(URL tocURL) 
37
		throws ParserConfigurationException, SAXException, IOException
38
	{
39
		parse(tocURL.openStream());
40
	}
41
	
42
	public void parse(InputStream in) 
43
		throws ParserConfigurationException, SAXException, IOException
44
	{
45
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
46
		parser.parse(in, this);
47
	}
48
	
49
	public ArrayList getItems()
50
	{
51
		return items;
52
	}
53
	
54
	public String toString()
55
	{
56
		return items.toString();
57
	}
58
	
59
	public String toJSON() {
60
		
61
		StringBuffer buf = new StringBuffer();
62
		
63
		buf.append(JSonHelper.BEGIN_BRACE);
64
		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
65
		
66
		buf.append(JSonHelper.IDENTIFIER);
67
		buf.append(JSonHelper.COLON);
68
		buf.append(JSonHelper.getQuotes(JSonHelper.ID));
69
		buf.append(JSonHelper.COMMA);
70
71
		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
72
		buf.append(JSonHelper.LABEL);
73
		buf.append(JSonHelper.COLON);
74
		buf.append(JSonHelper.getQuotes(label));
75
		buf.append(JSonHelper.COMMA);
76
77
		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
78
		buf.append(JSonHelper.ITEMS);
79
		buf.append(JSonHelper.COLON);
80
		buf.append(JSonHelper.BEGIN_BRACKET);
81
		
82
		for (int i = 0; i < items.size(); i++) {
83
			
84
			if (i > 0)
85
				buf.append(JSonHelper.COMMA);
86
			
87
			ParseElement element = (ParseElement) items.get(i);
88
			buf.append(element.toJSON(1));
89
		}
90
		
91
		if (items.size() > 0)
92
			buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
93
		
94
		buf.append(JSonHelper.END_BRACKET);
95
		buf.append(JSonHelper.NEWLINE);
96
		buf.append(JSonHelper.END_BRACE);
97
		
98
		return buf.toString();
99
	}
100
}
(-)src/org/eclipse/help/internal/webapp/parser/SearchParser.java (+94 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.eclipse.help.internal.webapp.utils.XMLHelper;
18
import org.xml.sax.Attributes;
19
20
public class SearchParser extends ResultParser {
21
22
//	private ParseElement element;
23
	private Properties properties;
24
	private String currentTag;
25
	
26
	public SearchParser() {
27
		super(JSonHelper.LABEL);
28
	}
29
30
	public void startElement(String uri, 
31
			String lname, String name, Attributes attrs) {
32
		
33
		currentTag = name;
34
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_HIT)) {
35
			
36
			properties = new Properties();
37
			properties.put(JSonHelper.PROPERTY_NAME, name);
38
			for (int i = 0; i < attrs.getLength(); i++) {
39
				String qname = attrs.getQName(i);
40
				String val = attrs.getValue(i);
41
				properties.put(qname, val);
42
			}
43
			
44
			String id = "" + items.size(); //$NON-NLS-1$
45
			properties.put(JSonHelper.ID, id);
46
			
47
		} else if (name.equalsIgnoreCase(XMLHelper.ELEMENT_CATEGORY)) {
48
//		} else {
49
			for (int i = 0; i < attrs.getLength(); i++) {
50
				String qname = attrs.getQName(i);
51
				String val = attrs.getValue(i);
52
				if (qname.equalsIgnoreCase(XMLHelper.ATTR_HREF))
53
					qname = XMLHelper.CATEGORY_HREF;
54
				properties.put(qname, val);
55
			}
56
		}
57
	}
58
	
59
	public void characters(char[] ch, int start, int length) {
60
		
61
		if (currentTag.equalsIgnoreCase(XMLHelper.ELEMENT_HIT) 
62
				|| currentTag.equalsIgnoreCase(XMLHelper.ELEMENT_HITS))
63
			return;
64
		
65
		if (properties != null)
66
		{
67
			String content = new String(ch, start, length);
68
			
69
			String existing = (String) properties.get(currentTag);
70
			if (existing == null)
71
				existing = ""; //$NON-NLS-1$
72
			
73
			content = content.replaceAll("[\\n\\t]", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$
74
			
75
			properties.put(currentTag, existing + content);
76
		}
77
	}
78
79
	public void endElement(String uri, String lname, String name) {
80
81
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_HIT)) {
82
			ParseElement element = new ParseElement(properties);
83
			items.add(element);
84
		}
85
//		} else if (name.equalsIgnoreCase(XMLHelper.ELEMENT_CATEGORY)) {
86
		/*} else {
87
			if (element != null && properties != null) {
88
				ParseElement category = new ParseElement(properties);
89
				element.addChild(category);
90
			}
91
			properties = null;
92
		}*/
93
	}
94
}
(-)src/org/eclipse/help/internal/webapp/parser/TocFragmentParser.java (+95 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.net.URL;
17
import java.util.Properties;
18
19
import javax.xml.parsers.ParserConfigurationException;
20
21
import org.eclipse.help.internal.webapp.utils.JSonHelper;
22
import org.eclipse.help.internal.webapp.utils.XMLHelper;
23
import org.xml.sax.Attributes;
24
import org.xml.sax.SAXException;
25
26
public class TocFragmentParser extends ResultParser {
27
28
	private Properties properties;
29
	private String parentId = ""; //$NON-NLS-1$
30
	private int tagLevel = -1;
31
	private int level = 0;
32
	
33
	public TocFragmentParser() {
34
		super(JSonHelper.TITLE);
35
	}
36
37
	public void parse(URL tocURL, int level) 
38
		throws ParserConfigurationException, SAXException, IOException
39
	{
40
		parse(tocURL.openStream(), level);
41
	}
42
	
43
	public void parse(InputStream in, int level) 
44
		throws ParserConfigurationException, SAXException, IOException
45
	{
46
		this.level = level;
47
		super.parse(in);
48
	}
49
50
	public void startElement(String uri, 
51
			String lname, String name, Attributes attrs) {
52
		
53
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE))
54
		{
55
			tagLevel++;
56
			
57
			if (tagLevel == level) {
58
				properties = new Properties();
59
				properties.put(JSonHelper.PROPERTY_NAME, JSonHelper.TOPIC);
60
				for (int i = 0; i < attrs.getLength(); i++) {
61
					String qname = attrs.getQName(i);
62
					String val = attrs.getValue(i);
63
					if (qname.equals(XMLHelper.ATTR_ID)) {
64
						if (parentId.length() > 0)
65
							val = parentId + '$' + val;
66
					}
67
					properties.put(qname, val);
68
				}
69
			} else if (parentId.length() <= 0) {
70
				for (int i = 0; i < attrs.getLength(); i++) {
71
					if (attrs.getQName(i).equals(XMLHelper.ATTR_ID))
72
						parentId = attrs.getValue(i);
73
				}
74
			}
75
		}
76
	}
77
78
	public void endElement(String uri, String lname, String name) {
79
80
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE))
81
		{
82
			if (tagLevel == level && properties != null ) {
83
				
84
				properties.setProperty("type", "toc"); //$NON-NLS-1$ //$NON-NLS-2$
85
				
86
				ParseElement element = new ParseElement(properties);
87
				items.add(element);
88
				
89
				properties = null;
90
			}
91
			
92
			tagLevel--;
93
		}
94
	}
95
}
(-)src/org/eclipse/help/internal/webapp/parser/TocParser.java (+59 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.parser;
13
14
import java.util.Properties;
15
16
import org.eclipse.help.internal.webapp.utils.JSonHelper;
17
import org.eclipse.help.internal.webapp.utils.XMLHelper;
18
import org.xml.sax.Attributes;
19
20
public class TocParser extends ResultParser {
21
22
	private ParseElement element = null;
23
24
	public TocParser() {
25
		super(JSonHelper.LABEL);
26
	}
27
	
28
	public void startElement(String uri, String lname, String name, Attributes attrs) {
29
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_TOC_CONTRIBUTIONS))
30
			return;
31
		
32
		Properties properties = new Properties();
33
		properties.put(JSonHelper.PROPERTY_NAME, name);
34
		for (int i = 0; i < attrs.getLength(); i++) {
35
			String qname = attrs.getQName(i);
36
			String val = attrs.getValue(i);
37
			properties.put(qname, val);
38
		}
39
		
40
		ParseElement elem = new ParseElement(properties, element);
41
		if (element != null)
42
			element.addChild(elem);
43
		else
44
			items.add(elem);
45
		
46
		element = elem;
47
		
48
	}
49
	
50
	public void endElement(String uri, String lname, String name) {
51
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_TOC_CONTRIBUTIONS))
52
			return;
53
		
54
		if (element != null) {
55
			element = element.getParent();
56
		}		
57
	}
58
59
}
(-)src/org/eclipse/help/internal/webapp/service/AboutService.java (+20 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import org.eclipse.help.internal.webapp.servlet.AboutServlet;
15
16
public class AboutService extends AboutServlet {
17
18
	private static final long serialVersionUID = 1L;
19
20
}
(-)src/org/eclipse/help/internal/webapp/service/ContentService.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.IOException;
15
import java.io.OutputStream;
16
import java.io.OutputStreamWriter;
17
import java.io.PrintWriter;
18
import java.net.URL;
19
20
import javax.servlet.ServletException;
21
import javax.servlet.http.HttpServlet;
22
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletResponse;
24
25
import org.eclipse.help.internal.webapp.utils.Utils;
26
27
public class ContentService extends HttpServlet {
28
29
	private static final long serialVersionUID = 1L;
30
31
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
32
			throws ServletException, IOException {
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		resp.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
35
		
36
		String servletPath = "/help"; //$NON-NLS-1$
37
		String pathInfo = req.getPathInfo();
38
		if (pathInfo != null) {
39
			int index = pathInfo.indexOf("/service"); //$NON-NLS-1$
40
			if (index > -1)
41
				pathInfo = pathInfo.substring(index + "/service".length()); //$NON-NLS-1$
42
			if (!pathInfo.equals("")) //$NON-NLS-1$
43
				servletPath += pathInfo.startsWith("/") ? pathInfo : "/" + pathInfo; //$NON-NLS-1$ //$NON-NLS-2$
44
		}
45
		
46
		String queryString = req.getQueryString();
47
		if (queryString != null && !queryString.equals("")) { //$NON-NLS-1$
48
			servletPath += "?" + queryString; //$NON-NLS-1$
49
		}
50
		
51
		URL url = new URL("http", req.getServerName(),  //$NON-NLS-1$
52
				req.getServerPort(), servletPath);
53
		String response = Utils.convertStreamToString(url.openStream());
54
		
55
		OutputStream out = resp.getOutputStream();
56
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); //$NON-NLS-1$
57
		writer.write(response);
58
		writer.close();
59
	}
60
61
}
(-)src/org/eclipse/help/internal/webapp/service/ContextService.java (+83 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.ContextParser;
23
import org.eclipse.help.internal.webapp.servlet.ContextServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
public class ContextService extends ContextServlet {
27
28
	private static final long serialVersionUID = 1L;
29
30
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
31
			throws ServletException, IOException {
32
		
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		// Set standard HTTP/1.1 no-cache headers.
35
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
36
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
37
		
38
		String response = processRequest(req, resp);
39
		if ("400".equals(response)) { //$NON-NLS-1$
40
			resp.sendError(400); // bad request; missing parameter
41
			return;
42
		} else if ("404".equals(response)) { //$NON-NLS-1$
43
			resp.sendError(404); // Wrong context id; not found
44
			return;
45
		}
46
		
47
		String returnType = req.getParameter(Utils.RETURN_TYPE);
48
		boolean boolIsJSON = (returnType != null 
49
				&& returnType.equalsIgnoreCase(Utils.JSON));
50
		
51
		// If JSON output is required
52
		if (boolIsJSON) {
53
			resp.setContentType("text/plain"); //$NON-NLS-1$
54
			response = getJSONResponse(response);
55
		}
56
		
57
		resp.getWriter().write(response);
58
	}
59
60
	protected String getJSONResponse(String response)
61
			throws IOException {
62
		ContextParser searchParser = new ContextParser();
63
		InputStream is = null;
64
		try {
65
			if (response != null) {
66
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
67
				searchParser.parse(is);
68
			}
69
70
		} catch (Exception e) {
71
			e.printStackTrace();
72
		}
73
		
74
		if (is != null)
75
			is.close();
76
77
		// Call after the catch.
78
		// An empty JSON is created if any Exception is thrown
79
		// Else returns the complete JSON
80
		return searchParser.toJSON();
81
	}
82
83
}
(-)src/org/eclipse/help/internal/webapp/service/ControlService.java (+20 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import org.eclipse.help.internal.webapp.servlet.ControlServlet;
15
16
public class ControlService extends ControlServlet {
17
18
	private static final long serialVersionUID = 1L;
19
20
}
(-)src/org/eclipse/help/internal/webapp/service/ExtensionService.java (+75 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.ExtensionParser;
23
import org.eclipse.help.internal.webapp.servlet.ExtensionServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
public class ExtensionService extends ExtensionServlet {
27
28
	private static final long serialVersionUID = 1L;
29
30
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
31
			throws ServletException, IOException {
32
		
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		// Set standard HTTP/1.1 no-cache headers.
35
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
36
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
37
		
38
		String response = processRequest(req, resp);
39
		String returnType = req.getParameter(Utils.RETURN_TYPE);
40
		boolean boolIsJSON = (returnType != null 
41
				&& returnType.equalsIgnoreCase(Utils.JSON));
42
		
43
		// If JSON output is required
44
		if (boolIsJSON) {
45
			resp.setContentType("text/plain"); //$NON-NLS-1$
46
			response = getJSONResponse(response);
47
		}
48
		
49
		resp.getWriter().write(response);
50
	}
51
52
	protected String getJSONResponse(String response)
53
			throws IOException {
54
		ExtensionParser searchParser = new ExtensionParser();
55
		InputStream is = null;
56
		try {
57
			if (response != null) {
58
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
59
				searchParser.parse(is);
60
			}
61
62
		} catch (Exception e) {
63
			e.printStackTrace();
64
		}
65
		
66
		if (is != null)
67
			is.close();
68
69
		// Call after the catch.
70
		// An empty JSON is created if any Exception is thrown
71
		// Else returns the complete JSON
72
		return searchParser.toJSON();
73
	}
74
75
}
(-)src/org/eclipse/help/internal/webapp/service/IndexFragmentService.java (+85 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.IndexFragmentParser;
23
import org.eclipse.help.internal.webapp.servlet.IndexFragmentServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
/*
27
 * Sends all available keyword index data in XML form. The data is sent as one
28
 * large index contribution that includes all merged contributions from the
29
 * system.
30
 * 
31
 * This is called on infocenters by client workbenches configured for remote
32
 * help in order to gather all the index keywords and assemble them into a
33
 * complete index.
34
 */
35
public class IndexFragmentService extends IndexFragmentServlet {
36
37
	private static final long serialVersionUID = 1L;
38
39
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
40
			throws ServletException, IOException {
41
		
42
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
43
		// Set standard HTTP/1.1 no-cache headers.
44
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
45
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
46
		
47
		String response = processRequest(req, resp);
48
		
49
		String returnType = req.getParameter(Utils.RETURN_TYPE);
50
		boolean boolIsJSON = (returnType != null 
51
				&& returnType.equalsIgnoreCase(Utils.JSON));
52
		
53
		// If JSON output is required
54
		if (boolIsJSON) {
55
			resp.setContentType("text/plain"); //$NON-NLS-1$
56
			response = getJSONResponse(response);
57
		}
58
		
59
		resp.getWriter().write(response);
60
	}
61
62
	protected String getJSONResponse(String response)
63
			throws IOException {
64
		IndexFragmentParser indexParser = new IndexFragmentParser();
65
		InputStream is = null;
66
		try {
67
			if (response != null) {
68
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
69
				indexParser.parse(is);
70
			}
71
72
		} catch (Exception e) {
73
			e.printStackTrace();
74
		}
75
		
76
		if (is != null)
77
			is.close();
78
79
		// Call after the catch.
80
		// An empty JSON is created if any Exception is thrown
81
		// Else returns the complete JSON
82
		return indexParser.toJSON();
83
	}
84
85
}
(-)src/org/eclipse/help/internal/webapp/service/IndexService.java (+76 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.IndexParser;
23
import org.eclipse.help.internal.webapp.servlet.IndexServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
public class IndexService extends IndexServlet {
27
28
	private static final long serialVersionUID = 1L;
29
30
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
31
			throws ServletException, IOException {
32
		
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		// Set standard HTTP/1.1 no-cache headers.
35
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
36
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
37
		
38
		String response = processRequest(req, resp);
39
		
40
		String returnType = req.getParameter(Utils.RETURN_TYPE);
41
		boolean boolIsJSON = (returnType != null 
42
				&& returnType.equalsIgnoreCase(Utils.JSON));
43
		
44
		// If JSON output is required
45
		if (boolIsJSON) {
46
			resp.setContentType("text/plain"); //$NON-NLS-1$
47
			response = getJSONResponse(response);
48
		}
49
		
50
		resp.getWriter().write(response);
51
	}
52
53
	protected String getJSONResponse(String response)
54
			throws IOException {
55
		IndexParser indexParser = new IndexParser();
56
		InputStream is = null;
57
		try {
58
			if (response != null) {
59
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
60
				indexParser.parse(is);
61
			}
62
63
		} catch (Exception e) {
64
			e.printStackTrace();
65
		}
66
		
67
		if (is != null)
68
			is.close();
69
70
		// Call after the catch.
71
		// An empty JSON is created if any Exception is thrown
72
		// Else returns the complete JSON
73
		return indexParser.toJSON();
74
	}
75
76
}
(-)src/org/eclipse/help/internal/webapp/service/LiveHelpService.java (+20 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import org.eclipse.help.internal.webapp.servlet.LiveHelpServlet;
15
16
public class LiveHelpService extends LiveHelpServlet {
17
18
	private static final long serialVersionUID = 1L;
19
20
}
(-)src/org/eclipse/help/internal/webapp/service/NavService.java (+79 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.IOException;
15
import java.io.OutputStream;
16
import java.io.OutputStreamWriter;
17
import java.io.PrintWriter;
18
import java.net.URL;
19
20
import javax.servlet.ServletException;
21
import javax.servlet.http.HttpServlet;
22
import javax.servlet.http.HttpServletRequest;
23
import javax.servlet.http.HttpServletResponse;
24
25
import org.eclipse.help.internal.webapp.utils.Utils;
26
27
public class NavService extends HttpServlet {
28
29
	private static final long serialVersionUID = 1L;
30
31
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
32
			throws ServletException, IOException {
33
		
34
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
35
		resp.setContentType("text/html; charset=UTF-8"); //$NON-NLS-1$
36
		
37
		String servletPath = "/help"; //$NON-NLS-1$
38
		String pathInfo = req.getPathInfo();
39
		if (pathInfo != null) {
40
			int index = pathInfo.indexOf("/service"); //$NON-NLS-1$
41
			if (index > -1)
42
				pathInfo = pathInfo.substring(index + "/service".length()); //$NON-NLS-1$
43
			if (!pathInfo.equals("")) //$NON-NLS-1$
44
				servletPath += pathInfo.startsWith("/") ? pathInfo : "/" + pathInfo; //$NON-NLS-1$ //$NON-NLS-2$
45
		}
46
		
47
		String queryString = req.getQueryString();
48
		if (queryString != null && !queryString.equals("")) { //$NON-NLS-1$
49
			servletPath += "?" + queryString; //$NON-NLS-1$
50
		}
51
		
52
		URL url = new URL("http", req.getServerName(),  //$NON-NLS-1$
53
				req.getServerPort(), servletPath);
54
		String response = Utils.convertStreamToString(url.openStream());
55
		
56
		OutputStream out = resp.getOutputStream();
57
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); //$NON-NLS-1$
58
		writer.write(response);
59
		writer.close();
60
		
61
		/*try {
62
			HttpServlet servlet = getWebappServlet("NavServlet"); //$NON-NLS-1$
63
			ServletConfig config = getServletConfig();
64
			servlet.init(config);
65
			servlet.service(req, resp);
66
		} catch (Exception e) {
67
			e.printStackTrace();
68
		}*/
69
	}
70
71
	/*private HttpServlet getWebappServlet(String service) 
72
			throws Exception {
73
		Class cls = Class.forName(
74
				"org.eclipse.help.internal.webapp.servlet." + service); //$NON-NLS-1$
75
		Constructor constructor = cls.getConstructor(new Class[]{});
76
		return (HttpServlet)constructor.newInstance(new Object[]{});
77
	}*/
78
79
}
(-)src/org/eclipse/help/internal/webapp/service/SearchService.java (+109 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletContext;
19
import javax.servlet.ServletException;
20
import javax.servlet.http.HttpServlet;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23
24
import org.eclipse.help.internal.search.SearchHit;
25
import org.eclipse.help.internal.search.SearchProgressMonitor;
26
import org.eclipse.help.internal.webapp.data.SearchData;
27
import org.eclipse.help.internal.webapp.data.UrlUtil;
28
import org.eclipse.help.internal.webapp.parser.SearchParser;
29
import org.eclipse.help.internal.webapp.utils.Utils;
30
import org.eclipse.help.internal.webapp.utils.SearchXMLGenerator;
31
32
/*
33
 * Returns the search hits for the query provided in the phrase parameter.
34
 * 
35
 * This is called on infocenters by client workbenches configured for remote
36
 * help in order to retrieve search hits from the remote help server.
37
 */
38
public class SearchService extends HttpServlet {
39
	
40
	private static final long serialVersionUID = 1L;
41
	public static final String XID = "xid"; //$NON-NLS-1$
42
	
43
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
44
			throws ServletException, IOException {
45
		
46
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
47
		// Set standard HTTP/1.1 no-cache headers.
48
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
49
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
50
		
51
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
52
		
53
		ServletContext context = req.getSession().getServletContext();
54
		SearchData searchData = new SearchData(context, req, resp);
55
		
56
		String noCat = req.getParameter(Utils.NO_CATEGORY);
57
		boolean boolIsCategory = (noCat == null 
58
				|| !noCat.equalsIgnoreCase("true")); //$NON-NLS-1$
59
		
60
		String locale = UrlUtil.getLocale(req, resp);
61
		SearchProgressMonitor pm = SearchProgressMonitor
62
				.getProgressMonitor(locale);
63
		while (!pm.isDone()) {
64
			try {
65
				Thread.sleep(500); // Sleep for 0.5 sec
66
			} catch(InterruptedException ex) {}
67
		}
68
		
69
		SearchHit[] hits = searchData.getResults();
70
		
71
		String response = SearchXMLGenerator.serialize(hits, boolIsCategory);
72
		
73
		String returnType = req.getParameter(Utils.RETURN_TYPE);
74
		boolean boolIsJSON = (returnType != null 
75
				&& returnType.equalsIgnoreCase(Utils.JSON));
76
		
77
		// If JSON output is required
78
		if (boolIsJSON) {
79
			resp.setContentType("text/plain"); //$NON-NLS-1$
80
			response = getJSONResponse(response);
81
		}
82
		
83
		resp.getWriter().write(response);
84
	}
85
	
86
	protected String getJSONResponse(String response)
87
			throws IOException {
88
		SearchParser searchParser = new SearchParser();
89
		
90
		InputStream is = null;
91
		try {
92
			if (response != null) {
93
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
94
				searchParser.parse(is);
95
			}
96
			
97
		} catch (Exception e) {
98
			e.printStackTrace();
99
		}
100
		
101
		if (is != null)
102
			is.close();
103
        
104
        // Call after the catch.
105
		// An empty JSON is created if any Exception is thrown
106
		// Else returns the complete JSON
107
		return searchParser.toJSON();
108
	}
109
}
(-)src/org/eclipse/help/internal/webapp/service/SearchStateService.java (+112 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.IOException;
15
16
import javax.servlet.ServletException;
17
import javax.servlet.http.HttpServlet;
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
21
import org.eclipse.help.internal.search.SearchProgressMonitor;
22
import org.eclipse.help.internal.webapp.data.UrlUtil;
23
import org.eclipse.help.internal.webapp.utils.Utils;
24
25
/**
26
 * This is the main servlet for WAR Update Actions.  It delegates each action to the
27
 * appropriate <code>IDelegatorOperation</code>
28
 * 
29
 * @author snehpaul
30
 *
31
 */
32
public class SearchStateService extends HttpServlet {
33
	
34
	private static final long serialVersionUID = 1L;
35
	private final static String STATE = "state"; //$NON-NLS-1$
36
	private final static String PERCENT = "percent"; //$NON-NLS-1$
37
	
38
	public void init() throws ServletException {
39
	}
40
41
	/**
42
	 * Called by the server (via the <code>service</code> method) to allow a
43
	 * Servlet to handle a GET request.
44
	 */
45
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46
			throws ServletException, IOException {
47
48
		process(req,resp);
49
	}
50
51
	/**
52
	 *
53
	 * Called by the server (via the <code>service</code> method) to allow a
54
	 * Servlet to handle a POST request.
55
	 *
56
	 * Handle the search requests,
57
	 *
58
	 */
59
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
60
			throws ServletException, IOException {
61
		process(req, resp);
62
	}
63
64
65
	/**
66
	 * Processes all requests to the servlet.
67
	 *
68
	 */
69
	private void process(HttpServletRequest req, HttpServletResponse resp)
70
			throws ServletException, IOException {
71
		
72
		resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); //$NON-NLS-1$ //$NON-NLS-2$
73
		resp.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
74
		
75
		int indexCompletion = 0;
76
		String locale = UrlUtil.getLocale(req, resp);
77
		SearchProgressMonitor pm = SearchProgressMonitor
78
				.getProgressMonitor(locale);
79
		if (pm.isDone()) {
80
			indexCompletion = 100;
81
		} else {
82
			indexCompletion = pm.getPercentage();
83
			if (indexCompletion >= 100) {
84
				// 38573 We do not have results, so index cannot be 100
85
				indexCompletion = 100 - 1;
86
			}
87
		}
88
		
89
		String returnType = req.getParameter(Utils.RETURN_TYPE);
90
		boolean isXML = Utils.XML.equalsIgnoreCase(returnType);
91
		if (isXML) {
92
			resp.setContentType("application/xml"); //$NON-NLS-1$
93
			resp.getWriter().write(toXML(indexCompletion));
94
		} else {
95
			resp.setContentType("text/plain"); //$NON-NLS-1$
96
			resp.getWriter().write(toString(indexCompletion));
97
		}
98
		resp.getWriter().flush();
99
	}
100
	
101
	public static String toXML(int percent) {
102
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; //$NON-NLS-1$
103
		xml += '<'+STATE+">\n"; //$NON-NLS-1$
104
		xml += "	<"+PERCENT+'>'+percent+"</"+PERCENT+">\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
105
		xml += "</"+STATE+">"; //$NON-NLS-1$ //$NON-NLS-2$
106
		return xml;
107
	}
108
	
109
	public static String toString(int percent) {
110
		return "Percent:" + percent; //$NON-NLS-1$
111
	}
112
}
(-)src/org/eclipse/help/internal/webapp/service/SimpleSearchService.java (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.SearchParser;
23
import org.eclipse.help.internal.webapp.servlet.SearchServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
public class SimpleSearchService extends SearchServlet {
27
28
	private static final long serialVersionUID = 1L;
29
30
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
31
			throws ServletException, IOException {
32
		
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		// Set standard HTTP/1.1 no-cache headers.
35
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
36
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
37
		
38
		String response = processRequest(req, resp);
39
		if ("".equals(response)) { //$NON-NLS-1$
40
			resp.sendError(400); // bad request; missing parameter
41
			return;
42
		}
43
		
44
		String returnType = req.getParameter(Utils.RETURN_TYPE);
45
		boolean boolIsJSON = (returnType != null 
46
				&& returnType.equalsIgnoreCase(Utils.JSON));
47
		
48
		// If JSON output is required
49
		if (boolIsJSON) {
50
			resp.setContentType("text/plain"); //$NON-NLS-1$
51
			response = getJSONResponse(response);
52
		}
53
		
54
		resp.getWriter().write(response);
55
	}
56
57
	protected String getJSONResponse(String response)
58
			throws IOException {
59
		SearchParser searchParser = new SearchParser();
60
		InputStream is = null;
61
		try {
62
			if (response != null) {
63
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
64
				searchParser.parse(is);
65
			}
66
67
		} catch (Exception e) {
68
			e.printStackTrace();
69
		}
70
		
71
		if (is != null)
72
			is.close();
73
74
		// Call after the catch.
75
		// An empty JSON is created if any Exception is thrown
76
		// Else returns the complete JSON
77
		return searchParser.toJSON();
78
	}
79
80
}
(-)src/org/eclipse/help/internal/webapp/service/TocFragmentService.java (+95 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.TocFragmentParser;
23
import org.eclipse.help.internal.webapp.servlet.TocFragmentServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
/*
27
 * Sends all toc contributions available on this host in XML form. The toc
28
 * is not send in assembled form, but instead fragments, because complete books
29
 * may be distributed between remote and local.
30
 * 
31
 */
32
public class TocFragmentService extends TocFragmentServlet {
33
	
34
	private static final long serialVersionUID = 1L;
35
36
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
37
			throws ServletException, IOException {
38
		
39
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
40
		// Set standard HTTP/1.1 no-cache headers.
41
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
42
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
43
		
44
		String response = processRequest(req, resp);
45
		
46
		String returnType = req.getParameter(Utils.RETURN_TYPE);
47
		boolean boolIsJSON = (returnType != null 
48
				&& returnType.equalsIgnoreCase(Utils.JSON));
49
		
50
		// If JSON output is required
51
		if (boolIsJSON) {
52
			resp.setContentType("text/plain"); //$NON-NLS-1$
53
			String toc = req.getParameter("toc"); //$NON-NLS-1$
54
			String path = req.getParameter("path"); //$NON-NLS-1$
55
			response = getJSONResponse(toc, path, response);
56
		}
57
		
58
		resp.getWriter().write(response);
59
	}
60
	
61
	protected String getJSONResponse(String toc, String path, String xmlResource)
62
			throws IOException {
63
		TocFragmentParser tocParser = new TocFragmentParser();
64
		InputStream is = null;
65
		try {
66
			if (xmlResource != null) {
67
	            is = new ByteArrayInputStream(xmlResource.getBytes("UTF-8")); //$NON-NLS-1$
68
	            
69
	            int level = 0;
70
				if (toc != null && toc.length() > 0) {
71
					level++;
72
					
73
					
74
					if (path != null && path.length() > 0) {
75
						String[] pathIdxs = path.split("_"); //$NON-NLS-1$
76
						level += pathIdxs.length;
77
					}
78
				}
79
				
80
				tocParser.parse(is, level);
81
			}
82
			
83
		} catch (Exception e) {
84
			e.printStackTrace();
85
		}
86
		
87
		if (is != null)
88
			is.close();
89
        
90
        // Call after the catch.
91
		// An empty JSON is created if any Exception is thrown
92
		// Else returns the complete JSON
93
		return tocParser.toJSON();
94
	}
95
}
(-)src/org/eclipse/help/internal/webapp/service/TocService.java (+76 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.service;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
import javax.servlet.ServletException;
19
import javax.servlet.http.HttpServletRequest;
20
import javax.servlet.http.HttpServletResponse;
21
22
import org.eclipse.help.internal.webapp.parser.TocParser;
23
import org.eclipse.help.internal.webapp.servlet.TocServlet;
24
import org.eclipse.help.internal.webapp.utils.Utils;
25
26
public class TocService extends TocServlet {
27
28
	private static final long serialVersionUID = 1L;
29
30
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
31
			throws ServletException, IOException {
32
		
33
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
34
		// Set standard HTTP/1.1 no-cache headers.
35
		resp.setHeader("Cache-Control",  //$NON-NLS-1$
36
				"no-store, no-cache, must-revalidate"); //$NON-NLS-1$
37
		
38
		String response = processRequest(req, resp);
39
		
40
		String returnType = req.getParameter(Utils.RETURN_TYPE);
41
		boolean boolIsJSON = (returnType != null 
42
				&& returnType.equalsIgnoreCase(Utils.JSON));
43
		
44
		// If JSON output is required
45
		if (boolIsJSON) {
46
			resp.setContentType("text/plain"); //$NON-NLS-1$
47
			response = getJSONResponse(response);
48
		}
49
		
50
		resp.getWriter().write(response);
51
	}
52
53
	protected String getJSONResponse(String response)
54
			throws IOException {
55
		TocParser tocParser = new TocParser();
56
		InputStream is = null;
57
		try {
58
			if (response != null) {
59
				is = new ByteArrayInputStream(response.getBytes("UTF-8")); //$NON-NLS-1$
60
				tocParser.parse(is);
61
			}
62
63
		} catch (Exception e) {
64
			e.printStackTrace();
65
		}
66
		
67
		if (is != null)
68
			is.close();
69
70
		// Call after the catch.
71
		// An empty JSON is created if any Exception is thrown
72
		// Else returns the complete JSON
73
		return tocParser.toJSON();
74
	}
75
76
}
(-)src/org/eclipse/help/internal/webapp/servlet/ContextServlet.java (-20 / +31 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-17 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
13
import java.io.IOException;
14
import java.io.IOException;
14
import java.io.Writer;
15
15
16
import javax.servlet.ServletException;
16
import javax.servlet.ServletException;
17
import javax.servlet.http.HttpServlet;
17
import javax.servlet.http.HttpServlet;
Lines 40-45 Link Here
40
	
40
	
41
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
41
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
42
			throws ServletException, IOException {
42
			throws ServletException, IOException {
43
		String response = processRequest(req, resp);
44
		if ("400".equals(response)) //$NON-NLS-1$
45
			resp.sendError(400); // bad request; missing parameter
46
		else if ("404".equals(response)) //$NON-NLS-1$
47
			resp.sendError(404); // Wrong context id; not found
48
		else
49
			resp.getWriter().write(response);
50
	}
51
	
52
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
53
			throws ServletException, IOException {
43
		BaseHelpSystem.checkMode();
54
		BaseHelpSystem.checkMode();
44
		String locale = UrlUtil.getLocale(req, resp);
55
		String locale = UrlUtil.getLocale(req, resp);
45
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
56
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
Lines 48-62 Link Here
48
		if (id != null) {
59
		if (id != null) {
49
			IContext context = getContext(locale, id);
60
			IContext context = getContext(locale, id);
50
			if (context != null) {
61
			if (context != null) {
51
				serialize(context, resp.getWriter());
62
				return serialize(context);
52
			}
63
			}
53
			else {
64
			// Wrong context id; not found
54
				resp.sendError(404);
65
			return "404"; //$NON-NLS-1$
55
			}
56
		}
57
		else {
58
			resp.sendError(400); // bad request; missing parameter
59
		}
66
		}
67
		// bad request; missing parameter
68
		return "400"; //$NON-NLS-1$
60
	}
69
	}
61
70
62
	protected IContext getContext(String locale, String id) {
71
	protected IContext getContext(String locale, String id) {
Lines 64-95 Link Here
64
		return context;
73
		return context;
65
	}
74
	}
66
	
75
	
67
	private void serialize(IContext context, Writer out) throws IOException {
76
	private String serialize(IContext context) throws IOException {
68
		out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
77
		StringBuffer buff = new StringBuffer();
69
		out.write('<' + Context.NAME );
78
		buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
79
		buff.append('<' + Context.NAME );
70
		if (context instanceof IContext2) {
80
		if (context instanceof IContext2) {
71
			String title = ((IContext2)context).getTitle();
81
			String title = ((IContext2)context).getTitle();
72
			if (title != null && title.length() > 0) {
82
			if (title != null && title.length() > 0) {
73
				out.write(" title=\"" + title + "\""); //$NON-NLS-1$ //$NON-NLS-2$			
83
				buff.append(" title=\"" + title + "\""); //$NON-NLS-1$ //$NON-NLS-2$			
74
			}
84
			}
75
		}
85
		}
76
		out.write(">\n"); //$NON-NLS-1$
86
		buff.append(">\n"); //$NON-NLS-1$
77
		String description = context.getText();
87
		String description = context.getText();
78
		if (description != null) {
88
		if (description != null) {
79
			out.write("   <description>" + UrlUtil.htmlEncode(description) + "</description>\n"); //$NON-NLS-1$ //$NON-NLS-2$
89
			buff.append("   <description>" + UrlUtil.htmlEncode(description) + "</description>\n"); //$NON-NLS-1$ //$NON-NLS-2$
80
		}
90
		}
81
		
91
		
82
		IHelpResource[] topics = context.getRelatedTopics();
92
		IHelpResource[] topics = context.getRelatedTopics();
83
		for (int i=0;i<topics.length;++i) {
93
		for (int i=0;i<topics.length;++i) {
84
			out.write("   <" + Topic.NAME); //$NON-NLS-1$
94
			buff.append("   <" + Topic.NAME); //$NON-NLS-1$
85
			if (topics[i].getLabel() != null) {
95
			if (topics[i].getLabel() != null) {
86
				out.write("\n         " + Topic.ATTRIBUTE_LABEL + "=\"" + topics[i].getLabel() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
96
				buff.append("\n         " + Topic.ATTRIBUTE_LABEL + "=\"" + topics[i].getLabel() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
87
			}
97
			}
88
			if (topics[i].getHref() != null) {
98
			if (topics[i].getHref() != null) {
89
				out.write("\n         " + Topic.ATTRIBUTE_HREF + "=\"" + topics[i].getHref() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
99
				buff.append("\n         " + Topic.ATTRIBUTE_HREF + "=\"" + topics[i].getHref() + '"'); //$NON-NLS-1$ //$NON-NLS-2$
90
			}
100
			}
91
			out.write(">\n   </topic>"); //$NON-NLS-1$
101
			buff.append(">   </" + Topic.NAME + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
92
		}
102
		}
93
		out.write("</" + Context.NAME + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
103
		buff.append("</" + Context.NAME + ">\n"); //$NON-NLS-1$ //$NON-NLS-2$
104
		return buff.toString();
94
	}
105
	}
95
}
106
}
(-)src/org/eclipse/help/internal/webapp/servlet/ExtensionServlet.java (-2 / +8 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
2
 * Copyright (c) 2007, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 39-44 Link Here
39
40
40
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
41
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
41
			throws ServletException, IOException {
42
			throws ServletException, IOException {
43
			resp.getWriter().write(processRequest(req, resp));
44
	}
45
	
46
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
47
			throws ServletException, IOException {
42
		String locale = UrlUtil.getLocale(req, resp);
48
		String locale = UrlUtil.getLocale(req, resp);
43
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
49
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
44
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
50
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
Lines 57-63 Link Here
57
			}
63
			}
58
			responseByLocale.put(locale, response);
64
			responseByLocale.put(locale, response);
59
		}
65
		}
60
		resp.getWriter().write(response);
66
		return response;
61
	}
67
	}
62
	
68
	
63
	private String serialize(ContentExtension[] extensions) throws TransformerException {
69
	private String serialize(ContentExtension[] extensions) throws TransformerException {
(-)src/org/eclipse/help/internal/webapp/servlet/IndexFragmentServlet.java (-2 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
2
 * Copyright (c) 2007, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 68-73 Link Here
68
69
69
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
70
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
70
			throws ServletException, IOException {
71
			throws ServletException, IOException {
72
		resp.getWriter().write(processRequest(req, resp));
73
	}
74
	
75
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
76
			throws ServletException, IOException {
71
		String locale = UrlUtil.getLocale(req, resp);
77
		String locale = UrlUtil.getLocale(req, resp);
72
		startParameter = req.getParameter("start"); //$NON-NLS-1$
78
		startParameter = req.getParameter("start"); //$NON-NLS-1$
73
		if (startParameter != null) {
79
		if (startParameter != null) {
Lines 112-118 Link Here
112
		Serializer serializer = new Serializer(locale, scope);
118
		Serializer serializer = new Serializer(locale, scope);
113
		String response = serializer.generateIndexXml();	
119
		String response = serializer.generateIndexXml();	
114
		locale2Response.put(locale, response);
120
		locale2Response.put(locale, response);
115
		resp.getWriter().write(response);
121
		
122
		return response;
116
	}
123
	}
117
	
124
	
118
	/*
125
	/*
(-)src/org/eclipse/help/internal/webapp/servlet/IndexServlet.java (-2 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 44-49 Link Here
44
45
45
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46
			throws ServletException, IOException {
47
			throws ServletException, IOException {
48
		resp.getWriter().write(processRequest(req, resp));
49
	}
50
	
51
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
52
			throws ServletException, IOException {
47
		BaseHelpSystem.checkMode();
53
		BaseHelpSystem.checkMode();
48
		String locale = UrlUtil.getLocale(req, resp);
54
		String locale = UrlUtil.getLocale(req, resp);
49
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
55
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
Lines 63-69 Link Here
63
			}
69
			}
64
			responseByLocale.put(locale, response);
70
			responseByLocale.put(locale, response);
65
		}
71
		}
66
		resp.getWriter().write(response);
72
		
73
		return (response != null) ? response : ""; //$NON-NLS-1$
67
	}
74
	}
68
		
75
		
69
	public String serialize(IndexContribution[] contributions, String locale) throws TransformerException {
76
	public String serialize(IndexContribution[] contributions, String locale) throws TransformerException {
(-)src/org/eclipse/help/internal/webapp/servlet/SearchServlet.java (-47 / +16 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 14-20 Link Here
14
import java.util.ArrayList;
15
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.Collections;
17
import java.util.Iterator;
18
import java.util.List;
18
import java.util.List;
19
19
20
import javax.servlet.ServletException;
20
import javax.servlet.ServletException;
Lines 27-36 Link Here
27
import org.eclipse.help.internal.search.ISearchHitCollector;
27
import org.eclipse.help.internal.search.ISearchHitCollector;
28
import org.eclipse.help.internal.search.ISearchQuery;
28
import org.eclipse.help.internal.search.ISearchQuery;
29
import org.eclipse.help.internal.search.QueryTooComplexException;
29
import org.eclipse.help.internal.search.QueryTooComplexException;
30
import org.eclipse.help.internal.search.SearchHit;
31
import org.eclipse.help.internal.search.SearchQuery;
30
import org.eclipse.help.internal.search.SearchQuery;
32
import org.eclipse.help.internal.util.URLCoder;
31
import org.eclipse.help.internal.util.URLCoder;
33
import org.eclipse.help.internal.webapp.data.UrlUtil;
32
import org.eclipse.help.internal.webapp.data.UrlUtil;
33
import org.eclipse.help.internal.webapp.utils.SearchXMLGenerator;
34
34
35
/*
35
/*
36
 * Returns the search hits for the query provided in the phrase parameter.
36
 * Returns the search hits for the query provided in the phrase parameter.
Lines 61-125 Link Here
61
	
61
	
62
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
62
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
63
			throws ServletException, IOException {
63
			throws ServletException, IOException {
64
		String response = processRequest(req, resp);
65
		if ("".equals(response)) //$NON-NLS-1$
66
			resp.sendError(400); // bad request; missing parameter
67
		else
68
			resp.getWriter().write(response);
69
	}
70
	
71
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
72
			throws ServletException, IOException {
64
		BaseHelpSystem.checkMode();
73
		BaseHelpSystem.checkMode();
65
		HitCollector collector = new HitCollector();
74
		HitCollector collector = new HitCollector();
66
		String locale = UrlUtil.getLocale(req, resp);
75
		String locale = UrlUtil.getLocale(req, resp);
67
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
76
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
68
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
77
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
69
		String phrase = req.getParameter(PARAMETER_PHRASE); 
78
		String phrase = req.getParameter(PARAMETER_PHRASE);
70
		if (phrase != null) {
79
		if (phrase != null) {
71
		    phrase = URLCoder.decode(phrase);
80
		    phrase = URLCoder.decode(phrase);
72
			ISearchQuery query = new SearchQuery(phrase, false, Collections.EMPTY_LIST, locale);
81
			ISearchQuery query = new SearchQuery(phrase, false, Collections.EMPTY_LIST, locale);
73
			collector.results.clear();
82
			collector.results.clear();
74
			BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
83
			BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
75
			if (searchException == null) {
84
			if (searchException == null) {
76
				String response = serialize(collector.results);
85
				return serialize(collector.results);
77
				resp.getWriter().write(response);
78
				return;
79
			}
86
			}
80
		}
87
		}
81
88
		return ""; //$NON-NLS-1$
82
		resp.sendError(400); // bad request; missing parameter
83
84
	}
89
	}
85
	
90
	
86
	public static String serialize(Collection results) {
91
	public static String serialize(Collection results) {
87
		StringBuffer buf = new StringBuffer();
92
		return SearchXMLGenerator.serialize(results);
88
		buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
89
		buf.append("<searchHits>\n"); //$NON-NLS-1$
90
		Iterator iter = results.iterator();
91
		while (iter.hasNext()) {
92
			SearchHit hit = (SearchHit)iter.next();
93
			serialize(hit, buf, "   "); //$NON-NLS-1$
94
		}
95
		buf.append("</searchHits>\n"); //$NON-NLS-1$
96
		return buf.toString();
97
	}
98
	
99
	private static void serialize(SearchHit hit, StringBuffer buf, String indent) {
100
		buf.append(indent + "<hit"); //$NON-NLS-1$
101
		if (hit.getHref() != null) {
102
			buf.append('\n' + indent	+ "      href=\"" + XMLGenerator.xmlEscape(hit.getHref()) + '"'); //$NON-NLS-1$
103
		}
104
		if (hit.getLabel() != null) {
105
			buf.append('\n' + indent	+ "      label=\"" + XMLGenerator.xmlEscape(hit.getLabel()) + '"'); //$NON-NLS-1$
106
		}
107
		if (hit.isPotentialHit()) {
108
			buf.append('\n' + indent	+ "      isPotentialHit=\"true\""); //$NON-NLS-1$
109
		}
110
		buf.append('\n' + indent + "      score=\"" + hit.getScore() + '"'); //$NON-NLS-1$
111
		buf.append(">\n"); //$NON-NLS-1$
112
		
113
		String summary = hit.getSummary();
114
		if (summary != null) {
115
			serialize(summary, buf, indent + "   "); //$NON-NLS-1$
116
		}
117
		buf.append(indent + "</hit>\n"); //$NON-NLS-1$
118
	}
119
120
	private static void serialize(String summary, StringBuffer buf, String indent) {
121
		buf.append(indent + "<summary>"); //$NON-NLS-1$
122
		buf.append(XMLGenerator.xmlEscape(summary));
123
		buf.append("</summary>\n"); //$NON-NLS-1$
124
	}
93
	}
125
}
94
}
(-)src/org/eclipse/help/internal/webapp/servlet/SecureServlet.java (+267 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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.ByteArrayInputStream;
14
import java.io.ByteArrayOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.OutputStream;
18
import java.io.PrintWriter;
19
import java.io.UnsupportedEncodingException;
20
import java.util.ArrayList;
21
import java.util.Enumeration;
22
import java.util.Hashtable;
23
import java.util.List;
24
import java.util.Locale;
25
26
import javax.servlet.ServletConfig;
27
import javax.servlet.ServletException;
28
import javax.servlet.ServletOutputStream;
29
import javax.servlet.http.HttpServlet;
30
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletResponse;
32
import javax.servlet.http.HttpServletResponseWrapper;
33
34
import org.eclipse.core.runtime.CoreException;
35
import org.eclipse.core.runtime.IConfigurationElement;
36
import org.eclipse.core.runtime.Platform;
37
import org.eclipse.help.internal.webapp.HelpWebappPlugin;
38
import org.eclipse.help.internal.webapp.WebappResources;
39
import org.eclipse.help.internal.webapp.data.UrlUtil;
40
41
/*
42
 * Class is responsible for implementing security protection.  All servlets
43
 * who use the org.eclipse.help.webapp.secureServlet extension point will
44
 * will be processed for security failures by this class.
45
 *
46
 */
47
public class SecureServlet extends HttpServlet {
48
49
	private static final long serialVersionUID = -3783758607845176051L;
50
	private Hashtable servletTable = new Hashtable();
51
	
52
	protected void process(HttpServletRequest req, HttpServletResponse resp)
53
		throws ServletException, IOException{
54
55
		String baseURL = req.getRequestURL().toString();		
56
		baseURL = baseURL.substring(0, baseURL.indexOf(req.getServletPath()));
57
		
58
		Locale locale = UrlUtil.getLocaleObj(req,resp);
59
60
		String service = req.getRequestURL().toString().substring(
61
				(baseURL).length()+("/ss".length())); //$NON-NLS-1$
62
		
63
		try {
64
			HttpServletResponseAdv response = new HttpServletResponseAdv(resp);
65
			
66
			HttpServlet servlet = getServlet(service);
67
			ServletConfig config = getServletConfig();
68
			servlet.init(config);
69
			servlet.service(req, response);
70
			
71
			if (isSecure(req, response))
72
				response.commitOutput();
73
			
74
		} catch(Exception ex) {
75
			
76
			String errorMsg = WebappResources.getString("cantCreateServlet", //$NON-NLS-1$
77
					locale, service);
78
			HelpWebappPlugin.logError(errorMsg, ex);
79
			
80
			PrintWriter writer = resp.getWriter();
81
			writer.println(errorMsg);
82
			ex.printStackTrace(writer);
83
			
84
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
85
		}
86
	}
87
	
88
	private HttpServlet getServlet(String name) throws CoreException
89
	{
90
		HttpServlet servlet = (HttpServlet)servletTable.get(name);
91
		
92
		if (servlet==null)
93
		{
94
			IConfigurationElement[] configs = 
95
				Platform.getExtensionRegistry().getConfigurationElementsFor(HelpWebappPlugin.PLUGIN_ID+".secureServlet"); //$NON-NLS-1$
96
			
97
			for (int c=0;c<configs.length;c++)
98
			{
99
				String alias = configs[c].getAttribute("alias"); //$NON-NLS-1$
100
				if (alias!=null)
101
				{
102
					if (isMatch(alias,name))
103
					{
104
						servlet = (HttpServlet)configs[c].createExecutableExtension("class"); //$NON-NLS-1$
105
						servletTable.put(name, servlet);
106
					}
107
				}
108
			}
109
		}
110
		return servlet;
111
		
112
	}
113
	
114
	private boolean isMatch(String alias,String name)
115
	{
116
		if (alias.indexOf("*")>-1) //$NON-NLS-1$
117
			alias = replaceAll(alias,'*',".*"); //$NON-NLS-1$
118
		
119
		alias+=".*"; //$NON-NLS-1$
120
		
121
		return name.matches(alias);
122
	}
123
124
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
125
			throws ServletException, IOException {
126
		process(req,resp);
127
	}
128
	
129
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
130
			throws ServletException, IOException {
131
		process(req,resp);
132
	}
133
	
134
	public boolean isSecure(HttpServletRequest req,HttpServletResponseAdv resp) throws SecurityException
135
	{
136
		Enumeration names = req.getParameterNames();
137
		List values = new ArrayList();
138
		List scripts = new ArrayList();
139
		
140
		while (names.hasMoreElements())
141
		{
142
			String name = (String)names.nextElement();
143
			String val = req.getParameter(name);
144
			values.add(val);
145
			if (replaceAll(val,'+',"").indexOf("<script")>-1) //$NON-NLS-1$ //$NON-NLS-2$
146
				scripts.add(val);
147
		}
148
		
149
		if(resp.getWriter()!=null)
150
		{
151
			String data = resp.getString();
152
			for (int s=0;s<scripts.size();s++)
153
				if (data.indexOf((String)scripts.get(s))>-1)
154
					throw new SecurityException("Potential cross-site scripting detected."); //$NON-NLS-1$
155
		}
156
		
157
		return true;
158
	}
159
160
	public void isScript(List params,OutputStream out)
161
	{
162
//		ByteArrayOutputStream bOut = new ByteArrayOutputStream(out);
163
	}
164
165
	
166
	public String replaceAll(String str,char remove,String add)
167
	{
168
		StringBuffer buffer = new StringBuffer();
169
		for (int s=0;s<str.length();s++)
170
		{
171
			if (str.charAt(s)==remove)
172
				buffer.append(add);
173
			else
174
				buffer.append(str.charAt(s));
175
		}
176
		return buffer.toString();
177
	}
178
	
179
	private class HttpServletResponseAdv extends HttpServletResponseWrapper
180
	{
181
		private HttpServletResponse response;
182
		private ByteArrayOutputStream out;
183
		private ServletPrintWriter writer;
184
		private SecureServletOutputStream stream;
185
		
186
		public HttpServletResponseAdv(HttpServletResponse response) {
187
			super(response);
188
			out = new ByteArrayOutputStream();
189
			this.response = response;
190
		}
191
192
		public PrintWriter getWriter()
193
		{
194
			if (writer==null && stream==null)
195
				writer = new ServletPrintWriter();
196
			return writer;
197
		}
198
		
199
		public ServletOutputStream getOutputStream()
200
		{
201
			if (stream==null && writer==null)
202
				stream = new SecureServletOutputStream(out);
203
			return stream;
204
		}
205
		
206
		public void commitOutput() throws IOException
207
		{
208
			OutputStream os = response.getOutputStream();
209
			InputStream is = getInputStream();
210
			
211
			int read;
212
			byte buffer[] = new byte[1024*8*8];
213
			
214
			while ((read = is.read(buffer))>-1)
215
				os.write(buffer,0,read);
216
			
217
			os.flush();
218
		}
219
		
220
		public InputStream getInputStream()
221
		{
222
			if (stream!=null)
223
			{
224
				try {
225
						out.flush();
226
				} catch (IOException e) {
227
					e.printStackTrace();
228
				}
229
				return new ByteArrayInputStream(out.toByteArray());
230
			}
231
			if (writer!=null)
232
			{
233
				try {
234
					return new ByteArrayInputStream(writer.toString().getBytes(getCharacterEncoding()));
235
				} catch (UnsupportedEncodingException e) {
236
					// TODO Auto-generated catch block
237
					e.printStackTrace();
238
				}
239
			}
240
			return null;
241
		}
242
		
243
		public String getString()
244
		{
245
			if (writer!=null)
246
				return writer.toString();
247
			
248
			return null;
249
		}
250
	}
251
	
252
	
253
	private class SecureServletOutputStream extends ServletOutputStream{
254
255
		private OutputStream out;
256
257
		public SecureServletOutputStream(OutputStream out)
258
		{
259
			this.out = out;
260
		}
261
		
262
		public void write(int b) throws IOException {
263
			out.write(b);
264
		}
265
		
266
	}
267
}
(-)src/org/eclipse/help/internal/webapp/servlet/ServletPrintWriter.java (+81 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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.ByteArrayOutputStream;
14
import java.io.PrintWriter;
15
16
public class ServletPrintWriter extends PrintWriter{
17
18
	private StringBuffer buffer;
19
	
20
	public ServletPrintWriter() {
21
		super(new ByteArrayOutputStream());
22
		buffer = new StringBuffer();
23
	}
24
	
25
    /**
26
     * Writes a single character.
27
     * @param c int specifying a character to be written.
28
     */
29
    public void write(int c) {
30
	    synchronized (lock) {
31
	    	buffer.append((char)(c));
32
	    }
33
    }
34
35
    /**
36
     * Writes A Portion of an array of characters.
37
     * @param buf Array of characters
38
     * @param off Offset from which to start writing characters
39
     * @param len Number of characters to write
40
     */
41
    public void write(char buf[], int off, int len) {
42
	    synchronized (lock) {
43
	    	buffer.append(buf, off, len);
44
	    }
45
    }
46
47
    /**
48
     * Writes an array of characters.  This method cannot be inherited from the
49
     * Writer class because it must suppress I/O exceptions.
50
     * @param buf Array of characters to be written
51
     */
52
    public void write(char buf[]) {
53
    	write(buf, 0, buf.length);
54
    }
55
56
    /**
57
     * Writes a portion of a string.
58
     * @param s A String
59
     * @param off Offset from which to start writing characters
60
     * @param len Number of characters to write
61
     */
62
    public void write(String s, int off, int len) {
63
	    synchronized (lock) {
64
	    	buffer.append(s.toCharArray(), off, off+len);
65
	    }
66
    }
67
68
    /**
69
     * Writes a string.  This method cannot be inherited from the Writer class
70
     * because it must suppress I/O exceptions.
71
     * @param s String to be written
72
     */
73
    public void write(String s) {
74
	write(s, 0, s.length());
75
    }
76
	
77
    public String toString()
78
    {
79
    	return buffer.toString();
80
    }
81
}
(-)src/org/eclipse/help/internal/webapp/servlet/TocFragmentServlet.java (-2 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2010 IBM Corporation and others.
2
 * Copyright (c) 2006, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 46-51 Link Here
46
47
47
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
48
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
48
			throws ServletException, IOException {
49
			throws ServletException, IOException {
50
		resp.getWriter().write(processRequest(req, resp));
51
	}
52
	
53
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
54
			throws ServletException, IOException {
49
		String locale = UrlUtil.getLocale(req, resp);
55
		String locale = UrlUtil.getLocale(req, resp);
50
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
56
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
51
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
57
		resp.setContentType("application/xml; charset=UTF-8"); //$NON-NLS-1$
Lines 60-66 Link Here
60
		Serializer serializer = new Serializer(data, req.getLocale(), scope);
66
		Serializer serializer = new Serializer(data, req.getLocale(), scope);
61
		String response = serializer.generateTreeXml();	
67
		String response = serializer.generateTreeXml();	
62
		locale2Response.put(locale, response);
68
		locale2Response.put(locale, response);
63
		resp.getWriter().write(response);
69
		
70
		return response;
64
	}
71
	}
65
72
66
	private void readParameters(HttpServletRequest req) {
73
	private void readParameters(HttpServletRequest req) {
(-)src/org/eclipse/help/internal/webapp/servlet/TocServlet.java (-2 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.help.internal.webapp.servlet;
12
package org.eclipse.help.internal.webapp.servlet;
12
13
Lines 44-49 Link Here
44
45
45
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
46
			throws ServletException, IOException {
47
			throws ServletException, IOException {
48
		resp.getWriter().write(processRequest(req, resp));
49
	}
50
	
51
	protected String processRequest(HttpServletRequest req, HttpServletResponse resp)
52
			throws ServletException, IOException {
47
		BaseHelpSystem.checkMode();
53
		BaseHelpSystem.checkMode();
48
		String locale = UrlUtil.getLocale(req, resp);
54
		String locale = UrlUtil.getLocale(req, resp);
49
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
55
		req.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
Lines 63-69 Link Here
63
			}
69
			}
64
			responseByLocale.put(locale, response);
70
			responseByLocale.put(locale, response);
65
		}
71
		}
66
		resp.getWriter().write(response);
72
		
73
		return (response != null) ? response : ""; //$NON-NLS-1$
67
	}
74
	}
68
		
75
		
69
	protected String serialize(TocContribution[] contributions, String locale) throws TransformerException {
76
	protected String serialize(TocContribution[] contributions, String locale) throws TransformerException {
(-)src/org/eclipse/help/internal/webapp/utils/JSonHelper.java (+57 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.utils;
13
14
public final class JSonHelper {
15
	
16
	//JSON Characters
17
	public static final String BEGIN_BRACE = "{"; //$NON-NLS-1$
18
	public static final String END_BRACE  = "}"; //$NON-NLS-1$
19
	public static final String QUOTE  = "\'"; //$NON-NLS-1$
20
	public static final String COLON  = ":"; //$NON-NLS-1$
21
	public static final String BEGIN_BRACKET  = "["; //$NON-NLS-1$
22
	public static final String END_BRACKET  =   "]"; //$NON-NLS-1$
23
	public static final String COMMA  =   ","; //$NON-NLS-1$
24
	public static final String NEWLINE  =   "\n"; //$NON-NLS-1$
25
	public static final String SPACE  =   "   "; //$NON-NLS-1$
26
	
27
	//JSON items
28
	public static final String LABEL			= "label"; //$NON-NLS-1$
29
	public static final String IDENTIFIER		= "identifier"; //$NON-NLS-1$
30
	public static final String URL				= "url"; //$NON-NLS-1$
31
	public static final String PROVIDER			= "provider"; //$NON-NLS-1$
32
	public static final String ITEMS			= "items"; //$NON-NLS-1$
33
	public static final String NAME				= "name"; //$NON-NLS-1$
34
	public static final String TITLE			= "title"; //$NON-NLS-1$
35
	public static final String ID				= "id"; //$NON-NLS-1$
36
	public static final String HREF				= "href"; //$NON-NLS-1$
37
	public static final String TYPE				= "type"; //$NON-NLS-1$
38
	public static final String CHECKED			= "checked"; //$NON-NLS-1$
39
	public static final String CHILDREN			= "children"; //$NON-NLS-1$
40
	public static final String REFERENCE		= "_reference"; //$NON-NLS-1$
41
	public static final String IS_LEAF			= "is_leaf"; //$NON-NLS-1$
42
	public static final String IS_SELECTED		= "is_selected"; //$NON-NLS-1$
43
	public static final String IS_HIGHLIGHTED	= "is_highlighted"; //$NON-NLS-1$
44
	public static final String TOC				= "toc"; //$NON-NLS-1$
45
	public static final String PATH				= "path"; //$NON-NLS-1$
46
	public static final String CATEGORY			= "category"; //$NON-NLS-1$
47
	public static final String DESCRIPTION		= "description"; //$NON-NLS-1$
48
	public static final String CATEGORY_HREF	= CATEGORY+"_"+HREF; //$NON-NLS-1$
49
	public static final String PROPERTY_NAME	= "propertyName"; //$NON-NLS-1$
50
	public static final String INDEX			= "Index"; //$NON-NLS-1$
51
	public static final String TOPIC			= "Topic"; //$NON-NLS-1$
52
	
53
	public static String getQuotes(String str){
54
		return ((str != null)?QUOTE + str + QUOTE:""); //$NON-NLS-1$
55
	}
56
57
}
(-)src/org/eclipse/help/internal/webapp/utils/SearchXMLGenerator.java (+118 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.utils;
13
14
import java.util.Collection;
15
16
import org.eclipse.help.HelpSystem;
17
import org.eclipse.help.IHelpResource;
18
import org.eclipse.help.IToc;
19
import org.eclipse.help.ITopic;
20
import org.eclipse.help.internal.search.SearchHit;
21
import org.eclipse.help.internal.webapp.data.UrlUtil;
22
import org.eclipse.help.internal.webapp.servlet.XMLGenerator;
23
24
public class SearchXMLGenerator  {
25
26
	public static String serialize(Collection results) {
27
		return serialize((results != null) ? results.toArray() : null, false);
28
	}
29
	
30
	public static String serialize(Object[] hits, boolean boolIsCategory) {
31
		StringBuffer buf = new StringBuffer();
32
		buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
33
		buf.append("<searchHits>\n"); //$NON-NLS-1$
34
		
35
		if (hits != null) {
36
			for (int i = 0; i < hits.length; i++) {
37
				SearchHit hit = (SearchHit) hits[i];
38
				serialize(hit, buf, "   ", boolIsCategory); //$NON-NLS-1$
39
			}
40
		}
41
		
42
		buf.append("</searchHits>\n"); //$NON-NLS-1$
43
		
44
		return buf.toString();
45
	}
46
	
47
	private static void serialize(SearchHit hit, StringBuffer buf, 
48
			String indent, boolean boolIsCategory) {
49
		buf.append(indent + "<hit"); //$NON-NLS-1$
50
		if (hit.getHref() != null) {
51
			buf.append('\n' + indent	+ "      href=\"" + XMLGenerator.xmlEscape(hit.getHref()) + '"'); //$NON-NLS-1$
52
		}
53
		if (hit.getLabel() != null) {
54
			buf.append('\n' + indent	+ "      label=\"" + XMLGenerator.xmlEscape(hit.getLabel()) + '"'); //$NON-NLS-1$
55
		}
56
		if (hit.isPotentialHit()) {
57
			buf.append('\n' + indent	+ "      isPotentialHit=\"true\""); //$NON-NLS-1$
58
		}
59
		buf.append('\n' + indent + "      score=\"" + hit.getScore() + '"'); //$NON-NLS-1$
60
		buf.append(">\n"); //$NON-NLS-1$
61
		
62
		// get Category
63
		if (boolIsCategory) {
64
			IHelpResource categoryResource = hit.getCategory();
65
			if (categoryResource != null) {
66
				serializeCategory(categoryResource, buf, indent + "  "); //$NON-NLS-1$
67
			}
68
		}
69
		
70
		// get Summary/Description
71
		String summary = hit.getSummary();
72
		if (summary != null) {
73
			serialize(summary, buf, indent + "   "); //$NON-NLS-1$
74
		}
75
		buf.append(indent + "</hit>\n"); //$NON-NLS-1$
76
	}
77
78
	private static void serialize(String summary, StringBuffer buf, String indent) {
79
		buf.append(indent + "<summary>"); //$NON-NLS-1$
80
		buf.append(XMLGenerator.xmlEscape(summary));
81
		buf.append("</summary>\n"); //$NON-NLS-1$
82
	}
83
	
84
	private static void serializeCategory(IHelpResource categoryResource, 
85
			StringBuffer buf, String indent) {
86
		String category = categoryResource.getLabel();
87
		if (category == null) return;
88
		
89
		buf.append(indent + "<category"); //$NON-NLS-1$
90
		
91
		String catHref = getCategoryHref(categoryResource);
92
		if (catHref != null) {
93
			buf.append('\n' + indent	+ "      href=\""  //$NON-NLS-1$
94
					+ XMLGenerator.xmlEscape(catHref) + '"');
95
		}
96
		
97
		buf.append(">\n"); //$NON-NLS-1$
98
		buf.append(XMLGenerator.xmlEscape(category));
99
		
100
		buf.append("</category>\n"); //$NON-NLS-1$
101
	}
102
	
103
	private static String getCategoryHref(IHelpResource categoryResource) {
104
		String tocHref = categoryResource.getHref();
105
		IToc[] tocs = HelpSystem.getTocs();
106
		for (int j=0;j<tocs.length;++j) {
107
			if (tocHref.equals(tocs[j].getHref())) {
108
				ITopic topic = tocs[j].getTopic(null);
109
				String topicHref = topic.getHref();
110
				if (topicHref != null) {
111
					return UrlUtil.getHelpURL(topicHref);
112
				}
113
				return "../nav/" + j; //$NON-NLS-1$
114
			}
115
		}
116
		return null;
117
	}
118
}
(-)src/org/eclipse/help/internal/webapp/utils/Utils.java (+52 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.utils;
13
14
import java.io.BufferedReader;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
18
import java.io.Reader;
19
import java.io.StringWriter;
20
import java.io.Writer;
21
22
public class Utils {
23
24
	public static final String RETURN_TYPE 			= "returnType"; //$NON-NLS-1$
25
	public static final String NO_CATEGORY 			= "noCategory"; //$NON-NLS-1$
26
27
	// returnType values: xml (default) | json
28
	public static final String XML 					= "xml"; //$NON-NLS-1$
29
	public static final String JSON 				= "json"; //$NON-NLS-1$
30
	
31
	public static String convertStreamToString(InputStream is)
32
			throws IOException {
33
		if (is != null) {
34
			Writer writer = new StringWriter();
35
			char[] buffer = new char[1024];
36
			try {
37
				Reader reader = new BufferedReader(
38
						new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
39
				int n;
40
				while ((n = reader.read(buffer)) != -1) {
41
					writer.write(buffer, 0, n);
42
				}
43
			} finally {
44
				is.close();
45
			}
46
			return writer.toString();
47
		} else {
48
			return ""; //$NON-NLS-1$
49
		}
50
	}
51
52
}
(-)src/org/eclipse/help/internal/webapp/utils/XMLHelper.java (+42 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.help.internal.webapp.utils;
13
14
public final class XMLHelper {
15
16
	public static final String SESSION_URL				= "helpURL"; //$NON-NLS-1$
17
	
18
	public static final String URL36_PREFIX				= "/advanced"; //$NON-NLS-1$
19
	public static final String TOC_URL					= "/tocfragment"; //$NON-NLS-1$
20
	public static final String SEARCH_URL				= "/search"; //$NON-NLS-1$
21
	
22
	public static final String ATTR_TITLE				= "title"; //$NON-NLS-1$
23
	public static final String ATTR_ID					= "id"; //$NON-NLS-1$
24
	public static final String ATTR_HREF				= "href"; //$NON-NLS-1$
25
	public static final String ATTR_IMAGE				= "image"; //$NON-NLS-1$
26
	public static final String ATTR_LABEL				= "label"; //$NON-NLS-1$
27
	public static final String ATTR_SCORE				= "score"; //$NON-NLS-1$
28
	public static final String ATTR_LOCALE				= "locale"; //$NON-NLS-1$
29
	public static final String ATTR_KEYWORD				= "keyword"; //$NON-NLS-1$
30
	
31
	public static final String ELEMENT_NODE				= "node"; //$NON-NLS-1$
32
	public static final String ELEMENT_HIT				= "hit"; //$NON-NLS-1$
33
	public static final String ELEMENT_HITS				= "searchHits"; //$NON-NLS-1$
34
	public static final String ELEMENT_CATEGORY			= "category"; //$NON-NLS-1$
35
	public static final String ELEMENT_SUMMARY			= "summary"; //$NON-NLS-1$
36
	public static final String ELEMENT_DESCRIPTION		= "description"; //$NON-NLS-1$
37
	public static final String ELEMENT_INDEX			= "index"; //$NON-NLS-1$
38
	public static final String ELEMENT_INDEX_CONTRIBUTIONS	= "indexContributions"; //$NON-NLS-1$
39
	public static final String ELEMENT_TOC_CONTRIBUTIONS	= "tocContributions"; //$NON-NLS-1$
40
	
41
	public static final String CATEGORY_HREF			= "categoryHref"; //$NON-NLS-1$
42
}
(-)help/org/eclipse/ua/tests/help/AllHelpTests.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2010 IBM Corporation and others.
2
 * Copyright (c) 2006, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 23-28 Link Here
23
import org.eclipse.ua.tests.help.search.AllSearchTests;
23
import org.eclipse.ua.tests.help.search.AllSearchTests;
24
import org.eclipse.ua.tests.help.toc.AllTocTests;
24
import org.eclipse.ua.tests.help.toc.AllTocTests;
25
import org.eclipse.ua.tests.help.webapp.AllWebappTests;
25
import org.eclipse.ua.tests.help.webapp.AllWebappTests;
26
import org.eclipse.ua.tests.help.webapp.service.AllWebappServiceTests;
26
27
27
/*
28
/*
28
 * Tests help functionality (automated).
29
 * Tests help functionality (automated).
Lines 50-54 Link Here
50
		addTest(AllOtherHelpTests.suite());
51
		addTest(AllOtherHelpTests.suite());
51
		addTest(AllRemoteTests.suite());
52
		addTest(AllRemoteTests.suite());
52
		addTest(AllScopeTests.suite());
53
		addTest(AllScopeTests.suite());
54
		addTest(AllWebappServiceTests.suite());
53
	}
55
	}
54
}
56
}
(-)help/org/eclipse/ua/tests/help/remote/ContextServletTest.java (-3 / +4 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.ua.tests.help.remote;
12
package org.eclipse.ua.tests.help.remote;
12
13
Lines 70-83 Link Here
70
		}
71
		}
71
	}
72
	}
72
73
73
	private Element[] getContextsFromServlet(String phrase)
74
	protected Element[] getContextsFromServlet(String phrase)
74
			throws Exception {
75
			throws Exception {
75
		int port = WebappManager.getPort();
76
		int port = WebappManager.getPort();
76
		URL url = new URL("http", "localhost", port, "/help/context?id=" + URLEncoder.encode(phrase, "UTF-8"));
77
		URL url = new URL("http", "localhost", port, "/help/context?id=" + URLEncoder.encode(phrase, "UTF-8"));
77
		return makeServletCall(url);
78
		return makeServletCall(url);
78
	}
79
	}
79
	
80
	
80
	private Element[] getContextsUsingLocale(String phrase, String locale)
81
	protected Element[] getContextsUsingLocale(String phrase, String locale)
81
			throws Exception {
82
			throws Exception {
82
		int port = WebappManager.getPort();
83
		int port = WebappManager.getPort();
83
		URL url = new URL("http", "localhost", port, "/help/context?id="
84
		URL url = new URL("http", "localhost", port, "/help/context?id="
Lines 85-91 Link Here
85
		return makeServletCall(url);
86
		return makeServletCall(url);
86
	}
87
	}
87
88
88
	private Element[] makeServletCall(URL url) throws IOException,
89
	protected Element[] makeServletCall(URL url) throws IOException,
89
			ParserConfigurationException, FactoryConfigurationError,
90
			ParserConfigurationException, FactoryConfigurationError,
90
			SAXException {
91
			SAXException {
91
		InputStream is = url.openStream();
92
		InputStream is = url.openStream();
(-)help/org/eclipse/ua/tests/help/remote/IndexServletTest.java (-1 / +2 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.ua.tests.help.remote;
12
package org.eclipse.ua.tests.help.remote;
12
13
Lines 146-152 Link Here
146
		}
147
		}
147
	}
148
	}
148
149
149
	private Node getIndexContributions( String locale)
150
	protected Node getIndexContributions( String locale)
150
			throws Exception {
151
			throws Exception {
151
		int port = WebappManager.getPort();
152
		int port = WebappManager.getPort();
152
		URL url = new URL("http", "localhost", port, "/help/index?lang=" + locale);
153
		URL url = new URL("http", "localhost", port, "/help/index?lang=" + locale);
(-)help/org/eclipse/ua/tests/help/remote/SearchServletTest.java (-3 / +4 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.ua.tests.help.remote;
12
package org.eclipse.ua.tests.help.remote;
12
13
Lines 97-110 Link Here
97
		assertEquals(0, hits.length);
98
		assertEquals(0, hits.length);
98
	}
99
	}
99
100
100
	private Node[] getSearchHitsFromServlet(String phrase)
101
	protected Node[] getSearchHitsFromServlet(String phrase)
101
			throws Exception {
102
			throws Exception {
102
		int port = WebappManager.getPort();
103
		int port = WebappManager.getPort();
103
		URL url = new URL("http", "localhost", port, "/help/search?phrase=" + URLEncoder.encode(phrase, "UTF-8"));
104
		URL url = new URL("http", "localhost", port, "/help/search?phrase=" + URLEncoder.encode(phrase, "UTF-8"));
104
		return makeServletCall(url);
105
		return makeServletCall(url);
105
	}
106
	}
106
	
107
	
107
	private Node[] getSearchHitsUsingLocale(String phrase, String locale)
108
	protected Node[] getSearchHitsUsingLocale(String phrase, String locale)
108
			throws Exception {
109
			throws Exception {
109
		int port = WebappManager.getPort();
110
		int port = WebappManager.getPort();
110
		URL url = new URL("http", "localhost", port, "/help/search?phrase="
111
		URL url = new URL("http", "localhost", port, "/help/search?phrase="
Lines 112-118 Link Here
112
		return makeServletCall(url);
113
		return makeServletCall(url);
113
	}
114
	}
114
115
115
	private Node[] makeServletCall(URL url) throws IOException,
116
	protected Node[] makeServletCall(URL url) throws IOException,
116
			ParserConfigurationException, FactoryConfigurationError,
117
			ParserConfigurationException, FactoryConfigurationError,
117
			SAXException {
118
			SAXException {
118
		InputStream is = url.openStream();
119
		InputStream is = url.openStream();
(-)help/org/eclipse/ua/tests/help/remote/TocServletTest.java (-1 / +2 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.ua.tests.help.remote;
12
package org.eclipse.ua.tests.help.remote;
12
13
Lines 110-116 Link Here
110
	}
111
	}
111
	 
112
	 
112
113
113
	private Node getTocContributions( String locale)
114
	protected Node getTocContributions( String locale)
114
			throws Exception {
115
			throws Exception {
115
		int port = WebappManager.getPort();
116
		int port = WebappManager.getPort();
116
		URL url = new URL("http", "localhost", port, "/help/toc?lang=" + locale);
117
		URL url = new URL("http", "localhost", port, "/help/toc?lang=" + locale);
(-)help/org/eclipse/ua/tests/help/util/ServletTester.java (+111 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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.ua.tests.help.util;
12
13
import java.io.BufferedReader;
14
import java.io.IOException;
15
import java.io.InputStreamReader;
16
import java.net.URL;
17
18
import javax.xml.parsers.DocumentBuilder;
19
import javax.xml.parsers.DocumentBuilderFactory;
20
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.MultiStatus;
23
import org.eclipse.core.runtime.Status;
24
import org.xml.sax.ErrorHandler;
25
import org.xml.sax.SAXException;
26
import org.xml.sax.SAXParseException;
27
28
29
public class ServletTester {
30
31
	// dtd validation
32
	// argument processing / validation
33
	// invalid args dont workit
34
	
35
	public static String callTextServlet(URL url) throws IOException
36
	{
37
		BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
38
39
		String res="";
40
		String line;
41
		while ((line = br.readLine())!=null)
42
			res+=line+'\n';
43
		
44
		br.close();
45
		
46
		return res;
47
	}
48
	
49
	public static boolean isXML(URL url) throws IOException
50
	{
51
		return callTextServlet(url).startsWith("<?xml");
52
//		return siteContains(url,"<?xml");
53
	}
54
	
55
	public static boolean siteContains(URL url,String str) throws IOException
56
	{
57
		return (callTextServlet(url).indexOf(str) > -1);
58
	}
59
	
60
	public static boolean isDTDValidated(URL url) throws Exception
61
	{
62
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63
		factory.setValidating(true);
64
		
65
		DocumentBuilder builder = factory.newDocumentBuilder();
66
		
67
		DTDErrorHandler handler = new DTDErrorHandler();
68
		builder.setErrorHandler(handler);
69
		builder.parse(url.toString());
70
		
71
		return handler.getStatus().getSeverity()!=IStatus.ERROR;
72
	}
73
	
74
	
75
	private static class DTDErrorHandler implements ErrorHandler
76
	{
77
		private MultiStatus status = null;
78
		
79
		public void warning(SAXParseException exception) throws SAXException {
80
			if (status==null)
81
				status = new MultiStatus("org.eclipse.ua.tests",IStatus.WARNING,"DTD Processing Warning",exception);
82
			else
83
			{
84
				IStatus tmp = new Status(IStatus.WARNING,"org.eclipse.ua.tests","DTD Processing Warning",exception);
85
				status.add(tmp);
86
			}
87
		}
88
89
		public void error(SAXParseException exception) throws SAXException {
90
			if (status==null)
91
				status = new MultiStatus("org.eclipse.ua.tests",IStatus.ERROR,"DTD Processing Error",exception);
92
			else
93
			{
94
				IStatus tmp = new Status(IStatus.ERROR,"org.eclipse.ua.tests","DTD Processing Error",exception);
95
				status.add(tmp);
96
			}
97
		}
98
99
		public void fatalError(SAXParseException exception) throws SAXException {
100
			error(exception);
101
		}
102
		
103
		public IStatus getStatus()
104
		{
105
			if (status==null)
106
				return Status.OK_STATUS;
107
			return status;
108
		}
109
		
110
	}
111
}
(-)help/org/eclipse/ua/tests/help/webapp/service/AllWebappServiceTests.java (+41 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import junit.framework.Test;
15
import junit.framework.TestSuite;
16
17
/*
18
 * Tests utility classes and servlets used in Web Application
19
 */
20
public class AllWebappServiceTests extends TestSuite {
21
22
	/*
23
	 * Returns the entire test suite.
24
	 */
25
	public static Test suite() {
26
		TestSuite suite = new TestSuite(
27
				"org.eclipse.ua.tests.help.AllWebappServiceTests");
28
		//$JUnit-BEGIN$
29
		suite.addTestSuite(ContentServiceTest.class);
30
		suite.addTestSuite(ContextServiceTest.class);
31
		suite.addTestSuite(IndexFragmentServiceTest.class);
32
		suite.addTestSuite(IndexServiceTest.class);
33
		suite.addTestSuite(SearchServiceTest.class);
34
		suite.addTestSuite(SimpleSearchServiceTest.class);
35
		suite.addTestSuite(TocFragmentServiceTest.class);
36
		suite.addTestSuite(TocServiceTest.class);
37
		//$JUnit-END$
38
		return suite;
39
	}
40
41
}
(-)help/org/eclipse/ua/tests/help/webapp/service/ContentServiceTest.java (+63 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.io.IOException;
15
16
import org.eclipse.ua.tests.help.remote.ContentServletTest;
17
18
public class ContentServiceTest extends ContentServletTest {
19
20
	private static final String UA_TESTS = "org.eclipse.ua.tests";
21
22
	public void testSimpleContent() throws Exception {
23
		final String path = "/data/help/index/topic1.html";
24
		String remoteContent = ServicesTestUtils.getRemoteContent(UA_TESTS, path, "en");
25
		String localContent = ServicesTestUtils.getLocalContent(UA_TESTS, path);	   
26
	    assertEquals(remoteContent, localContent);
27
	}
28
29
	public void testFilteredContent() throws Exception {
30
		final String path = "/data/help/manual/filter.xhtml";
31
		String remoteContent = ServicesTestUtils.getRemoteContent(UA_TESTS, path, "en");
32
		String localContent = ServicesTestUtils.getLocalContent(UA_TESTS, path);	   
33
	    assertEquals(remoteContent, localContent);
34
	}
35
36
	public void testContentInEnLocale() throws Exception {
37
		final String path = "/data/help/search/testnl1.xhtml";
38
		String remoteContent = ServicesTestUtils.getRemoteContent(UA_TESTS, path, "en");
39
		String localContent = ServicesTestUtils.getLocalContent(UA_TESTS, path);	   
40
	    assertEquals(remoteContent, localContent);
41
	}
42
	
43
	public void testContentInDeLocale() throws Exception {
44
		final String path = "/data/help/search/testnl1.xhtml";
45
		String remoteContent = ServicesTestUtils.getRemoteContent(UA_TESTS, path, "de");
46
		String enLocalContent = ServicesTestUtils.getLocalContent(UA_TESTS, path);	   
47
		String deLocalContent = ServicesTestUtils.getLocalContent(UA_TESTS, "/nl/de" + path);	   
48
	    assertEquals(remoteContent, deLocalContent);
49
	    assertFalse(remoteContent.equals(enLocalContent));
50
	}
51
	
52
	public void testRemoteContentNotFound() throws Exception {
53
		try {
54
			ServicesTestUtils.getRemoteContent(UA_TESTS, "/no/such/path.html", "en");
55
			fail("No exception thrown");
56
		} catch (IOException e) {
57
			// Exception caught as expected
58
		}	
59
	}
60
61
	
62
	
63
}
(-)help/org/eclipse/ua/tests/help/webapp/service/ContextServiceTest.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.net.URL;
15
import java.net.URLEncoder;
16
17
import org.eclipse.help.internal.server.WebappManager;
18
import org.eclipse.ua.tests.help.remote.ContextServletTest;
19
import org.w3c.dom.Element;
20
21
public class ContextServiceTest extends ContextServletTest {
22
23
	protected Element[] getContextsFromServlet(String phrase)
24
			throws Exception {
25
		int port = WebappManager.getPort();
26
		URL url = new URL("http", "localhost", port, "/help/ss/service/context?id=" 
27
				+ URLEncoder.encode(phrase, "UTF-8"));
28
		return makeServletCall(url);
29
	}
30
	
31
	protected Element[] getContextsUsingLocale(String phrase, String locale)
32
			throws Exception {
33
		int port = WebappManager.getPort();
34
		URL url = new URL("http", "localhost", port, "/help/ss/service/context?id="
35
				+ URLEncoder.encode(phrase, "UTF-8") + "&lang=" + locale);
36
		return makeServletCall(url);
37
	}
38
39
	public void testContextServiceXMLSchema() 
40
			throws Exception {
41
		int port = WebappManager.getPort();
42
		URL url = new URL("http", "localhost", port, "/help/ss/service/context?id=org.eclipse.ua.tests.test_cheatsheets&lang=en");
43
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/context.xsd");
44
		String schema = schemaUrl.toString();
45
		String uri = url.toString();
46
		String result = SchemaValidator.testXMLSchema(uri, schema);
47
		
48
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
49
	}
50
51
	public void testContextServiceJSONSchema() 
52
			throws Exception {
53
//		fail("Not yet implemented.");
54
	}
55
56
}
(-)help/org/eclipse/ua/tests/help/webapp/service/IndexFragmentServiceTest.java (+83 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
15
import java.io.InputStream;
16
import java.net.URL;
17
18
import javax.xml.parsers.DocumentBuilder;
19
import javax.xml.parsers.DocumentBuilderFactory;
20
21
import junit.framework.TestCase;
22
23
import org.eclipse.help.internal.base.BaseHelpSystem;
24
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
25
import org.eclipse.help.internal.server.WebappManager;
26
import org.w3c.dom.Document;
27
import org.w3c.dom.Node;
28
import org.xml.sax.InputSource;
29
30
public class IndexFragmentServiceTest extends TestCase {
31
32
	private int mode;
33
34
	protected void setUp() throws Exception {
35
		BaseHelpSystem.ensureWebappRunning();
36
		mode = BaseHelpSystem.getMode();
37
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
38
	}
39
	
40
	protected void tearDown() throws Exception {
41
		BaseHelpSystem.setMode(mode);
42
	}
43
44
	public void testIndexServletEn() throws Exception {
45
		getTreeData("en");
46
	}
47
48
	public void testIndexServletDe() throws Exception {
49
		getTreeData("de");
50
	}
51
52
	private void getTreeData(String locale)
53
			throws Exception {
54
		int port = WebappManager.getPort();
55
		URL url = new URL("http", "localhost", port, "/help/ss/service/indexfragment?lang=" + locale);
56
		InputStream is = url.openStream();
57
		InputSource inputSource = new InputSource(is);
58
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
59
		documentBuilder.setEntityResolver(new LocalEntityResolver());
60
		Document document = documentBuilder.parse(inputSource);
61
		Node root = document.getFirstChild();
62
		is.close();
63
		assertEquals("tree_data", root.getNodeName());
64
	}
65
66
	public void testIndexFragmentServiceXMLSchema() 
67
			throws Exception {
68
		int port = WebappManager.getPort();
69
		URL url = new URL("http", "localhost", port, "/help/ss/service/indexfragment?lang=en");
70
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/indexfragment.xsd");
71
		String schema = schemaUrl.toString();
72
		String uri = url.toString();
73
		String result = SchemaValidator.testXMLSchema(uri, schema);
74
		
75
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
76
	}
77
78
	public void testIndexFragmentServiceJSONSchema() 
79
			throws Exception {
80
//		fail("Not yet implemented.");
81
	}
82
83
}
(-)help/org/eclipse/ua/tests/help/webapp/service/IndexServiceTest.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.io.InputStream;
15
import java.net.URL;
16
17
import javax.xml.parsers.DocumentBuilder;
18
import javax.xml.parsers.DocumentBuilderFactory;
19
20
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
21
import org.eclipse.help.internal.server.WebappManager;
22
import org.eclipse.ua.tests.help.remote.IndexServletTest;
23
import org.w3c.dom.Document;
24
import org.w3c.dom.Node;
25
import org.xml.sax.InputSource;
26
27
public class IndexServiceTest extends IndexServletTest {
28
29
	protected Node getIndexContributions( String locale)
30
			throws Exception {
31
		int port = WebappManager.getPort();
32
		URL url = new URL("http", "localhost", port, "/help/ss/service/index?lang=" + locale);
33
		InputStream is = url.openStream();
34
		InputSource inputSource = new InputSource(is);
35
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
36
		documentBuilder.setEntityResolver(new LocalEntityResolver());
37
		Document document = documentBuilder.parse(inputSource);
38
		Node root = document.getFirstChild();
39
		is.close();
40
		assertEquals("indexContributions", root.getNodeName());
41
		return root;
42
	}
43
44
	public void testIndexServiceXMLSchema() 
45
			throws Exception {
46
		int port = WebappManager.getPort();
47
		URL url = new URL("http", "localhost", port, "/help/ss/service/index?lang=en");
48
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/index.xsd");
49
		String schema = schemaUrl.toString();
50
		String uri = url.toString();
51
		String result = SchemaValidator.testXMLSchema(uri, schema);
52
		
53
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
54
	}
55
56
	public void testIndexServiceJSONSchema() 
57
			throws Exception {
58
//		fail("Not yet implemented.");
59
	}
60
61
}
(-)help/org/eclipse/ua/tests/help/webapp/service/SchemaValidator.java (+57 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.io.File;
15
import java.net.URL;
16
17
import javax.xml.transform.Source;
18
import javax.xml.transform.stream.StreamSource;
19
import javax.xml.validation.Schema;
20
import javax.xml.validation.SchemaFactory;
21
import javax.xml.validation.Validator;
22
23
import org.xml.sax.SAXException;
24
25
public class SchemaValidator {
26
27
	public static String testXMLSchema(String uri, String schemaFile) {
28
		String msg = ""; //$NON-NLS-1$
29
		try {
30
			SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); //$NON-NLS-1$
31
			Schema schema = null;
32
			if (schemaFile.startsWith("http") || schemaFile.startsWith("ftp"))
33
				schema = factory.newSchema(new URL(schemaFile));
34
			else
35
				schema = factory.newSchema(new File(schemaFile));
36
			
37
			Validator validator = schema.newValidator();
38
			Source source = new StreamSource(uri);
39
			try {
40
				validator.validate(source);
41
				msg = "valid"; //$NON-NLS-1$
42
			} catch (SAXException ex) {
43
				msg = "not valid. Details: " + ex.getMessage(); //$NON-NLS-1$
44
			}
45
		} catch(Exception e) {
46
			msg = "Exception e: " + e; //$NON-NLS-1$
47
		}
48
		
49
		System.out.println(msg);
50
		return msg;
51
	}
52
	
53
	public static String testJSONSchema(String uri, String schemaFile) {
54
		// TODO: Not yet implemented
55
		return ""; //$NON-NLS-1$
56
	}
57
}
(-)help/org/eclipse/ua/tests/help/webapp/service/SearchServiceTest.java (+70 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
15
import java.net.URL;
16
import java.net.URLEncoder;
17
18
import org.eclipse.help.internal.server.WebappManager;
19
import org.eclipse.ua.tests.help.remote.SearchServletTest;
20
import org.w3c.dom.Node;
21
22
public class SearchServiceTest extends SearchServletTest {
23
24
	protected Node[] getSearchHitsFromServlet(String searchWord)
25
			throws Exception {
26
		int port = WebappManager.getPort();
27
		URL url = new URL("http", "localhost", port, "/help/ss/service/search?searchWord=" + URLEncoder.encode(searchWord, "UTF-8"));
28
		return makeServletCall(url);
29
	}
30
	
31
	protected Node[] getSearchHitsUsingLocale(String searchWord, String locale)
32
			throws Exception {
33
		int port = WebappManager.getPort();
34
		URL url = new URL("http", "localhost", port, "/help/ss/service/search?searchWord="
35
				+ URLEncoder.encode(searchWord, "UTF-8") + "&lang=" + locale);
36
		return makeServletCall(url);
37
	}
38
39
	public void testRemoteSearchXMLSchema() 
40
			throws Exception {
41
		int port = WebappManager.getPort();
42
		URL url = new URL("http", "localhost", port, 
43
				"/help/ss/service/search?searchWord=" + URLEncoder.encode("jehcyqpfjs vkrhjewiwh", "UTF-8"));
44
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/search.xsd");
45
		String schema = schemaUrl.toString();
46
		String uri = url.toString();
47
		String result = SchemaValidator.testXMLSchema(uri, schema);
48
		
49
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
50
	}
51
52
	public void testRemoteSearchXMLSchemaExactMatchFound() 
53
			throws Exception {
54
		int port = WebappManager.getPort();
55
		URL url = new URL("http", "localhost", port, 
56
				"/help/ss/service/search?searchWord=" + URLEncoder.encode("\"jehcyqpfjs vkrhjewiwh\"", "UTF-8"));
57
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/search.xsd");
58
		String schema = schemaUrl.toString();
59
		String uri = url.toString();
60
		String result = SchemaValidator.testXMLSchema(uri, schema);
61
		
62
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
63
	}
64
65
	public void testRemoteSearchJSONSchema() 
66
			throws Exception {
67
//		fail("Not yet implemented.");
68
	}
69
70
}
(-)help/org/eclipse/ua/tests/help/webapp/service/ServicesTestUtils.java (+29 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.net.URL;
15
16
import org.eclipse.help.internal.server.WebappManager;
17
import org.eclipse.ua.tests.help.remote.RemoteTestUtils;
18
19
public class ServicesTestUtils extends RemoteTestUtils {
20
21
	public static String getRemoteContent(String plugin, String path,
22
			String locale) throws Exception {
23
		int port = WebappManager.getPort();
24
		URL url = new URL("http", "localhost", port, "/help/ss/service/rtopic/" + plugin
25
				+ path + "?lang=" + locale);
26
		return readFromURL(url);
27
	}
28
29
}
(-)help/org/eclipse/ua/tests/help/webapp/service/SimpleSearchServiceTest.java (+70 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
15
import java.net.URL;
16
import java.net.URLEncoder;
17
18
import org.eclipse.help.internal.server.WebappManager;
19
import org.eclipse.ua.tests.help.remote.SearchServletTest;
20
import org.w3c.dom.Node;
21
22
public class SimpleSearchServiceTest extends SearchServletTest {
23
24
	protected Node[] getSearchHitsFromServlet(String phrase)
25
			throws Exception {
26
		int port = WebappManager.getPort();
27
		URL url = new URL("http", "localhost", port, "/help/ss/service/simplesearch?phrase=" + URLEncoder.encode(phrase, "UTF-8"));
28
		return makeServletCall(url);
29
	}
30
	
31
	protected Node[] getSearchHitsUsingLocale(String phrase, String locale)
32
			throws Exception {
33
		int port = WebappManager.getPort();
34
		URL url = new URL("http", "localhost", port, "/help/ss/service/simplesearch?phrase="
35
				+ URLEncoder.encode(phrase, "UTF-8") + "&lang=" + locale);
36
		return makeServletCall(url);
37
	}
38
39
	public void testRemoteSimpleSearchXMLSchema() 
40
			throws Exception {
41
		int port = WebappManager.getPort();
42
		URL url = new URL("http", "localhost", port, 
43
				"/help/ss/service/simplesearch?phrase=" + URLEncoder.encode("jehcyqpfjs vkrhjewiwh", "UTF-8"));
44
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/simplesearch.xsd");
45
		String schema = schemaUrl.toString();
46
		String uri = url.toString();
47
		String result = SchemaValidator.testXMLSchema(uri, schema);
48
		
49
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
50
	}
51
52
	public void testRemoteSimpleSearchXMLSchemaExactMatchFound() 
53
			throws Exception {
54
		int port = WebappManager.getPort();
55
		URL url = new URL("http", "localhost", port, 
56
				"/help/ss/service/simplesearch?phrase=" + URLEncoder.encode("\"jehcyqpfjs vkrhjewiwh\"", "UTF-8"));
57
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/simplesearch.xsd");
58
		String schema = schemaUrl.toString();
59
		String uri = url.toString();
60
		String result = SchemaValidator.testXMLSchema(uri, schema);
61
		
62
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
63
	}
64
65
	public void testRemoteSimpleSearchJSONSchema() 
66
			throws Exception {
67
//		fail("Not yet implemented.");
68
	}
69
70
}
(-)help/org/eclipse/ua/tests/help/webapp/service/TocFragmentServiceTest.java (+164 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.io.InputStream;
15
import java.net.URL;
16
import java.util.ArrayList;
17
import java.util.List;
18
19
import javax.xml.parsers.DocumentBuilder;
20
import javax.xml.parsers.DocumentBuilderFactory;
21
22
import junit.framework.TestCase;
23
24
import org.eclipse.help.internal.base.BaseHelpSystem;
25
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
26
import org.eclipse.help.internal.server.WebappManager;
27
import org.w3c.dom.Document;
28
import org.w3c.dom.Element;
29
import org.w3c.dom.Node;
30
import org.w3c.dom.NodeList;
31
import org.xml.sax.InputSource;
32
33
public class TocFragmentServiceTest extends TestCase {
34
35
	private int mode;
36
37
	protected void setUp() throws Exception {
38
		BaseHelpSystem.ensureWebappRunning();
39
		mode = BaseHelpSystem.getMode();
40
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
41
	}
42
	
43
	protected void tearDown() throws Exception {
44
		BaseHelpSystem.setMode(mode);
45
	}
46
47
	public void testTocFragmentServiceContainsUAToc() throws Exception {
48
		int port = WebappManager.getPort();
49
		URL url = new URL("http", "localhost", port, 
50
				"/help/ss/service/tocfragment?lang=en");
51
		Node root = getTreeData(url);
52
		Element[] UARoot = findNodeById(root, 
53
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
54
	    assertEquals(1, UARoot.length);
55
	}
56
57
	public void testTocFragmentServiceContainsFilteredToc() throws Exception {
58
		int port = WebappManager.getPort();
59
		URL url = new URL("http", "localhost", port, 
60
				"/help/ss/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=2");
61
		Node root = getTreeData(url);
62
		Element[] UARoot = findNodeById(root, 
63
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
64
		assertEquals(1, UARoot.length);
65
		Element[] filterNode = findNodeById(UARoot[0], "2");
66
		assertEquals(1, filterNode.length);
67
		Element[] results = findChildren(filterNode[0], "node", "href", 
68
				"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/simple_page.html");
69
		assertEquals(24, results.length);
70
		
71
		results = findChildren(filterNode[0], "node", "href", 
72
		"../topic/org.eclipse.ua.tests/data/help/toc/filteredToc/helpInstalled.html");
73
		assertEquals(1, results.length);
74
	}
75
	
76
	public void testReadEnToc() throws Exception {
77
		int port = WebappManager.getPort();
78
		URL url = new URL("http", "localhost", port, 
79
				"/help/ss/service/tocfragment?lang=en&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=7");
80
		Node root = getTreeData(url);
81
		Element[] UARoot = findNodeById(root, 
82
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
83
		assertEquals(1, UARoot.length);
84
		Element[] searchNode = findNodeById(UARoot[0], "7");
85
		assertEquals(1, searchNode.length);
86
		Element[] topicEn = findChildren(searchNode[0], "node", "href", 
87
				"../topic/org.eclipse.ua.tests/data/help/search/test_en.html");
88
		assertEquals(1, topicEn.length);
89
		Element[] topicDe = findChildren(searchNode[0], "node", "href", 
90
				"../topic/org.eclipse.ua.tests/data/help/search/test_de.html");
91
		assertEquals(0, topicDe.length);
92
	}	
93
	
94
	public void testReadDeToc() throws Exception {
95
		int port = WebappManager.getPort();
96
		URL url = new URL("http", "localhost", port, 
97
				"/help/ss/service/tocfragment?lang=de&toc=/org.eclipse.ua.tests/data/help/toc/root.xml&path=7");
98
		Node root = getTreeData(url);
99
		Element[] UARoot = findNodeById(root, 
100
				"/org.eclipse.ua.tests/data/help/toc/root.xml");
101
		assertEquals(1, UARoot.length);
102
		Element[] searchNode = findNodeById(UARoot[0], "7");
103
		Element[] topicEn = findChildren(searchNode[0], "node", "href", 
104
				"../topic/org.eclipse.ua.tests/data/help/search/test_en.html");
105
		assertEquals(0, topicEn.length);
106
		Element[] topicDe = findChildren(searchNode[0], "node", "href", 
107
				"../topic/org.eclipse.ua.tests/data/help/search/test_de.html");
108
		assertEquals(1, topicDe.length);
109
	}
110
	
111
	private Element[] findNodeById(Node root, String id) {
112
		return findChildren(root, "node", "id", id);
113
	}
114
	
115
	private Element[] findChildren(Node parent, String childKind, 
116
			String attributeName, String attributeValue) {
117
		NodeList nodes = parent.getChildNodes();
118
		List<Node> results = new ArrayList<Node>();
119
		for (int i = 0; i < nodes.getLength(); i++) {
120
			Node next = nodes.item(i);
121
			if (next instanceof Element) {
122
				Element nextElement = (Element)next;
123
				if ( childKind.equals(nextElement.getTagName()) 
124
						&& attributeValue.equals(nextElement.getAttribute(attributeName))) {
125
		
126
				    results.add(next);	
127
				}
128
			}
129
		}
130
		return (Element[]) results.toArray(new Element[results.size()]);
131
	}
132
	 
133
134
	private Node getTreeData(URL url)
135
			throws Exception {
136
		InputStream is = url.openStream();
137
		InputSource inputSource = new InputSource(is);
138
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
139
		documentBuilder.setEntityResolver(new LocalEntityResolver());
140
		Document document = documentBuilder.parse(inputSource);
141
		Node root = document.getFirstChild();
142
		is.close();
143
		assertEquals("tree_data", root.getNodeName());
144
		return root;
145
	}
146
147
	public void testTocFragmentServiceXMLSchema() 
148
			throws Exception {
149
		int port = WebappManager.getPort();
150
		URL url = new URL("http", "localhost", port, "/help/ss/service/tocfragment?lang=en");
151
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/tocfragment.xsd");
152
		String schema = schemaUrl.toString();
153
		String uri = url.toString();
154
		String result = SchemaValidator.testXMLSchema(uri, schema);
155
		
156
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
157
	}
158
	
159
	public void testTocFragmentServiceJSONSchema() 
160
			throws Exception {
161
//		fail("Not yet implemented.");
162
	}
163
164
}
(-)help/org/eclipse/ua/tests/help/webapp/service/TocServiceTest.java (+69 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 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
 *     Snehasish Paul <snehpaul@in.ibm.com> - Eclipse help public API services
11
 *******************************************************************************/
12
package org.eclipse.ua.tests.help.webapp.service;
13
14
import java.io.InputStream;
15
import java.net.URL;
16
17
import javax.xml.parsers.DocumentBuilder;
18
import javax.xml.parsers.DocumentBuilderFactory;
19
20
import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
21
import org.eclipse.help.internal.server.WebappManager;
22
import org.eclipse.ua.tests.help.remote.TocServletTest;
23
import org.w3c.dom.Document;
24
import org.w3c.dom.Node;
25
import org.xml.sax.InputSource;
26
27
public class TocServiceTest extends TocServletTest {
28
29
	protected Node getTocContributions( String locale)
30
			throws Exception {
31
		int port = WebappManager.getPort();
32
		URL url = new URL("http", "localhost", port, "/help/ss/service/toc?lang=" + locale);
33
		InputStream is = url.openStream();
34
		InputSource inputSource = new InputSource(is);
35
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
36
		documentBuilder.setEntityResolver(new LocalEntityResolver());
37
		Document document = documentBuilder.parse(inputSource);
38
		Node root = document.getFirstChild();
39
		is.close();
40
		assertEquals("tocContributions", root.getNodeName());
41
		return root;
42
	}
43
44
	public void testTocServiceXMLSchema() 
45
			throws Exception {
46
		int port = WebappManager.getPort();
47
		URL url = new URL("http", "localhost", port, "/help/ss/service/toc?lang=en");
48
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/xml/toc.xsd");
49
		String schema = schemaUrl.toString();
50
		String uri = url.toString();
51
		String result = SchemaValidator.testXMLSchema(uri, schema);
52
		
53
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);
54
	}
55
	
56
	public void testTocServiceJSONSchema() 
57
			throws Exception {
58
		/*int port = WebappManager.getPort();
59
		URL url = new URL("http", "localhost", port, "/help/ss/service/toc?lang=en");
60
		URL schemaUrl = new URL("http", "localhost", port, "/help/test/schema/json/toc.json");
61
		String schema = schemaUrl.toString();
62
		String uri = url.toString();
63
		String result = SchemaValidator.testJSONSchema(uri, schema);
64
		
65
		assertEquals("URL: \"" + uri + "\" is ", "valid", result);*/
66
//		fail("Not yet implemented.");
67
	}
68
69
}
(-)plugin.xml (+4 lines)
Lines 545-550 Link Here
545
            alias="/titlesearch/script"
545
            alias="/titlesearch/script"
546
            base-name="/script">
546
            base-name="/script">
547
      </resource>
547
      </resource>
548
      <resource
549
            alias="/test"
550
            base-name="/service">
551
      </resource>
548
      <serviceSelector
552
      <serviceSelector
549
            filter="(other.info=org.eclipse.help)">
553
            filter="(other.info=org.eclipse.help)">
550
      </serviceSelector>
554
      </serviceSelector>
(-)service/schema/xml/context.xsd (+22 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
4
	elementFormDefault="qualified">
5
6
	<xs:element name="context">
7
		<xs:complexType mixed="false">
8
			<xs:sequence>
9
				<xs:element name="description" type="xs:string" minOccurs="1" maxOccurs="1" />
10
				<xs:element ref="topic" minOccurs="0" maxOccurs="unbounded" />	
11
			</xs:sequence>
12
			<xs:attribute name="title" type="xs:string" /> <!-- attribute for tree_data -->
13
		</xs:complexType>
14
	</xs:element>
15
	
16
	<xs:element name="topic">
17
		<xs:complexType mixed="true">
18
			<xs:attribute name="label" type="xs:string" /> <!-- attribute for tree_data -->
19
			<xs:attribute name="href" type="xs:string" /> <!-- attribute for tree_data -->
20
		</xs:complexType>
21
	</xs:element>
22
</xs:schema>
(-)service/schema/xml/index.xsd (+66 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
5
<xs:element name="indexContributions">
6
		<xs:complexType mixed="false">
7
			<xs:sequence>
8
				<xs:element ref="indexContribution" minOccurs="0"
9
					maxOccurs="unbounded" />
10
			</xs:sequence>
11
		</xs:complexType>
12
	</xs:element>
13
	
14
	
15
	<xs:element name="indexContribution">
16
		<xs:complexType mixed="false">
17
			<xs:sequence>
18
				<xs:element ref="index" minOccurs="1"	maxOccurs="1" />
19
			</xs:sequence>
20
			<xs:attribute name="id" type="xs:string" use="required"/> <!-- attribute for tree_data -->
21
			<xs:attribute name="locale" type="xs:string" use="required"/> <!-- attribute for tree_data -->
22
		</xs:complexType>
23
	</xs:element>
24
	
25
	<xs:element name="index">
26
		<xs:complexType mixed="false">
27
			<xs:sequence>
28
				<xs:element ref="entry" minOccurs="0" maxOccurs="unbounded" />
29
			</xs:sequence>
30
		</xs:complexType>
31
	</xs:element>
32
	
33
	<xs:element name="entry">
34
		<xs:complexType mixed="false">
35
			<xs:sequence>
36
				<xs:element ref="topic" minOccurs="0" maxOccurs="unbounded" />
37
				<xs:element ref="entry" minOccurs="0" maxOccurs="unbounded" />
38
				<xs:element ref="see" minOccurs="0" maxOccurs="unbounded" />
39
			</xs:sequence>
40
			<xs:attribute name="keyword" type="xs:string" use="required"/> <!-- attribute for tree_data -->
41
		</xs:complexType>
42
	</xs:element>
43
	
44
	<xs:element name="topic">
45
		<xs:complexType mixed="false">
46
			<xs:attribute name="href" type="xs:string" /> <!-- attribute for tree_data -->
47
			<xs:attribute name="title" type="xs:string" /> <!-- attribute for tree_data -->
48
			<xs:attribute name="label" type="xs:string" /> <!-- attribute for tree_data -->			
49
		</xs:complexType>
50
	</xs:element>
51
	
52
	<xs:element name="see">
53
		<xs:complexType mixed="false">
54
			<xs:sequence>
55
				<xs:element ref="subpath" minOccurs="0" maxOccurs="unbounded" />
56
			</xs:sequence>
57
			<xs:attribute name="keyword" type="xs:string" use="required"/> <!-- attribute for tree_data -->
58
		</xs:complexType>
59
	</xs:element>
60
	
61
	<xs:element name="subpath">
62
		<xs:complexType mixed="false">
63
			<xs:attribute name="keyword" type="xs:string" use="required"/> <!-- attribute for tree_data -->
64
		</xs:complexType>
65
	</xs:element>
66
</xs:schema>
(-)service/schema/xml/indexfragment.xsd (+27 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
	<xs:element name="tree_data">
5
		<xs:complexType mixed="false">
6
			<xs:sequence>
7
				<xs:element ref="node" minOccurs="0" maxOccurs="unbounded" />
8
			</xs:sequence>
9
			<xs:attribute name="enableNext" type="xs:boolean" use="required" /> <!-- attribute for tree_data -->
10
			<xs:attribute name="enablePrevious" type="xs:boolean" use="required" /> <!-- attribute for tree_data -->
11
12
		</xs:complexType>
13
	</xs:element>
14
15
	<xs:element name="node" abstract="false" nillable="false">
16
		<xs:complexType mixed="false">
17
			<xs:sequence>
18
				<xs:element ref="node" minOccurs="0" maxOccurs="unbounded" />
19
			</xs:sequence>
20
21
			<xs:attribute name="title" type="xs:string" /> <!-- optional attribute for node -->
22
			<xs:attribute name="id" type="xs:ID" use="required" /> <!-- attribute for node -->
23
			<xs:attribute name="href" type="xs:string"/> <!-- optional attribute for node -->
24
		</xs:complexType>
25
	</xs:element>
26
27
</xs:schema>
(-)service/schema/xml/search.xsd (+33 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
	<xs:element name="searchHits">
5
		<xs:complexType mixed="false">
6
			<xs:sequence>
7
				<xs:element ref="hit" minOccurs="0" maxOccurs="unbounded" />   <!-- hits result could be zero -->
8
			</xs:sequence>
9
		</xs:complexType>
10
	</xs:element>
11
12
	<xs:element name="hit" abstract="false" nillable="false">
13
		<xs:complexType mixed="false">
14
			<xs:sequence>
15
				<xs:element name="category" minOccurs="1" maxOccurs="1">
16
					<xs:complexType>
17
						<xs:simpleContent>
18
							<xs:extension base="xs:string">
19
								<xs:attribute name="href" type="xs:string" use="required" />
20
							</xs:extension>
21
						</xs:simpleContent>
22
					</xs:complexType>
23
				</xs:element>
24
				<xs:element name="summary" type="xs:string" minOccurs="1" maxOccurs="1" />
25
			</xs:sequence>
26
			<xs:attribute name="href" type="xs:string" use="required" /> <!-- attribute for hit -->
27
			<xs:attribute name="label" type="xs:string" use="required" /> <!-- attribute for hit -->
28
			<xs:attribute name="isPotentialHit" type="xs:boolean" />  <!-- optional attribute for hit -->
29
			<xs:attribute name="score" type="xs:double" use="required" /> <!-- attribute for hit -->
30
		</xs:complexType>
31
	</xs:element>
32
33
</xs:schema>
(-)service/schema/xml/simplesearch.xsd (+24 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
	<xs:element name="searchHits">
5
		<xs:complexType mixed="false">
6
			<xs:sequence>
7
				<xs:element ref="hit" minOccurs="0" maxOccurs="unbounded" />   <!-- hits result could be zero -->
8
			</xs:sequence>
9
		</xs:complexType>
10
	</xs:element>
11
12
	<xs:element name="hit" abstract="false" nillable="false">
13
		<xs:complexType mixed="false">
14
			<xs:sequence>
15
				<xs:element name="summary" type="xs:string" minOccurs="1" maxOccurs="1" />
16
			</xs:sequence>
17
			<xs:attribute name="href" type="xs:string" use="required" /> <!-- attribute for hit -->
18
			<xs:attribute name="label" type="xs:string" use="required" /> <!-- attribute for hit -->
19
			<xs:attribute name="isPotentialHit" type="xs:boolean" />  <!-- optional attribute for hit -->
20
			<xs:attribute name="score" type="xs:double" use="required" /> <!-- attribute for hit -->
21
		</xs:complexType>
22
	</xs:element>
23
24
</xs:schema>
(-)service/schema/xml/toc.xsd (+101 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
5
	<xs:element name="tocContributions">
6
		<xs:complexType mixed="false">
7
			<xs:sequence>
8
				<xs:element ref="tocContribution" minOccurs="0"
9
					maxOccurs="unbounded" />
10
			</xs:sequence>
11
		</xs:complexType>
12
	</xs:element>
13
14
15
	<xs:element name="tocContribution " abstract="false"
16
		nillable="false">
17
		<xs:complexType mixed="false">
18
			<xs:sequence>
19
				<xs:element ref="toc" minOccurs="0" maxOccurs="unbounded" />
20
				<xs:element ref="extraDocument" minOccurs="0" maxOccurs="unbounded" />
21
			</xs:sequence>
22
			<xs:attribute name="categoryId" type="xs:string" /> <!-- attribute for tree_data -->
23
			<xs:attribute name="contributorId" type="xs:string" /> <!-- attribute for tree_data -->
24
			<xs:attribute name="id" type="xs:string" use="required" /> <!-- attribute for tree_data -->
25
			<xs:attribute name="locale" type="xs:string" use="required" /> <!-- attribute for tree_data -->
26
			<xs:attribute name="isPrimary" type="xs:boolean" use="required" /> <!-- attribute for tree_data -->
27
		</xs:complexType>
28
	</xs:element>
29
30
	<xs:element name="toc" abstract="false" nillable="false">
31
		<xs:complexType mixed="false">
32
			<xs:sequence>
33
				<xs:element ref="anchor" minOccurs="0" maxOccurs="unbounded" />
34
				<xs:element ref="topic" minOccurs="0" maxOccurs="unbounded" />
35
				<xs:element ref="enablement" minOccurs="0" maxOccurs="1" />
36
			</xs:sequence>
37
			
38
			<xs:attribute name="label" type="xs:string" use="required" /> <!-- optional attribute for node -->
39
			<xs:attribute name="link_to" type="xs:string" /> <!-- attribute for node -->
40
			<xs:attribute name="topic" type="xs:string" /> <!-- optional attribute for node -->
41
		</xs:complexType>
42
		
43
		
44
	</xs:element>
45
46
	<xs:element name="extraDocument" abstract="false" nillable="false">
47
		<xs:complexType mixed="false">
48
			<xs:attribute name="href" type="xs:string" use="required" />
49
		</xs:complexType>
50
	</xs:element>
51
52
	<xs:element name="topic" abstract="false" nillable="false">
53
		<xs:complexType mixed="false">
54
			<xs:sequence>
55
				<xs:element ref="topic" minOccurs="0" maxOccurs="unbounded" />
56
				<xs:element ref="link" minOccurs="0" maxOccurs="unbounded" />
57
				<xs:element ref="enablement" minOccurs="0" maxOccurs="1" />
58
			</xs:sequence>
59
60
			<xs:attribute name="label" type="xs:string" /> <!-- optional attribute for node -->
61
			<xs:attribute name="href" type="xs:string" /> <!-- optional attribute for node -->
62
		</xs:complexType>
63
	</xs:element>
64
65
	<xs:element name="anchor" abstract="false" nillable="false">
66
		<xs:complexType mixed="false">
67
			<xs:attribute name="id" type="xs:string" use="required" /> <!-- optional attribute for node -->
68
		</xs:complexType>
69
	</xs:element>
70
71
	<xs:element name="link" abstract="false" nillable="false">
72
		<xs:complexType mixed="false">
73
			<xs:attribute name="toc" type="xs:string" use="required" /> <!-- optional attribute for node -->
74
		</xs:complexType>
75
	</xs:element>
76
77
	<xs:element name="enablement" abstract="false" nillable="false">
78
		<xs:complexType mixed="false">
79
			<xs:sequence>
80
				<xs:element ref="with" minOccurs="0" maxOccurs="unbounded" />
81
				<xs:element ref="test" minOccurs="0" maxOccurs="unbounded" />
82
			</xs:sequence>
83
		</xs:complexType>
84
	</xs:element>
85
86
	<xs:element name="test" abstract="false" nillable="false">
87
		<xs:complexType mixed="false">
88
			<xs:attribute name="args" type="xs:string" use="required" /> <!-- optional attribute for node -->
89
			<xs:attribute name="property" type="xs:string" use="required" /> <!-- attribute for node -->
90
		</xs:complexType>
91
	</xs:element>
92
93
	<xs:element name="with" abstract="false" nillable="false">
94
		<xs:complexType mixed="false">
95
			<xs:sequence>
96
				<xs:element ref="test" minOccurs="0" maxOccurs="unbounded" />
97
			</xs:sequence>
98
			<xs:attribute name="variable" type="xs:string" /> <!-- optional attribute for node -->
99
		</xs:complexType>
100
	</xs:element>
101
</xs:schema>
(-)service/schema/xml/tocfragment.xsd (+64 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
	elementFormDefault="qualified">
4
	<xs:element name="tree_data">
5
		<xs:complexType mixed="false">
6
			<xs:sequence>
7
				<xs:element name="error" minOccurs="0" />
8
				<xs:element ref="numeric_path" minOccurs="0" maxOccurs="1" />
9
				<xs:element name="node"  minOccurs="0" maxOccurs="unbounded">
10
					<!--
11
						toc node, only difference b/w toc and topic level node is of
12
						is_leaf attrb and order of attrb
13
					-->
14
					<xs:complexType mixed="false">
15
						<xs:sequence>
16
							<xs:element name="node" type="TopicNode" minOccurs="0" maxOccurs="unbounded" />
17
						</xs:sequence>
18
						<xs:attribute name="title" type="xs:string" /> <!-- optional attribute for node -->
19
						<xs:attribute name="id" type="xs:string" use="required" /> <!-- attribute for node -->
20
						<xs:attribute name="href" type="xs:string" use="required" /> <!-- attribute for node -->
21
						<xs:attribute name="openImage" type="xs:string" /> <!-- optional attribute for node -->
22
						<xs:attribute name="closedImage" type="xs:string" />  <!-- optional attribute for node -->
23
						<xs:attribute name="imageAlt" type="xs:string" />  <!-- optional attribute for node -->
24
						<xs:attribute name="image" type="xs:string" default="toc_closed" />  <!-- optional attribute for node -->
25
						<xs:attribute name="is_selected" type="xs:boolean"
26
							default="true" /> <!-- attribute for node -->
27
						<xs:attribute name="is_highlighted" type="xs:boolean"
28
							default="true" /> <!-- attribute for node -->
29
30
					</xs:complexType>
31
				</xs:element>
32
			</xs:sequence>
33
		</xs:complexType>
34
	</xs:element>
35
36
	<xs:element name="numeric_path">
37
		<xs:complexType>
38
			<xs:attribute name="path" type="xs:string" />
39
		</xs:complexType>
40
	</xs:element>
41
42
	
43
		<xs:complexType name="TopicNode" mixed="false">
44
			<xs:sequence>
45
				<xs:element name="node" type="TopicNode" minOccurs="0" maxOccurs="unbounded" />
46
			</xs:sequence>
47
48
			<xs:attribute name="title" type="xs:string" /> <!-- optional attribute for node -->
49
			<xs:attribute name="id" type="xs:string" use="required" /> <!-- attribute for node -->
50
			<xs:attribute name="href" type="xs:string" use="required" /> <!-- attribute for node -->
51
			<xs:attribute name="is_leaf" type="xs:boolean" default="true" /> <!-- attribute for node -->
52
			<xs:attribute name="is_selected" type="xs:boolean"
53
				default="true" /> <!-- attribute for node -->
54
			<xs:attribute name="is_highlighted" type="xs:boolean"
55
				default="true" /> <!-- attribute for node -->
56
			<xs:attribute name="openImage" type="xs:string" /> <!-- optional attribute for node -->
57
			<xs:attribute name="closedImage" type="xs:string" />  <!-- optional attribute for node -->
58
			<xs:attribute name="imageAlt" type="xs:string" />  <!-- optional attribute for node -->
59
			<xs:attribute name="image" type="xs:string" default="toc_closed" />  <!-- optional attribute for node -->
60
61
		</xs:complexType>
62
	
63
64
</xs:schema>

Return to bug 319907