1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.OutputStream ; 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 import java.io.Writer ; 21 import java.lang.reflect.UndeclaredThrowableException ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 import java.util.Enumeration ; 25 import java.util.Locale ; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.eclipse.help.internal.base.BaseHelpSystem; 32 import org.eclipse.help.internal.protocols.HelpURLConnection; 33 import org.eclipse.help.internal.protocols.HelpURLStreamHandler; 34 import org.eclipse.help.internal.webapp.HelpWebappPlugin; 35 import org.eclipse.help.internal.webapp.data.ServletResources; 36 import org.eclipse.help.internal.webapp.data.UrlUtil; 37 38 41 public class EclipseConnector { 42 private static final String errorPageBegin = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" + "<html><head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" + "</head>\n" + "<body><p>\n"; private static final String errorPageEnd = "</p></body></html>"; private static final IFilter filters[] = new IFilter[]{ 49 new HighlightFilter(), new FramesetFilter(), new InjectionFilter(), new DynamicXHTMLFilter(), new BreadcrumbsFilter() }; 50 private ServletContext context; 51 52 public EclipseConnector(ServletContext context) { 53 this.context= context; 54 } 55 56 public void transfer(HttpServletRequest req, HttpServletResponse resp) 57 throws IOException { 58 try { 59 String url = getURL(req); 60 if (url == null) 61 return; 62 int index = url.lastIndexOf(HelpURLConnection.PLUGINS_ROOT); 64 if (index!= -1 && url.indexOf("content/" + HelpURLConnection.PLUGINS_ROOT) == -1) { StringBuffer redirectURL = new StringBuffer (); 66 67 redirectURL.append(req.getContextPath()); 68 redirectURL.append(req.getServletPath()); 69 redirectURL.append("/"); redirectURL.append(url.substring(index+HelpURLConnection.PLUGINS_ROOT.length())); 71 72 resp.sendRedirect(redirectURL.toString()); 73 return; 74 } 75 if (url.toLowerCase(Locale.ENGLISH).startsWith("file:") || url.toLowerCase(Locale.ENGLISH).startsWith("jar:") || url.toLowerCase(Locale.ENGLISH).startsWith("platform:")) { int i = url.indexOf('?'); 79 if (i != -1) 80 url = url.substring(0, i); 81 if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_INFOCENTER 83 || !UrlUtil.isLocalRequest(req)) { 84 return; 85 } 86 } else { 87 90 url = "help:" + url; } 92 93 URLConnection con = openConnection(url, req, resp); 94 String contentType = con.getContentType(); 95 if ("text/plain".equals(contentType)) { String pathInfo = req.getPathInfo(); 99 String mimeType = context.getMimeType(pathInfo); 100 if (mimeType != null) { 101 contentType = mimeType; 102 } 103 } 104 resp.setContentType(contentType); 105 106 long maxAge = 0; 107 try { 108 long expiration = con.getExpiration(); 111 maxAge = (expiration - System.currentTimeMillis()) / 1000; 112 if (maxAge < 0) 113 maxAge = 0; 114 } catch (Exception e) { 115 } 116 resp.setHeader("Cache-Control", "max-age=" + maxAge); 118 InputStream is; 119 try { 120 is = con.getInputStream(); 121 } catch (IOException ioe) { 122 if (url.toLowerCase(Locale.ENGLISH).endsWith("htm") || url.toLowerCase(Locale.ENGLISH).endsWith("html")) { String error = errorPageBegin 125 + ServletResources.getString("noTopic", req) + errorPageEnd; 127 is = new ByteArrayInputStream (error.getBytes("UTF8")); } else { 129 return; 130 } 131 } 132 catch (Exception e) { 133 Throwable t = e; 135 if (t instanceof UndeclaredThrowableException && t.getCause() != null) { 136 t = t.getCause(); 137 } 138 139 StringBuffer message = new StringBuffer (); 140 message.append(errorPageBegin); 141 message.append("<p>"); message.append(ServletResources.getString( 143 "contentProducerException", req)); 145 message.append("</p>"); message.append("<pre>"); Writer writer = new StringWriter (); 148 t.printStackTrace(new PrintWriter (writer)); 149 message.append(writer.toString()); 150 message.append("</pre>"); message.append(errorPageEnd); 152 153 is = new ByteArrayInputStream (message.toString().getBytes("UTF8")); } 155 156 OutputStream out = resp.getOutputStream(); 157 for (int i = 0; i < filters.length; i++) { 158 out = filters[i].filter(req, out); 159 } 160 161 transferContent(is, out); 162 out.close(); 163 is.close(); 164 165 } catch (Exception e) { 166 String msg = "Error processing help request " + getURL(req); HelpWebappPlugin.logError(msg, e); 168 } 169 } 170 171 174 private void transferContent(InputStream inputStream, OutputStream out) 175 throws IOException { 176 try { 177 BufferedInputStream dataStream = new BufferedInputStream ( 179 inputStream); 180 181 byte[] buffer = new byte[4096]; 184 int len = 0; 185 while (true) { 186 len = dataStream.read(buffer); if (len == -1) 188 break; 189 out.write(buffer, 0, len); 190 } 191 } catch (Exception e) { 192 } 193 } 194 195 198 private URLConnection openConnection(String url, 199 HttpServletRequest request, HttpServletResponse response) 200 throws Exception { 201 202 URLConnection con = null; 203 if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_INFOCENTER) { 204 String locale = UrlUtil.getLocale(request, response); 206 if (url.indexOf('?') >= 0) { 207 url = url + "&lang=" + locale; } else { 209 url = url + "?lang=" + locale; } 211 } 212 URL helpURL; 213 if (url.startsWith("help:")) { helpURL = new URL ("help", null, -1, url.substring("help:".length()), HelpURLStreamHandler.getDefault()); 217 } else { 218 if (url.startsWith("jar:")) { int excl = url.indexOf("!/"); String jar = url.substring(0, excl); 222 String path = url.length() > excl + 2 ? url.substring(excl + 2) 223 : ""; url = jar.replaceAll("!", "%21") + "!/" + path.replaceAll("!", "%21"); } 227 helpURL = new URL (url); 228 } 229 String protocol = helpURL.getProtocol(); 230 if (!("help".equals(protocol) || "file".equals(protocol) || "platform".equals(protocol) || "jar".equals(protocol))) { throw new IOException (); 235 } 236 237 con = helpURL.openConnection(); 238 239 con.setAllowUserInteraction(false); 240 con.setDoInput(true); 241 con.connect(); 242 return con; 243 } 244 245 248 private String getURL(HttpServletRequest req) { 249 String query = ""; boolean firstParam = true; 251 for (Enumeration params = req.getParameterNames(); params 252 .hasMoreElements();) { 253 String param = (String ) params.nextElement(); 254 String [] values = req.getParameterValues(param); 255 if (values == null) 256 continue; 257 for (int i = 0; i < values.length; i++) { 258 if (firstParam) { 259 query += "?" + param + "=" + values[i]; firstParam = false; 261 } else 262 query += "&" + param + "=" + values[i]; } 264 } 265 266 String url = req.getPathInfo() + query; 268 if (url.startsWith("/")) url = url.substring(1); 270 return url; 271 } 272 } 273 | Popular Tags |