1 22 23 package org.cofax; 24 25 import java.util.*; 26 import javax.servlet.http.*; 27 28 39 40 public final class CofaxPage { 41 42 43 private StringBuffer contents = new StringBuffer (); 44 45 public byte[] contentBytes = null; 46 47 48 private HashMap headers = new HashMap(); 49 50 51 private String pageId; 52 53 54 private boolean cacheThisPage = true; 55 56 57 private String errorMsg = ""; 58 59 60 private long lastModified; 61 62 63 private long cachedTime; 64 65 66 private long startBuildTime; 67 68 69 private long endBuildTime; 70 71 72 private boolean isCached; 73 74 78 private int hitCount = 0; 79 80 84 private int status; 85 86 87 private Glossary glossary; 88 89 90 private TemplateLoader templateLoader; 91 92 93 private DataStore dataStore; 94 95 96 private String servingPage; 97 98 99 private String servingClient; 100 101 private final float SECOND = 1000; 102 103 private final float MINUTE = 60 * SECOND; 104 105 private final float HOUR = 60 * MINUTE; 106 107 private long Start; 108 109 114 public CofaxPage() { 115 contents.setLength(0); 117 118 Start = System.currentTimeMillis(); 119 status = HttpServletResponse.SC_OK; 120 headers.put("Content-Type", "text/html"); 121 } 122 123 129 public void setDataStore(DataStore dataStore) { 130 this.dataStore = dataStore; 131 } 132 133 139 public DataStore getDataStore() { 140 if ((dataStore == null) || (!dataStore.isConnected())) { 141 dataStore.connect(); 142 } 143 return dataStore; 144 } 145 146 149 public void disconnectDataStore() { 150 dataStore.disConnect(); 151 } 152 153 159 public void setTemplateLoader(TemplateLoader templateLoader) { 160 this.templateLoader = templateLoader; 161 } 162 163 168 public TemplateLoader getTemplateLoader() { 169 return templateLoader; 170 } 171 172 178 public void setGlossary(Glossary glossary) { 179 this.glossary = glossary; 180 } 181 182 187 public Glossary getGlossary() { 188 return glossary; 189 } 190 191 197 public void addToGlossary(HashMap newGlossaryValues) { 198 glossary.addToGlossary(newGlossaryValues); 199 return; 200 } 201 202 210 public void putGlossaryValue(String key, String value) { 211 glossary.putString(key, value); 212 } 213 214 221 public String getGlossaryValue(String key) { 222 return glossary.getString(key); 223 } 224 225 234 public String applyGlossary(String inString) { 235 return glossary.applyGlossary(inString) + ""; 236 } 237 238 249 public static String applyGlossaryWithHashMap(String inString, HashMap params) { 250 Glossary tmpGlossary = new Glossary(); 251 tmpGlossary.setKeyValues(params); 252 return tmpGlossary.applyGlossary(inString) + ""; 253 } 254 255 260 public String printGlossary() { 261 return glossary.printGlossary(); 262 } 263 264 271 public String printGlossary(String startWithName) { 272 return glossary.printGlossary(startWithName); 273 } 274 275 283 public void putHeader(String headerName, String headerValue) { 284 headers.put(headerName, headerValue + ""); 285 } 286 287 294 public String getHeader(String headerName) { 295 return headers.get(headerName) + ""; 296 } 297 298 303 public HashMap getHeaders() { 304 return headers; 305 } 306 307 314 public boolean doesHeaderExist(String headerName) { 315 if (headers.containsKey(headerName)) { 316 return true; 317 } else { 318 return false; 319 } 320 } 321 322 328 public void setLastModified(long theTime) { 329 lastModified = theTime; 330 } 331 332 337 public long getLastModified() { 338 return lastModified; 339 } 340 341 348 public String toString() { 349 return contents.toString(); 350 } 351 352 357 public String getContents() { 358 return contents.toString(); 359 } 360 361 366 public int getContentsLength() { 367 return contents.length(); 368 } 369 370 376 public void append(String s) { 377 contents.append(s); 378 } 379 380 386 public void setStatus(int status) { 387 this.status = status; 388 } 389 390 395 public int getStatus() { 396 return status; 397 } 398 399 405 public void setErrorMsg(String errorMsg) { 406 this.errorMsg = this.errorMsg + "\n" + errorMsg; 407 } 408 409 414 public String getErrorMsg() { 415 return errorMsg; 416 } 417 418 421 public void reset() { 422 contents.setLength(0); 423 contents = new StringBuffer (); contentBytes = null; } 426 427 430 public void bytes() { 431 contentBytes = contents.toString().getBytes(); 432 contents.setLength(0); 433 } 434 435 442 public boolean setHitCount(int value) { 443 this.hitCount = value; 444 return true; 445 } 446 447 452 public int getHitCount() { 453 return hitCount; 454 } 455 456 463 public boolean setPageCached(boolean status) { 464 this.isCached = status; 465 return true; 466 } 467 468 476 public boolean isPageCached() { 477 return this.isCached; 478 } 479 480 487 public boolean setCachedTime(long time) { 488 this.cachedTime = time; 489 return true; 490 } 491 492 495 public void incrementHitCount() { 496 this.hitCount++; 497 } 498 499 public String cacheTime(long requestStart) { 500 String procTime = processTime(this.endBuildTime - requestStart); 501 String buildString = "<!-- Served from Cache in : " + procTime + " -->\r\n"; 502 return buildString; 503 } 504 505 public String buildTime(long requestStart) { 506 String procTime = processTime(this.endBuildTime - requestStart); 507 String buildString = "<!-- Page Built in : " + procTime + " -->\r\n"; 508 return buildString; 509 } 510 511 public String pageAge() { 512 long End = System.currentTimeMillis(); 513 String procTime = processTime(End - Start); 514 String ageString = "<!-- Page Built : " + procTime + " ago -->\r\n"; 515 return ageString; 516 } 517 518 523 524 public void setStartBuildTime() { 525 this.startBuildTime = System.currentTimeMillis(); 526 return; 527 } 528 529 public void setEndBuildTime() { 530 this.endBuildTime = System.currentTimeMillis(); 531 return; 532 } 533 534 539 public long getStartBuildTime() { 540 541 return this.startBuildTime; 542 } 543 544 public long getEndBuildTime() { 545 546 return this.endBuildTime; 547 } 548 549 555 public void setServingPage(String inServingPage) { 556 servingPage = inServingPage; 557 } 558 559 564 public String getServingPage() { 565 return servingPage; 566 } 567 568 574 public void setServingClient(String inServingClient) { 575 servingClient = inServingClient; 576 } 577 578 583 public String getServingClient() { 584 return servingClient; 585 } 586 587 public String processTime(long time) { 588 StringBuffer timeString = new StringBuffer (); 589 590 if (time <= SECOND) { 591 timeString.append(time); 592 timeString.append(" ms"); 593 } else if (time <= MINUTE) { 594 timeString.append(time / SECOND); 595 timeString.append(" secs"); 596 } else if (time <= HOUR) { 597 timeString.append(time / MINUTE); 598 timeString.append(" mins"); 599 } else { 600 timeString.append(time / HOUR); 601 timeString.append(" hours"); 602 } 603 604 return timeString.toString(); 605 } 606 607 public void putInCache(boolean c) { 609 this.cacheThisPage = c; 610 } 611 612 public boolean mustBeCached() { 613 return this.cacheThisPage; 614 } 615 616 public void setPageId(String pid) { 618 pageId = pid; 619 } 620 621 public String getPageId() { 622 return pageId; 623 } 624 } 625 627 | Popular Tags |