KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.webapp.servlet;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.OutputStreamWriter JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.eclipse.help.internal.base.remote.RemoteHelp;
25 import org.eclipse.help.internal.webapp.WebappResources;
26 import org.eclipse.help.internal.webapp.data.UrlUtil;
27 import org.eclipse.help.internal.xhtml.DynamicXHTMLProcessor;
28
29 /*
30  * A filter that performs any required XHTML processing, if the content is
31  * XHTML. Using a servlet path of /rtopic (raw topic) will turn off this
32  * filter.
33  */

34 public class DynamicXHTMLFilter implements IFilter {
35
36     private static final String JavaDoc ERROR_PAGE_PREFIX = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n</head>\n<body>"; //$NON-NLS-1$
37
private static final String JavaDoc ERROR_PAGE_SUFFIX = "</body>\n</html>"; //$NON-NLS-1$
38
private static final String JavaDoc CHARSET_UTF8 = "UTF-8"; //$NON-NLS-1$
39

40     /* (non-Javadoc)
41      * @see org.eclipse.help.internal.webapp.servlet.IFilter#filter(javax.servlet.http.HttpServletRequest, java.io.OutputStream)
42      */

43     public OutputStream JavaDoc filter(final HttpServletRequest JavaDoc req, final OutputStream JavaDoc out) {
44         final String JavaDoc uri = req.getRequestURI();
45         if (uri == null || !uri.endsWith("html") && !uri.endsWith("htm")) { //$NON-NLS-1$ //$NON-NLS-2$
46
return out;
47         }
48         
49         /*
50          * Remote help does all XHTML processing on the client side, so we
51          * want raw XHTML topics from the remote help server.
52          */

53         if ("/rtopic".equals(req.getServletPath())) { //$NON-NLS-1$
54
return out;
55         }
56
57         /*
58          * Buffers the contents of the document until stream is closed, where
59          * the document is processed then pushed through the filter.
60          */

61         ByteArrayOutputStream JavaDoc out2 = new ByteArrayOutputStream JavaDoc() {
62             public void close() throws IOException JavaDoc {
63                 super.close();
64                 byte[] buf = toByteArray();
65                 ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buf);
66                 
67                 String JavaDoc locale = UrlUtil.getLocale(req, null);
68                 String JavaDoc contextPath = req.getContextPath();
69                 String JavaDoc servletPath = req.getServletPath();
70                 String JavaDoc href = uri;
71                 if (href.startsWith(contextPath)) {
72                     href = href.substring(contextPath.length());
73                 }
74                 if (href.startsWith(servletPath)) {
75                     href = href.substring(servletPath.length());
76                 }
77                 
78                 try {
79                     InputStream JavaDoc in2 = DynamicXHTMLProcessor.process(href, in, locale, RemoteHelp.isAllowed());
80                     transferContent(in2, out);
81                 }
82                 catch (Throwable JavaDoc t) {
83                     PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(out, CHARSET_UTF8));
84                     writer.println(ERROR_PAGE_PREFIX);
85                     writer.println("<p>"); //$NON-NLS-1$
86
writer.println(WebappResources.getString("ProcessingError", req.getLocale())); //$NON-NLS-1$
87
writer.println("</p>"); //$NON-NLS-1$
88
writer.println("<pre>"); //$NON-NLS-1$
89

90                     StringWriter JavaDoc w1 = new StringWriter JavaDoc();
91                     PrintWriter JavaDoc w2 = new PrintWriter JavaDoc(w1);
92                     t.printStackTrace(w2);
93                     
94                     writer.println(UrlUtil.htmlEncode(w1.getBuffer().toString()));
95                     writer.println("</pre>"); //$NON-NLS-1$
96
writer.println(ERROR_PAGE_SUFFIX);
97                     writer.close();
98                 }
99             }
100         };
101         return out2;
102     }
103
104     /*
105      * Forward the contents of the input stream to the output stream.
106      */

107     private void transferContent(InputStream JavaDoc in, OutputStream JavaDoc out)
108             throws IOException JavaDoc {
109         byte[] buffer = new byte[4096];
110         int len = 0;
111         while (true) {
112             len = in.read(buffer);
113             if (len == -1)
114                 break;
115             out.write(buffer, 0, len);
116         }
117     }
118 }
119
Popular Tags