KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > pub > ServletDebug


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.web.pub;
15
16 import java.io.*;
17
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.ejbca.ui.web.RequestHelper;
23
24
25 /**
26  * Prints debug info back to browser client
27  * @version $Id: ServletDebug.java,v 1.3 2006/02/09 08:45:22 anatom Exp $
28  */

29 public class ServletDebug {
30     private final ByteArrayOutputStream buffer;
31     private final PrintStream printer;
32     private final HttpServletRequest JavaDoc request;
33     private final HttpServletResponse JavaDoc response;
34
35     public ServletDebug(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
36         buffer = new ByteArrayOutputStream();
37         printer = new PrintStream(buffer);
38         this.request = request;
39         this.response = response;
40     }
41
42     public void printDebugInfo() throws IOException, ServletException JavaDoc {
43         request.setAttribute("ErrorMessage", new String JavaDoc(buffer.toByteArray()));
44         request.getRequestDispatcher("error.jsp").forward(request, response);
45     }
46
47     public void print(Object JavaDoc o) {
48         printer.println(o);
49     }
50
51     public void printMessage(String JavaDoc msg) {
52         print("<p>" + msg);
53     }
54
55     public void printInsertLineBreaks(byte[] bA) throws Exception JavaDoc {
56         BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bA)));
57
58         while (true) {
59             String JavaDoc line = br.readLine();
60
61             if (line == null) {
62                 break;
63             }
64
65             print(line.toString() + "<br>");
66         }
67     }
68
69     public void takeCareOfException(Throwable JavaDoc t) {
70         ByteArrayOutputStream baos = new ByteArrayOutputStream();
71         t.printStackTrace(new PrintStream(baos));
72         print("<h4>Exception:</h4>");
73
74         try {
75             printInsertLineBreaks(baos.toByteArray());
76         } catch (Exception JavaDoc e) {
77             e.printStackTrace(printer);
78         }
79
80         request.setAttribute("Exception", "true");
81     }
82
83     public void ieCertFix(byte[] bA) throws Exception JavaDoc {
84         ByteArrayOutputStream baos = new ByteArrayOutputStream();
85         PrintStream tmpPrinter = new PrintStream(baos);
86         RequestHelper.ieCertFormat(bA, tmpPrinter);
87         printInsertLineBreaks(baos.toByteArray());
88     }
89 }
90  // Debug
91
Popular Tags