| 1 10 package org.mmbase.servlet; 11 12 import java.util.*; 13 import java.io.*; 14 import javax.servlet.*; 15 import javax.servlet.http.*; 16 17 18 import org.mmbase.util.*; 19 import org.mmbase.module.*; 20 import org.mmbase.module.core.*; 21 import org.mmbase.module.gui.html.*; 22 23 import org.mmbase.util.logging.Logger; 24 import org.mmbase.util.logging.Logging; 25 26 39 public class servscan extends JamesServlet { 40 private static Logger log; 41 42 private static sessionsInterface sessions=null; 44 private scanparser parser; 45 46 public static final String SHTML_CONTENTTYPE = "text/html"; 47 public static final String DEFAULT_CHARSET = "UTF-8"; 49 protected String charSet = DEFAULT_CHARSET; 50 51 54 public void init() throws ServletException { 55 super.init(); 56 String encodingParameter = getInitParameter("encoding"); 57 if (encodingParameter != null) { 58 charSet = encodingParameter; 59 } 60 } 61 62 63 public void setMMBase(MMBase mmb) { 64 super.setMMBase(mmb); 65 66 log = Logging.getLoggerInstance(servscan.class); 68 69 try { 70 MMBaseContext.initHtmlRoot(); 71 } catch (Exception e){ 72 log.error(e); 73 } 74 log.info("Getting scan parser"); 75 parser = (scanparser)getModule("SCANPARSER"); 76 if(parser == null) { 77 String msg = "Module with name 'scanparser' should be active"; 78 log.error(msg); 79 throw new RuntimeException (msg); 80 } 81 sessions = (sessionsInterface)getModule("SESSION"); 82 if(sessions == null) { 83 String msg = "module with name 'sessions' is not active"; 84 log.warn(msg); 85 } 86 } 87 88 92 private String addCharSet(String mimetype) { 93 if (mimetype.equals(SHTML_CONTENTTYPE)) { 94 return mimetype + "; charset=\"" + charSet + "\""; 95 } else { 96 return mimetype; 97 } 98 } 99 100 106 protected String handle_line(String body, sessionInfo session, scanpage sp) throws ParseException { 107 return parser.handle_line(body, session, sp); 108 } 109 110 114 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 115 if (!checkInited(res)) { 116 return; 117 } 118 if (parser == null) { 119 throw new ServletException("No scan parser for request " + req.getRequestURI()); 120 } 121 122 123 incRefCount(req); 124 try { 125 126 128 scanpage sp = new scanpage(this, req, res, sessions); 129 130 if (req.getMethod().equals("POST")) { 132 handlePost(sp, res); 133 } 134 135 PrintWriter out = null; 137 138 do { 139 sp.rstatus = 0; 140 sp.body = parser.getfile(sp.req_line); 141 142 if (log.isDebugEnabled()) { 143 log.trace("body :" + sp.body); 144 } 145 if (!doCrcCheck(sp,res)) { 146 throw new PageCRCException("invalid crc"); 147 } 148 doSecure(sp, res); long stime = handleTime(sp); 150 try { 151 if (handleCache(sp, res, out)) return; 152 } catch (Exception e) { 153 log.error("servscan - something is wrong with scancache: " + e.getClass().getName() + " " + e.getMessage()); 154 log.service(Logging.stackTrace(e)); 155 } 156 157 if (log.isDebugEnabled()) { 158 log.trace("body " + sp.body); 159 } 160 161 if (sp.body != null && !sp.body.equals("")) { 162 sp.body = handle_line(sp.body, sp.session, sp); 164 if (sp.body != null) { 166 if (sp.rstatus == 0) { sp.mimetype = addCharSet(sp.mimetype); 168 res.setContentType(sp.mimetype); 170 if (out == null) { 171 out = res.getWriter(); 172 } 173 out.print(sp.body); 174 handleCacheSave(sp, res); 175 } else if (sp.rstatus == 1) { 176 res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); res.setContentType(addCharSet(sp.mimetype)); 178 if (out == null) { 179 out = res.getWriter(); 180 } 181 res.setHeader("Location", sp.body); 182 } else if (sp.rstatus == 2) { sp.req_line = sp.body; 184 if (sp.req_line.indexOf('\n') !=- 1) { 185 sp.req_line = sp.req_line.substring(0 ,sp.req_line.indexOf('\n')); 186 } 187 } else if (sp.rstatus == 4) { 188 String tmp = req.getHeader("If-Modified-Since:"); 189 if (tmp != null && sp.processor != null) { 190 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.reset(); 192 res.setContentType(addCharSet(sp.mimetype)); 193 if (out == null) { 194 out = res.getWriter(); 195 } 196 } else { 197 setHeaders(sp, res, sp.body,0,0); 199 if (out == null) { 200 out = res.getWriter(); 201 } 202 out.print(sp.body); 203 } 204 } 205 } else { 206 sp.body = "<TITLE>Servscan</TITLE>handle_line returned null<BR>"; 207 setHeaders(sp, res, sp.body,0,0); 209 if (out == null) { 210 out = res.getWriter(); 211 } 212 out.print(sp.body); 213 } 214 } else { 215 break; 216 } 217 218 if (out == null) { 219 out = res.getWriter(); 220 } 221 222 if (stime != -1) { 223 stime = System.currentTimeMillis()-stime; 224 if (log.isDebugEnabled()) { 225 log.debug("service(" + getRequestURL(req) + "): STIME " + stime + "ms " + (stime/1000) + "sec"); 226 } 227 } 228 } while (sp.rstatus == 2); 229 out.flush(); 232 } catch(NotLoggedInException e) { 234 log.debug("service(): page(" + getRequestURL(req) + "): NotLoggedInException: "); 235 } catch(PageCRCException e) { 236 log.warn("service(): page(" + getRequestURL(req) + "): Invalid CRC"); 237 } catch(Exception a) { 238 log.debug("service(): exception on page: " + getRequestURL(req)); 239 a.printStackTrace(); 240 } finally { 241 decRefCount(req); 242 } 243 244 } 245 246 251 private final void setHeaders(scanpage sp, HttpServletResponse res, String len, long lastModDate, long expireDate) { 252 res.reset(); 253 254 res.setContentType(addCharSet(sp.mimetype)); 255 256 257 if (sp.mimetype.equals(SHTML_CONTENTTYPE)) { 260 try { 261 res.setContentLength(len.getBytes(charSet).length); 262 } catch (java.io.UnsupportedEncodingException uee) { 263 } 265 } else { 266 res.setContentLength(len.getBytes().length); 267 } 268 269 270 271 Date lastmod = null; 272 if (lastModDate > 0) { 273 lastmod = new Date(lastModDate); 274 } else { 275 lastmod = new Date(); } 277 Date expire = null; 278 if (expireDate > 0) { 279 expire = new Date(expireDate); 280 } else { 281 expire = new Date(System.currentTimeMillis() - 7200000); 283 } 284 285 String lastmodStr = RFC1123.makeDate(lastmod); 287 String expireStr = RFC1123.makeDate(expire); 288 289 if (log.isDebugEnabled()) { 290 log.debug("Set headers for URL: " + sp.req_line + "\nExpires: " + expireStr + "\nLast-Modified: " + lastmodStr ); 291 } 292 293 res.setHeader("Expires", expireStr); 294 res.setHeader("Last-Modified", lastmodStr); 295 297 301 } 304 305 public String getServletInfo() { 306 return "extended html parser that adds extra html commands and a interface to modules."; 307 } 308 309 void handlePost(scanpage sp, HttpServletResponse res) throws Exception { 310 String rtn, part, part2, finals, tokje, header; 311 Hashtable proc_cmd = new Hashtable(); 312 Hashtable proc_var = new Hashtable(); 313 Object obj; 314 HttpServletRequest req = sp.req; 315 HttpPost poster = new HttpPost(req); 316 sp.poster = poster; 317 String name=null; 318 319 String sec = poster.getPostParameter("SECURE"); 321 322 if (sec != null) { 323 if (log.isDebugEnabled()) log.debug("handlePost(" + sp.getUrl() + "): Secure tag found, displaying username/password window at client-side."); 324 name = getAuthorization(req, res); 325 if (log.isDebugEnabled()) log.debug("handlePost(" + sp.getUrl() + "): getting cookie to check name"); 326 String sname = getCookie(req, res); 327 if (name == null) { 328 log.debug("handlePost(): Warning Username is null"); 329 return; 330 } 331 } 332 333 for (Enumeration t = poster.getPostParameters().keys(); t.hasMoreElements(); ) { 335 obj = t.nextElement(); 336 part = (String )obj; 337 if (part.indexOf("SESSION-") == 0) { 338 if (sp.session != null) { 339 if (poster.checkPostMultiParameter((String )obj)) { 340 sessions.addSetValues(sp.session, part.substring(8), poster.getPostMultiParameter((String )obj)); 342 } else { 343 sessions.setValue(sp.session, part.substring(8), poster.getPostParameter((String )obj)); 344 } 345 } 346 } else if (part.indexOf("ID-") == 0) { 348 if (name == null) { 352 log.debug("handlePost(): Warning Username is null"); 353 return; 354 } 355 if (name != null && name.length() > 1) { 356 358 if (poster.checkPostMultiParameter((String )obj)) { 359 } else { 362 } 364 } 365 } else if (part.indexOf("PRC-CMD-") == 0) { 367 if (poster.checkPostMultiParameter((String )obj)) { 368 proc_cmd.put(part.substring(8), poster.getPostMultiParameter((String )obj)); 369 } else { 370 proc_cmd.put(part.substring(8), poster.getPostParameter((String )obj)); 371 } 372 } else if (part.indexOf("PRC-VAR-") == 0) { 374 if (poster.checkPostMultiParameter((String )obj)) { 375 proc_var.put(part.substring(8), poster.getPostMultiParameter((String )obj)); 376 } else { 377 proc_var.put(part.substring(8), poster.getPostParameter((String )obj)); 378 } 379 } 380 } 381 if (!proc_cmd.isEmpty()) parser.do_proc_input(sp.req_line, poster,proc_var, proc_cmd,sp); 383 } 384 385 386 boolean handleCacheSave(scanpage sp, HttpServletResponse res) { 387 if (sp.wantCache != null) { 388 String req_line = sp.req_line; 389 if (sp.querystring != null) req_line += "?" + sp.querystring; 390 try { 391 parser.scancache.newput(sp.wantCache, res, req_line, sp.body, sp.mimetype); 392 } catch (Exception e) { 393 log.error("servscan - something is wrong with scancache"); 394 } 395 } 396 return true; 397 } 398 399 boolean handleCache(scanpage sp,HttpServletResponse res, PrintWriter out) { 400 String req_line = sp.req_line; 401 if (sp.querystring != null) req_line += "?" + sp.querystring; 402 403 406 409 if (sp.body != null) { 410 411 int start = sp.body.indexOf("<CACHE HENK"); 412 if (start >= 0) { 413 start += 11; 414 int end = sp.body.indexOf(">", start); 415 sp.wantCache ="HENK"; 416 417 String rst = parser.scancache.get(sp.wantCache, req_line, sp.body.substring(start, end + 1), sp); 418 if (log.isDebugEnabled()) { 419 log.debug("handleCache: sp.reload: " + sp.reload); 420 } 421 422 if (rst != null && !sp.reload) { 423 long lastModDate = parser.scancache.getLastModDate(sp.wantCache, req_line); 424 long expireDate = parser.scancache.getExpireDate(sp.wantCache, req_line, sp.body.substring(start, end).trim()); 425 426 setHeaders(sp, res, rst, lastModDate, expireDate); 427 try { 429 if (out == null) { 430 out = res.getWriter(); 431 } 432 out.print(rst); 433 out.flush(); 434 out.close(); 435 } catch (IOException io) { 436 log.error(io); 437 } 438 if (log.isDebugEnabled()) { 439 log.debug("handleCache(): cache.hit(" + req_line + ")"); 440 } 441 return(true); 442 } else { 443 if (log.isDebugEnabled()) { 444 log.debug("handleCache(): cache.miss(" + req_line + ")"); 445 } 446 } 447 } 448 449 if (sp.body.indexOf("<CACHE PAGE>") !=- 1) { 450 451 sp.wantCache="PAGE"; 452 String rst=parser.scancache.get(sp.wantCache, req_line, sp); 453 454 if (log.isDebugEnabled()) { 455 log.debug("handleCache: sp.reload: " + sp.reload); 456 } 457 if (rst != null && !sp.reload) { 458 long lastModDate = parser.scancache.getLastModDate(sp.wantCache, req_line); 459 460 setHeaders(sp, res,rst,lastModDate,0); 461 try { 463 464 if (out == null) { 465 out = res.getWriter(); 466 } 467 out.print(rst); 468 out.flush(); 469 out.close(); 470 } catch (IOException io) { 471 log.error(io); 472 } 473 474 if (log.isDebugEnabled()) { 475 log.debug("handleCache(): cache.hit(" + req_line + ")"); 476 } 477 return true; 478 } else { 479 log.debug("handleCache(): cache.miss(" + req_line + ")"); 480 } 481 } 482 } 483 484 return false; 485 } 486 487 488 private long handleTime(scanpage sp) { 489 if (sp.body != null && sp.body.indexOf("<TIME>") != -1) { 490 return System.currentTimeMillis(); 491 } 492 return -1; 493 } 494 495 496 private boolean doCrcCheck(scanpage sp, HttpServletResponse res) { 497 if (sp.body != null && sp.body.indexOf("<CRC>") != -1) { 498 Vector p = sp.getParamsVector(); 499 String value = null; 500 String checker = null; 501 for (Enumeration t= p.elements(); t.hasMoreElements();) { 502 String part = (String )t.nextElement(); 503 if (!((String )p.lastElement()).equals(part)) { 504 if (value == null) { 505 value = part; 506 } else { 507 value += "+" + part; 508 } 509 } else { 510 checker = part; 511 } 512 } 513 value = sp.req.getRequestURI() + "?" + value; 514 int crc = scanparser.calccrc32(value); 515 String thiscrc = "CRC" + crc; 516 System.out.println("CRC = " + crc); 517 if (checker != null && checker.equals(thiscrc)) { 518 return true; 519 } 520 return false; 521 } 522 return true; 523 } 524 525 private String doSecure(scanpage sp, HttpServletResponse res) throws Exception { 526 String name = null; 527 if (sp.body != null && sp.body.indexOf("<SECURE>") != -1) { 528 if (log.isDebugEnabled()) { 529 log.debug("doSecure(" + sp.getUrl() + "): Secure tag found, calling getAuthorization()..."); 530 } 531 name = getAuthorization(sp.req, res); 532 if (log.isDebugEnabled()) { 533 log.debug("doSecure(" + sp.getUrl() + "): getting cookie from user..."); 534 } 535 String sname = getCookie(sp.req, res); 536 537 if (name == null) { 539 log.warn("doSecure(" + sp.getUrl() + "): WARNING: no username found!"); 540 return null; 541 } 542 } 543 return name; 544 } 545 } 546 | Popular Tags |