001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.tools.checks;
007:
008: import java.io.IOException;
009: import java.io.PrintWriter;
010: import java.util.ArrayList;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: import javax.servlet.http.HttpServlet;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017:
018: /**
019: * A Servlet which renders the results of the checks.
020: * @version $Revision: 35929 $ $Date: 2005-07-15 13:29:15 -0700 (Fri, 15 Jul 2005) $
021: * @since uPortal 2.5
022: */
023: public class ChecksServlet extends HttpServlet {
024:
025: protected final void doPost(HttpServletRequest request,
026: HttpServletResponse response) throws IOException {
027:
028: if ("rerun".equals(request.getParameter("rerun"))) {
029:
030: SafeDelegatingCheckRunner checkRunner = new SafeDelegatingCheckRunner();
031: List results = checkRunner.doChecks();
032: getServletContext().setAttribute(
033: CheckingContextListener.RESULTS_SC_KEY, results);
034: }
035:
036: doGet(request, response);
037: }
038:
039: protected final void doGet(HttpServletRequest request,
040: HttpServletResponse response) throws IOException {
041:
042: List results = (List) this .getServletContext().getAttribute(
043: CheckingContextListener.RESULTS_SC_KEY);
044:
045: response.setHeader("pragma", "no-cache");
046: response.setHeader("Cache-Control",
047: "no-cache, max-age=0, must-revalidate");
048: response.setDateHeader("Expires", 0);
049:
050: PrintWriter writer = response.getWriter();
051:
052: writer.println("<html>");
053: writer.println("<head>");
054: writer.println("<title>");
055: writer.println("uPortal instrumentation");
056: writer.println("</title>");
057: writer.println("</head>");
058:
059: writer.println("<body>");
060:
061: if (results == null) {
062: writer
063: .println("<p>Could not find check results in servlet context.</p>");
064: } else {
065: List failedChecks = failedChecks(results);
066:
067: if (!failedChecks.isEmpty()) {
068: writer.println("<h3>Failed checks</h3>");
069: writer.println("<p>" + failedChecks.size()
070: + " checks failed.</p>");
071:
072: printChecksAsTable(failedChecks, writer);
073:
074: }
075:
076: writer.println("<h3>Results of all checks:</h3>");
077:
078: printChecksAsTable(results, writer);
079:
080: writer
081: .println("<form action='instrumentation' method='post'>");
082: writer
083: .println("<input name='rerun' value='rerun' type='submit'/>");
084: writer.println("</form>");
085:
086: }
087:
088: // print out package information
089:
090: writer.println("<br/>");
091: writer.println("<h3>Package information.</h3>");
092:
093: writer.println("<table>");
094: writer.println("<tr>");
095: writer.println("<td>");
096: writer.println("Implementation title");
097: writer.println("</td>");
098: writer.println("<td>");
099: writer.println("Implementation version");
100: writer.println("</td>");
101: writer.println("<td>");
102: writer.println("Package name");
103: writer.println("</td>");
104: writer.println("<td>");
105: writer.println("Implementation vendor");
106: writer.println("</td>");
107: writer.println("<td>");
108: writer.println("Specification title");
109: writer.println("</td>");
110: writer.println("<td>");
111: writer.println("Specification vendor");
112: writer.println("</td>");
113: writer.println("<td>");
114: writer.println("Specification version");
115: writer.println("</td>");
116: writer.println("</tr>");
117:
118: Package[] packs = Package.getPackages();
119:
120: for (int i = 0; i < packs.length; i++) {
121: Package pack = packs[i];
122: writer.println("<tr>");
123: writer.println("<td>");
124: writer.println(pack.getImplementationTitle());
125: writer.println("</td>");
126: writer.println("<td>");
127: writer.println(pack.getImplementationVersion());
128: writer.println("</td>");
129: writer.println("<td>");
130: writer.println(pack.getName());
131: writer.println("</td>");
132: writer.println("<td>");
133: writer.println(pack.getImplementationVendor());
134: writer.println("</td>");
135: writer.println("<td>");
136: writer.println(pack.getSpecificationTitle());
137: writer.println("</td>");
138: writer.println("<td>");
139: writer.println(pack.getSpecificationVendor());
140: writer.println("</td>");
141: writer.println("<td>");
142: writer.println(pack.getSpecificationVersion());
143: writer.println("</td>");
144: writer.println("</tr>");
145: }
146:
147: writer.println("</table>");
148:
149: writer.println("</body>");
150:
151: writer.println("</html>");
152: writer.flush();
153:
154: }
155:
156: /**
157: * Returns the List of failed checks that contains, order retained, those
158: * checks from the given list that failed.
159: * @param checks a List of CheckAndResults
160: * @return a List of those of the given CheckAndResults that failed.
161: */
162: private List failedChecks(List checks) {
163: if (checks == null) {
164: throw new IllegalArgumentException(
165: "Checks must not be null.");
166: }
167:
168: List failedChecks = new ArrayList();
169:
170: for (Iterator iter = checks.iterator(); iter.hasNext();) {
171: CheckAndResult checkAndResult = (CheckAndResult) iter
172: .next();
173: if (!checkAndResult.isSuccess()) {
174: // found a failure
175: failedChecks.add(checkAndResult);
176: }
177: }
178:
179: return failedChecks;
180: }
181:
182: /**
183: * Prints the given List of CheckAndResult instances as a table to the
184: * given PrintWriter.
185: * @param checks CheckAndResult instances
186: * @param writer output target
187: */
188: private void printChecksAsTable(List checks, PrintWriter writer) {
189: writer.println("<table>");
190: writer.println("<tr>");
191: writer.println("<td>");
192: writer.println("Status");
193: writer.println("</td>");
194: writer.println("<td>");
195: writer.println("Check description");
196: writer.println("</td>");
197: writer.println("<td>");
198: writer.println("Check result message");
199: writer.println("</td>");
200: writer.println("<td>");
201: writer.println("Remediation advice");
202: writer.println("</td>");
203: writer.println("</tr>");
204:
205: for (Iterator iter = checks.iterator(); iter.hasNext();) {
206: CheckAndResult checkAndResult = (CheckAndResult) iter
207: .next();
208:
209: writer.println("<tr>");
210: writer.println("<td>");
211: if (checkAndResult.isSuccess()) {
212: writer.println("OK");
213: } else {
214: writer.println("FAILURE");
215: }
216: writer.println("</td>");
217: writer.println("<td>");
218: writer.println(checkAndResult.getCheckDescription());
219: writer.println("</td>");
220: writer.println("<td>");
221: writer.println(checkAndResult.getResult().getMessage());
222: writer.println("</td>");
223: writer.println("<td>");
224: if (checkAndResult.isSuccess()) {
225: writer.println("Not applicable.");
226: } else {
227: writer.println(checkAndResult.getResult()
228: .getRemediationAdvice());
229: }
230: writer.println("</td>");
231: writer.println("</tr>");
232:
233: }
234:
235: writer.println("</table>");
236: }
237:
238: }
|