1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest;
  28 
  29 import java.io.PrintWriter;
  30 import java.util.Iterator;
  31 
  32 import com.sun.javatest.httpd.httpURL;
  33 import com.sun.javatest.httpd.JThttpProvider;
  34 import com.sun.javatest.httpd.PageGenerator;
  35 import com.sun.javatest.util.Debug;
  36 import com.sun.javatest.util.I18NResourceBundle;
  37 
  38 /*
  39  * HTTP service provider for TestResultTable.
  40  */
  41 class TRT_HttpHandler extends JThttpProvider {
  42     TRT_HttpHandler(TestResultTable trt, String url, int instanceNum) {
  43         this.instanceNum = instanceNum;
  44         this.trt = trt;
  45     }
  46 
  47     public void serviceRequest(httpURL requestURL, PrintWriter out) {
  48         String nf = requestURL.getNextFile();
  49 
  50         // start the document
  51         if (nf == null) {
  52             beginGood(out);
  53             PageGenerator.writeBeginDoc(out);
  54 
  55             printIndex(out);
  56         }
  57         else if (nf.equals("tests")) {
  58             beginGood(out);
  59             PageGenerator.writeBeginDoc(out);
  60             printTests(requestURL, out);
  61         }
  62         else {
  63             if (debug)
  64                 Debug.println("TRT.HH-remainder of URL unknown (" + nf + ")");
  65             beginBad(out);
  66             printIndex(out);
  67         }
  68 
  69         out.println("<br><hr>");
  70         PageGenerator.writeFooter(out);
  71         PageGenerator.endBody(out);
  72         PageGenerator.writeEndDoc(out);
  73 
  74         out.close();
  75     }
  76 
  77     public String getRegistredURL() {
  78         return file;
  79     }
  80 
  81     private void beginGood(PrintWriter out) {
  82         PageGenerator.generateOkHttp(out);
  83         PageGenerator.generateDocType(out, PageGenerator.HTML32);
  84     }
  85 
  86     private void beginBad(PrintWriter out) {
  87         PageGenerator.generateBadHttp(out);
  88         PageGenerator.generateDocType(out, PageGenerator.HTML32);
  89     }
  90 
  91     private void printIndex(PrintWriter out) {
  92         PageGenerator.writeHeader(out, i18n.getString("trtHttp.index.title"));
  93         PageGenerator.startBody(out);
  94         // heading
  95         out.print("<h2>");
  96         print(out, i18n.getString("trtHttp.index.hdr"));
  97         out.println("</h2>");
  98         out.println("<hr Width=\"40%\" Align=left>");
  99 
 100         out.println("<p>");
 101         printStats(out);
 102 
 103     }
 104 
 105     private void printTests(httpURL url, PrintWriter out) {
 106         PageGenerator.writeHeader(out, i18n.getString("trtHttp.tests.title"));
 107         PageGenerator.startBody(out);
 108         out.print("<h2>");
 109         print(out, i18n.getString("trtHttp.tests.hdr"));
 110         out.println("</h2>");
 111         out.println("<hr Width=\"40%\" Align=left>");
 112 
 113         out.println("<p>");
 114         writeTests(out, i18n.getString("trtHttp.tests.name"),
 115                    i18n.getString("trtHttp.tests.status"));
 116     }
 117 
 118     public void writeTests(PrintWriter out, String keyHeader, String valHeader) {
 119         // XXX should include HTML filtering of strings
 120         // this is a custom version of the code found in PageGenerator
 121 
 122         out.println("<Table Border>");
 123 
 124         StringBuffer buf = new StringBuffer(50);
 125 
 126         // write the table header
 127         buf.append("<tr><th>");
 128         buf.append(keyHeader);
 129         buf.append("<th>");
 130         buf.append(valHeader);
 131         buf.append("</tr>");
 132         out.println(buf.toString());
 133 
 134         /*
 135         if (dict == null || dict.size() == 0) {
 136             // no values to write, fill the space
 137             buf.setLength(0);
 138             buf.append("<tr><td colspan=2>");
 139             buf.append("-EMPTY-");
 140             buf.append("</tr>");
 141         }
 142         else {
 143             */
 144             Iterator it = trt.getIterator();
 145             while (it.hasNext()) {
 146                 TestResult tr = (TestResult)(it.next());
 147                 String url;
 148                 try {
 149                     url = tr.getDescription().getRootRelativeURL();
 150                 }
 151                 catch(TestResult.Fault f) {
 152                     out.println("<tr><td>Unable to get TestResult info, aborting.</tr>");
 153                     if (debug) f.printStackTrace();
 154                     // exit the while loop
 155                     break;
 156                 }
 157 
 158                 out.println("<tr>");
 159                 buf.setLength(0);
 160                 buf.append("<td>");
 161                 buf.append(url);
 162                 buf.append("<td>");
 163                 buf.append(tr.getStatus().toString());
 164                 out.println(buf.toString());
 165                 out.println("</tr>");
 166             }   // while
 167         //}
 168 
 169         out.println("</Table>");
 170     }
 171 
 172     private void printStats(PrintWriter out) {
 173         out.print("<b>");
 174         print(out, i18n.getString("trtHttp.stats.hdr"));
 175         out.println("</b><br>");
 176         PageGenerator.startTable(out, false);
 177 
 178         // workdir
 179         out.println("<tr>");
 180         out.print("   <td>");
 181         print(out, i18n.getString("trtHttp.wd.hdr"));
 182         out.println("</td>");
 183         out.print("   <td>");
 184         print(out, (trt.getWorkDir() != null ? trt.getWorkDir().getPath() :
 185                 i18n.getString("trtHttp.wd.unset")));
 186         out.println("</td>");
 187 
 188         // size
 189         out.println("<tr>");
 190         out.print("   <td>");
 191         print(out, i18n.getString("trtHttp.size.hdr"));
 192         out.println("</td>");
 193 
 194         out.print("   <td><a href=\"/trt/");
 195         out.print(Integer.toString(instanceNum));
 196         out.print("/tests\">");
 197         out.print(Integer.toString(trt.size()));
 198         out.println("</a></td>");
 199         out.println("</tr>");
 200 
 201         PageGenerator.endTable(out);
 202     }
 203 
 204     private TestResultTable trt;
 205     private int instanceNum;
 206     private String file;
 207 
 208     private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(TRT_HttpHandler.class);
 209     private static boolean debug = Debug.getBoolean(TRT_HttpHandler.class);
 210 }