  private void writeResponse(HttpServletResponse resp, Result rs, byte[] data, String query, String d, int num)
      throws IOException {
    HttpResponseRecord httpResponseRecord = new HttpResponseRecord(data);
    if (httpResponseRecord.getType().startsWith("text/plain") || !httpResponseRecord.getType().startsWith("text")) {
      resp.setHeader("Content-Type", httpResponseRecord.getType());
      resp.setContentLength(httpResponseRecord.getBodyByte().length);
      resp.getOutputStream().write(httpResponseRecord.getBodyByte());
    } else {
      ArrayList<String> dates = new ArrayList<String>(10);
      for (int i = 0; i < rs.raw().length; i++)
        dates.add(new String(rs.raw()[i].getQualifier()));
      Collections.sort(dates);
      String prevDate = null, nextDate = null;
      for (int i = 1; i < dates.size(); i++){
        if (dates.get(i).compareTo(d) == 0){
          prevDate = dates.get(i - 1);
          if (i + 1 < dates.size())
            nextDate = dates.get(i + 1);
          else
            nextDate = d;
          break;
        }
        if (dates.get(i).compareTo(d) > 0) {
          if ( i > 2)
            prevDate = dates.get(i - 2);
          else
            prevDate = dates.get(i - 1);
          nextDate = dates.get(i);
          break;
        }
      }
      System.setProperty("file.encoding", "UTF8");
      resp.setHeader("Content-Type", httpResponseRecord.getType());
      resp.setCharacterEncoding("UTF-8");
      PrintWriter out = resp.getWriter();
      TextDocument2 t2 = new TextDocument2(null, null, null);
      String bodyContent = new String(httpResponseRecord.getBodyByte(), "UTF8");
      Document doc = Jsoup.parse(bodyContent);
      Element head = doc.select("head").first();
      Element base = doc.select("base").first();
      if(base == null){
        head.prepend("<base id='warcbase-base-added' href='" + query +"'>");
      }
      bodyContent = doc.html();
      bodyContent = t2.fixURLs(bodyContent, query, d);
      doc = Jsoup.parse(bodyContent);
      base = doc.select("base").first();
      if(base != null){
        if(base.attr("id").equals("warcbase-base-added"))
          base.remove();
      }
      head = doc.select("head").first();
      String baseUrl = TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + d + "&query=" + query;
      head.prepend("<script type=\"text/javascript\"> function initYTVideo(id) {  _wmVideos_.init('/web/', id); } </script>  <script> function $(a){return document.getElementById(a)};     function addLoadEvent(a){if(window.addEventListener)addEventListener('load',a,false);else if(window.attachEvent)attachEvent('onload',a)} </script>");
      Element body = doc.select("body").first();
      body.prepend("<div id=\"wm-ipp\" style=\"display: block; position: relative; padding: 0px 5px; min-height: 70px; min-width: 800px; z-index: 9000;\"><div id=\"wm-ipp-inside\" style=\"position:fixed;padding:0!important;margin:0!important;width:97%;min-width:780px;border:5px solid #000;border-top:none;background-image:url(" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_bk_trns.png);text-align:center;-moz-box-shadow:1px 1px 3px #333;-webkit-box-shadow:1px 1px 3px #333;box-shadow:1px 1px 3px #333;font-size:11px!important;font-family:'Lucida Grande','Arial',sans-serif!important;\">    <table style=\"border-collapse:collapse;margin:0;padding:0;width:100%;\"><tbody><tr>    <td style=\"padding:10px;vertical-align:top;min-width:110px;\">    <a href=\""
          + TextDocument2.SERVER_PREFIX + "warcbase"
          + "\" title=\"Warcbase home page\" style=\"background-color:transparent;border:none;\">Warcbase</a>    </td>            <td style=\"padding:0!important;text-align:center;vertical-align:top;width:100%;\">         <table style=\"border-collapse:collapse;margin:0 auto;padding:0;width:570px;\"><tbody><tr>        <td style=\"padding:3px 0;\" colspan=\"2\">        <form target=\"_top\" method=\"get\" action=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "servlet\" name=\"wmtb\" id=\"wmtb\" style=\"margin:0!important;padding:0!important;\"><input name=\"query\" id=\"wmtbURL\" value=\""
              + query
              + "\" style=\"width:400px;font-size:11px;font-family:'Lucida Grande','Arial',sans-serif;\" onfocus=\"javascript:this.focus();this.select();\" type=\"text\"><input name=\"date\" value=\"\" type=\"hidden\"><input name=\"type\" value=\"replay\" type=\"hidden\"><input name=\"date\" value=\"20120201185436\" type=\"hidden\"><input value=\"Go\" style=\"font-size:11px;font-family:'Lucida Grande','Arial',sans-serif;margin-left:5px;\" type=\"submit\"><span id=\"wm_tb_options\" style=\"display:block;\"></span></form>        </td>        <td style=\"vertical-align:bottom;padding:5px 0 0 0!important;\" rowspan=\"2\">            <table style=\"border-collapse:collapse;width:110px;color:#99a;font-family:'Helvetica','Lucida Grande','Arial',sans-serif;\"><tbody>                  <!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->            <tr style=\"width:110px;height:16px;font-size:10px!important;\">              <td style=\"padding-right:9px;font-size:11px!important;font-weight:bold;text-transform:uppercase;text-align:right;white-space:nowrap;overflow:visible;\" nowrap=\"nowrap\">                                     <strong>PREV</strong>                                     </td>         <td style=\"padding-left:9px;font-size:11px!important;font-weight:bold;text-transform:uppercase;white-space:nowrap;overflow:visible;\" nowrap=\"nowrap\">                <strong>NEXT</strong>                                    </td>            </tr>             <!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->            <tr>                <td style=\"padding-right:9px;white-space:nowrap;overflow:visible;text-align:right!important;vertical-align:middle!important;\" nowrap=\"nowrap\">                                    <a href=\""
                  + TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + prevDate + "&query=" + query
                  + "\" title=\""
                  + prevDate
                  + "\" style=\"background-color:transparent;border:none;\"><img src=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_prv_on.png\" alt=\"Previous capture\" border=\"0\" height=\"16\" width=\"14\"></a>                                    </td>         <td style=\"padding-left:9px;white-space:nowrap;overflow:visible;text-align:left!important;vertical-align:middle!important;\" nowrap=\"nowrap\">                                    <a href=\""
                      + TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + nextDate + "&query=" + query
                      + "\" title=\""
                      + nextDate
                      + "\" style=\"background-color:transparent;border:none;\"><img src=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_nxt_on.png\" alt=\"Next capture\" border=\"0\" height=\"16\" width=\"14\"></a>                              </td>            </tr>             </tbody></table>        </td>         </tr>        <tr>        <td style=\"vertical-align:middle;padding:0!important;\">            <strong>"
                          + " " + num + " captures"
                          + "</strong>            <div style=\"margin:0!important;padding:0!important;color:#666;font-size:9px;padding-top:2px!important;white-space:nowrap;\" title=\"Timespan for captures of this URL\">"
                              + dates.get(0).substring(0, 10) + "  -  " + dates.get(dates.size() - 1).substring(0, 10)
                              + "</div>        </td>                </tr></tbody></table>    </td>    <td style=\"text-align:right;padding:5px;width:65px;font-size:11px!important;\">        <a href=\"javascript:;\" onclick=\"document.getElementById('wm-ipp').style.display='none';\" style=\"display:block;padding-right:18px;background:url(images/wm_tb_close.png) no-repeat 100% 0;color:#33f;font-family:'Lucida Grande','Arial',sans-serif;margin-bottom:23px;background-color:transparent;border:none;\" title=\"Close the toolbar\">Close</a>            </td>    </tr></tbody></table>  </div> </div>      <style type=\"text/css\">body{margin-top:0!important;padding-top:0!important;min-width:800px!important;}#wm-ipp a:hover{text-decoration:underline!important;}</style>");
      bodyContent = doc.html();
      out.println(bodyContent);
    }

  private void writeResponse(HttpServletResponse resp, Result rs, byte[] data, String query, String d, int num)
      throws IOException {
    HttpResponseRecord httpResponseRecord = new HttpResponseRecord(data);
    if (httpResponseRecord.getType().startsWith("text/plain") || !httpResponseRecord.getType().startsWith("text")) {
      resp.setHeader("Content-Type", httpResponseRecord.getType());
      resp.setContentLength(httpResponseRecord.getBodyByte().length);
      resp.getOutputStream().write(httpResponseRecord.getBodyByte());
    } else {
      ArrayList<String> dates = new ArrayList<String>(10);
      for (int i = 0; i < rs.raw().length; i++)
        dates.add(new String(rs.raw()[i].getQualifier()));
      Collections.sort(dates);
      String prevDate = null, nextDate = null;
      for (int i = 1; i < dates.size(); i++){
        if (dates.get(i).compareTo(d) == 0){
          prevDate = dates.get(i - 1);
          if (i + 1 < dates.size())
            nextDate = dates.get(i + 1);
          else
            nextDate = d;
          break;
        }
        if (dates.get(i).compareTo(d) > 0) {
          if ( i > 2)
            prevDate = dates.get(i - 2);
          else
            prevDate = dates.get(i - 1);
          nextDate = dates.get(i);
          break;
        }
      }
      System.setProperty("file.encoding", "UTF8");
      resp.setHeader("Content-Type", httpResponseRecord.getType());
      resp.setCharacterEncoding("UTF-8");
      PrintWriter out = resp.getWriter();
      TextDocument2 t2 = new TextDocument2(null, null, null);
      String bodyContent = new String(httpResponseRecord.getBodyByte(), "UTF8");
      Document doc = Jsoup.parse(bodyContent);
      Element head = doc.select("head").first();
      Element base = doc.select("base").first();
      if(base == null){
        head.prepend("<base id='warcbase-base-added' href='" + query +"'>");
      }
      bodyContent = doc.html();
      bodyContent = t2.fixURLs(bodyContent, query, d);
      doc = Jsoup.parse(bodyContent);
      base = doc.select("base").first();
      if(base != null){
        if(base.attr("id").equals("warcbase-base-added"))
          base.remove();
      }
      head = doc.select("head").first();
      String baseUrl = TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + d + "&query=" + query;
      head.prepend("<script type=\"text/javascript\"> function initYTVideo(id) {  _wmVideos_.init('/web/', id); } </script>  <script> function $(a){return document.getElementById(a)};     function addLoadEvent(a){if(window.addEventListener)addEventListener('load',a,false);else if(window.attachEvent)attachEvent('onload',a)} </script>");
      Element body = doc.select("body").first();
      body.prepend("<div id=\"wm-ipp\" style=\"display: block; position: relative; padding: 0px 5px; min-height: 70px; min-width: 800px; z-index: 9000;\"><div id=\"wm-ipp-inside\" style=\"position:fixed;padding:0!important;margin:0!important;width:97%;min-width:780px;border:5px solid #000;border-top:none;background-image:url(" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_bk_trns.png);text-align:center;-moz-box-shadow:1px 1px 3px #333;-webkit-box-shadow:1px 1px 3px #333;box-shadow:1px 1px 3px #333;font-size:11px!important;font-family:'Lucida Grande','Arial',sans-serif!important;\">    <table style=\"border-collapse:collapse;margin:0;padding:0;width:100%;\"><tbody><tr>    <td style=\"padding:10px;vertical-align:top;min-width:110px;\">    <a href=\""
          + TextDocument2.SERVER_PREFIX + "warcbase"
          + "\" title=\"Warcbase home page\" style=\"background-color:transparent;border:none;\">Warcbase</a>    </td>            <td style=\"padding:0!important;text-align:center;vertical-align:top;width:100%;\">         <table style=\"border-collapse:collapse;margin:0 auto;padding:0;width:570px;\"><tbody><tr>        <td style=\"padding:3px 0;\" colspan=\"2\">        <form target=\"_top\" method=\"get\" action=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "servlet\" name=\"wmtb\" id=\"wmtb\" style=\"margin:0!important;padding:0!important;\"><input name=\"query\" id=\"wmtbURL\" value=\""
              + query
              + "\" style=\"width:400px;font-size:11px;font-family:'Lucida Grande','Arial',sans-serif;\" onfocus=\"javascript:this.focus();this.select();\" type=\"text\"><input name=\"date\" value=\"\" type=\"hidden\"><input name=\"type\" value=\"replay\" type=\"hidden\"><input name=\"date\" value=\"20120201185436\" type=\"hidden\"><input value=\"Go\" style=\"font-size:11px;font-family:'Lucida Grande','Arial',sans-serif;margin-left:5px;\" type=\"submit\"><span id=\"wm_tb_options\" style=\"display:block;\"></span></form>        </td>        <td style=\"vertical-align:bottom;padding:5px 0 0 0!important;\" rowspan=\"2\">            <table style=\"border-collapse:collapse;width:110px;color:#99a;font-family:'Helvetica','Lucida Grande','Arial',sans-serif;\"><tbody>                  <!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->            <tr style=\"width:110px;height:16px;font-size:10px!important;\">              <td style=\"padding-right:9px;font-size:11px!important;font-weight:bold;text-transform:uppercase;text-align:right;white-space:nowrap;overflow:visible;\" nowrap=\"nowrap\">                                     <strong>PREV</strong>                                     </td>         <td style=\"padding-left:9px;font-size:11px!important;font-weight:bold;text-transform:uppercase;white-space:nowrap;overflow:visible;\" nowrap=\"nowrap\">                <strong>NEXT</strong>                                    </td>            </tr>             <!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->            <tr>                <td style=\"padding-right:9px;white-space:nowrap;overflow:visible;text-align:right!important;vertical-align:middle!important;\" nowrap=\"nowrap\">                                    <a href=\""
                  + TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + prevDate + "&query=" + query
                  + "\" title=\""
                  + prevDate
                  + "\" style=\"background-color:transparent;border:none;\"><img src=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_prv_on.png\" alt=\"Previous capture\" border=\"0\" height=\"16\" width=\"14\"></a>                                    </td>         <td style=\"padding-left:9px;white-space:nowrap;overflow:visible;text-align:left!important;vertical-align:middle!important;\" nowrap=\"nowrap\">                                    <a href=\""
                      + TextDocument2.SERVER_PREFIX + "warcbase/servlet?date=" + nextDate + "&query=" + query
                      + "\" title=\""
                      + nextDate
                      + "\" style=\"background-color:transparent;border:none;\"><img src=\"" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_nxt_on.png\" alt=\"Next capture\" border=\"0\" height=\"16\" width=\"14\"></a>                              </td>            </tr>             </tbody></table>        </td>         </tr>        <tr>        <td style=\"vertical-align:middle;padding:0!important;\">            <strong>"
                          + " " + num + " captures"
                          + "</strong>            <div style=\"margin:0!important;padding:0!important;color:#666;font-size:9px;padding-top:2px!important;white-space:nowrap;\" title=\"Timespan for captures of this URL\">"
                              + dates.get(0).substring(0, 10) + "  -  " + dates.get(dates.size() - 1).substring(0, 10)
                              + "</div>        </td>                </tr></tbody></table>    </td>    <td style=\"text-align:right;padding:5px;width:65px;font-size:11px!important;\">        <a href=\"javascript:;\" onclick=\"document.getElementById('wm-ipp').style.display='none';\" style=\"display:block;padding-right:18px;background:url(" + TextDocument2.SERVER_PREFIX + "warcbase/" + "images/wm_tb_close.png) no-repeat 100% 0;color:#33f;font-family:'Lucida Grande','Arial',sans-serif;margin-bottom:23px;background-color:transparent;border:none;\" title=\"Close the toolbar\">Close</a>            </td>    </tr></tbody></table>  </div> </div>      <style type=\"text/css\">body{margin-top:0!important;padding-top:0!important;min-width:800px!important;}#wm-ipp a:hover{text-decoration:underline!important;}</style>");
      bodyContent = doc.html();
      out.println(bodyContent);
    }
