This project has retired. For details please refer to its Attic page.
WebRunnerServlet xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.tck.runner;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.chemistry.opencmis.tck.CmisTest;
32  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
33  import org.apache.chemistry.opencmis.tck.CmisTestProgressMonitor;
34  import org.apache.chemistry.opencmis.tck.report.CoreHtmlReport;
35  
36  /**
37   * Web Runner.
38   */
39  public class WebRunnerServlet extends HttpServlet {
40  
41      private static final long serialVersionUID = 1L;
42  
43      @Override
44      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45          resp.setContentType("text/html; charset=UTF-8");
46  
47          PrintWriter pw = resp.getWriter();
48  
49          printHeader(pw);
50  
51          pw.println("<h1>OpenCMIS TCK</h1>");
52  
53          pw.println("<form action=\"" + req.getRequestURI() + "\" method=\"POST\">");
54          pw.println("<table>");
55          pw.println("<tr><td>AtomPub URL:</td><td><input type=\"text\" name=\"org.apache.chemistry.opencmis.binding.atompub.url\" size=\"50\"></td></tr>");
56          pw.println("<tr><td>Username:</td><td><input type=\"text\" name=\"org.apache.chemistry.opencmis.user\" size=\"50\"></td></tr>");
57          pw.println("<tr><td>Password:</td><td><input type=\"password\" name=\"org.apache.chemistry.opencmis.password\" size=\"50\"></td></tr>");
58          pw.println("<tr><td>Repository Id:</td><td><input type=\"text\" name=\"org.apache.chemistry.opencmis.session.repository.id\" size=\"50\"></td></tr>");
59          pw.println("<tr><td></td><td><input type=\"submit\" value=\"Start TCK\"></td></tr>");
60          pw.println("<input type=\"hidden\" name=\"org.apache.chemistry.opencmis.binding.spi.type\" value=\"atompub\">");
61          pw.println("</form>");
62  
63          printFooter(pw);
64      }
65  
66      @SuppressWarnings("unchecked")
67      @Override
68      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
69          resp.setBufferSize(0);
70          resp.setContentType("text/html; charset=UTF-8");
71  
72          PrintWriter pw = resp.getWriter();
73  
74          printHeader(pw);
75  
76          Map<String, String> parameters = new HashMap<String, String>();
77          for (Map.Entry<String, String[]> entry : ((Map<String, String[]>) req.getParameterMap()).entrySet()) {
78              if ((entry.getValue() == null) || (entry.getValue().length < 1)) {
79                  continue;
80              }
81              parameters.put(entry.getKey(), entry.getValue()[0]);
82          }
83  
84          try {
85              WebRunner runner = new WebRunner();
86              runner.setParameters(parameters);
87              runner.loadDefaultTckGroups();
88  
89              pw.println("<div id=\"progress\">");
90              pw.println("<h1>Running OpenCMIS TCK</h1>");
91  
92              runner.run(new WebProgressMonitor(pw));
93  
94              pw.println("</div>");
95  
96              // let progress div disappear
97              pw.println("<script language=\"javascript\">");
98              pw.println("document.getElementById(\"progress\").style.display = \"none\";");
99              pw.println("</script>");
100 
101             (new CoreHtmlReport()).createReport(runner.getParameters(), runner.getGroups(), pw);
102         } catch (Exception e) {
103             pw.println("<h2>Exception</h2>");
104 
105             pw.println("\n<pre>");
106             e.printStackTrace(pw);
107             pw.println("\n</pre>");
108         }
109 
110         printFooter(pw);
111     }
112 
113     protected void printHeader(PrintWriter pw) throws IOException {
114         pw.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
115         pw.println("<html><head>\n<title>OpenCMIS TCK</title>");
116         CoreHtmlReport.printStyle(pw);
117         pw.println("</head><body>");
118     }
119 
120     protected void printFooter(PrintWriter pw) throws IOException {
121         pw.println("\n</body></html>");
122         pw.flush();
123     }
124 
125     private static class WebRunner extends AbstractRunner {
126     }
127 
128     private static class WebProgressMonitor implements CmisTestProgressMonitor {
129         private final PrintWriter pw;
130 
131         public WebProgressMonitor(PrintWriter pw) {
132             this.pw = pw;
133         }
134 
135         public void startGroup(CmisTestGroup group) {
136             pw.println("<h3>" + group.getName() + " (" + group.getTests().size() + " tests)</h3>");
137             pw.flush();
138         }
139 
140         public void endGroup(CmisTestGroup group) {
141             pw.println("<br>");
142             pw.flush();
143         }
144 
145         public void startTest(CmisTest test) {
146             pw.print("&nbsp;&nbsp;&nbsp;" + test.getName() + " ... ");
147             pw.flush();
148         }
149 
150         public void endTest(CmisTest test) {
151             pw.println("completed<br>");
152             pw.flush();
153         }
154 
155         public void message(String msg) {
156             pw.println(msg);
157             pw.flush();
158         }
159     }
160 }