1 28 29 package com.caucho.doc; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.L10N; 33 import com.caucho.vfs.Path; 34 import com.caucho.vfs.ReadStream; 35 import com.caucho.vfs.Vfs; 36 37 import javax.servlet.GenericServlet ; 38 import javax.servlet.ServletConfig ; 39 import javax.servlet.ServletContext ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.ServletRequest ; 42 import javax.servlet.ServletResponse ; 43 import javax.servlet.http.HttpServletRequest ; 44 import java.io.IOException ; 45 import java.io.PrintWriter ; 46 import java.util.logging.Logger ; 47 import java.util.regex.Pattern ; 48 49 53 public class ViewFileServlet extends GenericServlet { 54 static private final Logger log = 55 Logger.getLogger(ViewFileServlet.class.getName()); 56 static final L10N L = new L10N(ViewFileServlet.class); 57 58 static private final String PARAM_CONTEXTPATH = "contextpath"; 59 static private final String PARAM_SERVLETPATH = "servletpath"; 60 static private final String PARAM_FILE = "file"; 61 static private final String PARAM_RE_MARKER = "re-marker"; 62 static private final String PARAM_RE_START = "re-start"; 63 static private final String PARAM_RE_END = "re-end"; 64 65 ServletContext _context; 66 67 public void init(ServletConfig config) 68 throws ServletException 69 { 70 super.init(config); 71 _context = config.getServletContext(); 72 } 73 74 public void service(ServletRequest request, ServletResponse response) 75 throws ServletException , IOException 76 { 77 try { 78 viewFile(response.getWriter(), request); 79 } 80 catch (Exception ex) { 81 throw new ServletException (ex); 82 } 83 } 84 85 private void viewFile(PrintWriter out, ServletRequest request) 86 throws Exception 87 { 88 String file = getFileName(request); 89 Path path = getFilePath(request); 90 91 if (path != null) { 92 String re_mrk_str = request.getParameter(PARAM_RE_MARKER); 93 String re_beg_str = request.getParameter(PARAM_RE_START); 94 String re_end_str = request.getParameter(PARAM_RE_END); 95 96 Pattern re_mrk = re_mrk_str == null || re_mrk_str.length() == 0 ? null : Pattern.compile(re_mrk_str); 97 Pattern re_beg = re_beg_str == null || re_beg_str.length() == 0 ? null : Pattern.compile(re_beg_str); 98 Pattern re_end = re_end_str == null || re_end_str.length() == 0 ? null : Pattern.compile(re_end_str); 99 100 105 106 out.println("<html>"); 107 out.println("<head>"); 108 out.print("<title>"); 109 out.print(file); 110 out.println("</title>"); 111 out.println("<style type='text/css'>"); 112 out.println(" .code-highlight { color: #1764FF; }"); 113 out.println(" .face-xmlelement { color: #003DB8; font-weight: bold }"); 114 out.println("</style>"); 115 out.println("</head>"); 116 out.println("<body bgcolor=white>"); 117 out.print("<code>"); 118 out.print("<b>"); 119 out.print(file); 120 out.print("</b>"); 121 out.print("</code>"); 122 out.println("<p>"); 123 124 ReadStream is; 125 try { 126 is = path.openRead(); 127 } catch (java.io.FileNotFoundException ex) { 128 out.println("<font color='red'><b>File not found " + path.getPath() + "</b></font>"); 129 out.println("</body>"); 130 out.println("</html>"); 131 return; 132 } 133 134 String line; 135 out.print("<pre>"); 136 137 boolean h = false; boolean m = false; 140 while ((line = is.readln()) != null) { 141 if (!m && re_mrk != null && re_mrk.matcher(line).matches()) { 143 out.print("<a name='code-highlight'></a>"); 144 m = true; 145 } 146 147 if (!h && re_beg != null && re_beg.matcher(line).matches()) { 149 h = true; 150 out.print("<b class='code-highlight'>"); 151 if (!m && re_mrk == null) { 152 out.print("<a name='code-highlight'></a>"); 153 m = true; 154 } 155 } 156 157 int l = line.length(); 160 for (int i = 0; i < l; i++) { 161 int ch = line.charAt(i); 162 if (ch == '<') { 163 if (h) out.print("<span class='face-xmlelement'>"); 164 out.print("<"); 165 } else if (ch == '>') { 166 out.print(">"); 167 if (h) out.print("</span>"); 168 } 169 else 170 out.print((char) ch); 171 } 172 out.println(); 173 174 if (h && (re_end == null || (re_end != null && re_end.matcher(line).matches()))) { 176 h = false; 177 out.print("</b>"); 178 } 179 } 180 181 is.close(); 182 if (h) 183 out.print("</b>"); 184 out.println("</pre>"); 185 out.println("</body>"); 186 out.println("</html>"); 187 return; 188 } 189 } 190 191 private String getFileName(ServletRequest request) 192 { 193 String f = request.getParameter(PARAM_FILE); 194 if (f != null && f.length() > 0 && f.indexOf("..") < 0) { 195 return f; 196 } 197 return null; 198 } 199 200 private Path getFilePath(ServletRequest request) 201 { 202 String cp = request.getParameter(PARAM_CONTEXTPATH); 203 String sp = request.getParameter(PARAM_SERVLETPATH); 204 String f = getFileName(request); 205 206 Path pwd = Vfs.lookup().createRoot(); 207 208 if (f != null) { 209 ServletContext ctx = _context; 210 211 String requestContext = ((HttpServletRequest ) request).getContextPath(); 212 213 if (cp != null && cp.startsWith(requestContext)) 214 cp = cp.substring(requestContext.length()); 215 216 CharBuffer cb = new CharBuffer(); 217 218 if (cp != null) 219 cb.append(cp); 220 221 cb.append('/'); 222 cb.append(f); 223 224 return pwd.lookup(cb.toString()); 226 } 227 228 return null; 229 } 230 231 } 232 233 | Popular Tags |