1 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.OutputStreamWriter ; 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 import javax.servlet.http.HttpServletRequest ; 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 34 public class DynamicXHTMLFilter implements IFilter { 35 36 private static final String 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>"; private static final String ERROR_PAGE_SUFFIX = "</body>\n</html>"; private static final String CHARSET_UTF8 = "UTF-8"; 40 43 public OutputStream filter(final HttpServletRequest req, final OutputStream out) { 44 final String uri = req.getRequestURI(); 45 if (uri == null || !uri.endsWith("html") && !uri.endsWith("htm")) { return out; 47 } 48 49 53 if ("/rtopic".equals(req.getServletPath())) { return out; 55 } 56 57 61 ByteArrayOutputStream out2 = new ByteArrayOutputStream () { 62 public void close() throws IOException { 63 super.close(); 64 byte[] buf = toByteArray(); 65 ByteArrayInputStream in = new ByteArrayInputStream (buf); 66 67 String locale = UrlUtil.getLocale(req, null); 68 String contextPath = req.getContextPath(); 69 String servletPath = req.getServletPath(); 70 String 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 in2 = DynamicXHTMLProcessor.process(href, in, locale, RemoteHelp.isAllowed()); 80 transferContent(in2, out); 81 } 82 catch (Throwable t) { 83 PrintWriter writer = new PrintWriter (new OutputStreamWriter (out, CHARSET_UTF8)); 84 writer.println(ERROR_PAGE_PREFIX); 85 writer.println("<p>"); writer.println(WebappResources.getString("ProcessingError", req.getLocale())); writer.println("</p>"); writer.println("<pre>"); 90 StringWriter w1 = new StringWriter (); 91 PrintWriter w2 = new PrintWriter (w1); 92 t.printStackTrace(w2); 93 94 writer.println(UrlUtil.htmlEncode(w1.getBuffer().toString())); 95 writer.println("</pre>"); writer.println(ERROR_PAGE_SUFFIX); 97 writer.close(); 98 } 99 } 100 }; 101 return out2; 102 } 103 104 107 private void transferContent(InputStream in, OutputStream out) 108 throws IOException { 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 |