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.io.Writer;
12 import java.util.Enumeration;
13 import javax.servlet.ServletRequest;
14 import javax.servlet.http.HttpServletRequest;
15 import org.astrogrid.community.Account;
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 public static Account getUser(HttpServletRequest request) {
30 return Account.ANONYMOUS;
31 }
32
33
34 /*** Convenience routine for returning the correct 'HTML' snippet that
35 * refreshes the page given by the URL - which should point to the same page
36 * that contains the snippet */
37 public static String makeRefreshSnippet(int secs, String url) {
38 return("(Refreshes every "+secs+" seconds)"+
39 "<META HTTP-EQUIV='Refresh' CONTENT='"+secs+";URL="+url+"'>");
40 }
41
42 /*** Returns the stylesheet filename to be used for the center's html pages in the form
43 * of a full URL including 'link' element. Returns an empty string if none configured
44 public static String getCssLink() {
45 String cssName = SimpleConfig.getProperty("datacenter.stylesheet", "default.css");
46 if (cssName.length() == 0) {
47 return "";
48 }
49 else {
50 return "<LINK href='"+cssName+"' rel='stylesheet' type='text/css'>";
51 }
52 }
53
54 /** Convenience routine that returns the complete <HEAD> element for the
55 * standard datacenter page *
56 public static String getHeadElement(String title) {
57 return "<HEAD>\n"+
58 " <TITLE>"+title+" ("+DataServer.getDatacenterName()+")</TITLE>\n"+
59 " "+getCssLink()+"\n"+
60 "</HEAD>\n";
61 }
62
63
64 /**
65 * Returns an error as string suitable for display in a browser as an html
66 * page
67 */
68 public static String exceptionAsHtmlPage(String title, Throwable th, String details) {
69 return
70 "<html>\n"+
71 "<head><title>ERROR: "+makeSafeForHtml(title)+"</title></head>\n"+
72 "<body>\n"+
73 exceptionAsHtml(title, th, details)+
74 "</body>\n"+
75 "</html>\n";
76 }
77
78 /*** Returns exception suitable for a paragraph in an hmtl page */
79 public static String exceptionAsHtml(String title, Throwable th, String details) {
80
81 String s =
82 "<h1>ERROR REPORT</h1>\n"+
83 "<h2>"+makeSafeForHtml(title)+"</h2>\n";
84 if (th != null) {
85 StringWriter sw = new StringWriter();
86 th.printStackTrace(new PrintWriter(sw));
87 String stack = sw.toString();
88
89 s = s +
90 "<p><b>"+makeSafeForHtml(th.toString())+"</b></p>\n"+
91 "<p>\n"+
92 "<pre>"+makeSafeForHtml(stack)+"</pre>\n"+
93 "</p>\n"+
94 "<p>\n"+
95 "<pre>"+makeSafeForHtml(details)+"</pre>\n"+
96 "</p>\n";
97 }
98 return s;
99 }
100
101 /***
102 * Deals with special characters */
103 public static String makeSafeForHtml(String s) {
104 if (s==null) {
105 return "";
106 }
107 s = s.replaceAll(">", ">").replaceAll("<", "<");
108 return s.replaceAll("\n","<br/>");
109 }
110
111 /*** Convenience routine for exceptionAsHtml(String, Exception, String) */
112 public static String exceptionAsHtmlPage(String title, Throwable th) {
113 return exceptionAsHtmlPage(title, th, "");
114 }
115
116 /*** Creates suitable hidden input fields so that all parameters passed in
117 * are preserved when the form is submitted
118 */
119 public static void preserveParams(PrintWriter out, ServletRequest request) {
120 Enumeration e = request.getParameterNames();
121 while (e.hasMoreElements()) {
122 String key = (String)e.nextElement();
123 String[] values = request.getParameterValues(key);
124 for(int i = 0; i < values.length; i++) {
125 out.print("<input type='hidden' name='"+key+"' value='"+values[i]+"' />");
126 }
127 out.println();
128 }
129 }
130
131 /*** Dumps all the parameters in the given request to the given writer.
132 * Useful for debug at the bottom of the screen
133 */
134 public static void dumpAllParams(PrintWriter out, ServletRequest request) {
135 Enumeration e = request.getParameterNames();
136 while (e.hasMoreElements()) {
137 String key = (String)e.nextElement();
138 String[] values = request.getParameterValues(key);
139 out.print(" " + key + " = ");
140 for(int i = 0; i < values.length; i++) {
141 out.print(values[i] + " ");
142 }
143 out.println();
144 }
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157