1
2
3
4
5
6
7 package org.astrogrid.webapp;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.security.Principal;
12 import java.util.Enumeration;
13 import javax.servlet.ServletRequest;
14 import javax.servlet.http.HttpServletRequest;
15 import org.astrogrid.io.account.LoginAccount;
16
17 /***
18 * A set of methods for helping serving data in HTML form, eg for servlets
19 * or JSPs
20 * <p>
21 * @author M Hill
22 */
23
24 public class HtmlHelper
25 {
26
27 /***
28 * Gets the user details from the request
29 * @todo at the moment just returns anonymous
30 */
31 public static Principal getUser(HttpServletRequest request) {
32 return LoginAccount.ANONYMOUS;
33 }
34
35
36 /*** Convenience routine for returning the correct 'HTML' snippet that
37 * refreshes the page given by the URL - which should point to the same page
38 * that contains the snippet */
39 public static String makeRefreshSnippet(int secs, String url) {
40 return("(Refreshes every "+secs+" seconds)"+
41 "<META HTTP-EQUIV='Refresh' CONTENT='"+secs+";URL="+url+"'>");
42 }
43
44 /***
45 * Returns an error as string suitable for display in a browser as an html
46 * page
47 */
48 public static String exceptionAsHtmlPage(String title, Throwable th, String details) {
49 return
50 "<html>\n"+
51 "<head><title>ERROR: "+makeSafeForHtml(title)+"</title></head>\n"+
52 "<body>\n"+
53 exceptionAsHtml(title, th, details)+
54 "</body>\n"+
55 "</html>\n";
56 }
57
58 /*** Returns exception suitable for a paragraph in an hmtl page */
59 public static String exceptionAsHtml(String title, Throwable th, String details) {
60
61 String s =
62 "<h1>ERROR REPORT</h1>\n"+
63 "<h2>"+makeSafeForHtml(title)+"</h2>\n";
64 if (th != null) {
65 StringWriter sw = new StringWriter();
66 th.printStackTrace(new PrintWriter(sw));
67 String stack = sw.toString();
68
69 s = s +
70 "<p><b>"+makeSafeForHtml(th.toString())+"</b></p>\n"+
71 "<p>\n"+
72 "<pre>"+makeSafeForHtml(stack)+"</pre>\n"+
73 "</p>\n"+
74 "<p>\n"+
75 "<pre>"+makeSafeForHtml(details)+"</pre>\n"+
76 "</p>\n";
77 }
78 return s;
79 }
80
81 /***
82 * Deals with special characters */
83 public static String makeSafeForHtml(String s) {
84 if (s==null) {
85 return "";
86 }
87 s = s.replaceAll(">", ">").replaceAll("<", "<");
88 return s.replaceAll("\n","<br/>");
89 }
90
91 /*** Convenience routine for exceptionAsHtml(String, Exception, String) */
92 public static String exceptionAsHtmlPage(String title, Throwable th) {
93 return exceptionAsHtmlPage(title, th, "");
94 }
95
96 /*** Creates suitable hidden input fields so that all parameters passed in
97 * are preserved when the form is submitted
98 */
99 public static void preserveParams(PrintWriter out, ServletRequest request) {
100 Enumeration e = request.getParameterNames();
101 while (e.hasMoreElements()) {
102 String key = (String)e.nextElement();
103 String[] values = request.getParameterValues(key);
104 for(int i = 0; i < values.length; i++) {
105 out.print("<input type='hidden' name='"+key+"' value='"+values[i]+"' />");
106 }
107 out.println();
108 }
109 }
110
111 /*** Dumps all the parameters in the given request to the given writer.
112 * Useful for debug at the bottom of the screen
113 */
114 public static void dumpAllParams(PrintWriter out, ServletRequest request) {
115 Enumeration e = request.getParameterNames();
116 while (e.hasMoreElements()) {
117 String key = (String)e.nextElement();
118 String[] values = request.getParameterValues(key);
119 out.print(" " + key + " = ");
120 for(int i = 0; i < values.length; i++) {
121 out.print(values[i] + " ");
122 }
123 out.println();
124 }
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137