Source code: com/acme/gui/presObjs/HttpDebug.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * $Id: HttpDebug.java,v 1.9.10.1 2000/10/19 17:59:06 jasona Exp $
22 */
23
24
25
26
27
28 package com.acme.gui.presObjs;
29
30 import java.util.Vector;
31 import java.util.Enumeration;
32 import javax.servlet.http.Cookie;
33 import com.lutris.appserver.server.httpPresentation.*;
34 import com.lutris.http.*;
35 import com.lutris.html.*;
36 import com.lutris.util.FatalExceptionError;
37
38 /**
39 * This class is used for debugging. Now only used for httpPresentation
40 * tests.
41 *
42 * @author Kyle Clark
43 */
44 public class HttpDebug {
45
46 private static int tableBorderWidth = 2;
47 private static int tablePercentWidth = 100;
48 private static String [] httpRequestHeader = {
49 "Accept",
50 "Accept-Charset",
51 "Accept-Encoding",
52 "Accept-Language",
53 "Authorization",
54 "From",
55 "Host",
56 "If-Modified-Since",
57 "If-Match",
58 "If-None-Match",
59 "If-Range",
60 "If-Unmodified-Since",
61 "Max-Forwards",
62 "Proxy-Authorization",
63 "Range",
64 "Referer",
65 "User-Agent" };
66
67 private static String [] httpResponseHeader = {
68 "Age",
69 "Allow",
70 "Content-Base",
71 "Content-Encoding",
72 "Content-Language",
73 "Content-Length",
74 "Content-Location",
75 "Content-MD5",
76 "Content-Range",
77 "Content-Type",
78 "ETag",
79 "Expires",
80 "Last-Modified",
81 "Location",
82 "Proxy-Authenticate",
83 "Public",
84 "Retry-After",
85 "Server",
86 "Vary",
87 "Warning",
88 "WWW-Authenticate"
89 };
90
91 private static String [] httpRedirectHeader = httpResponseHeader;
92
93 /**
94 * Private constructor, prevent instantiation.
95 */
96 private HttpDebug()
97 {
98 }
99
100 /**
101 * Output an set of HTML table of a presentation object's arguments
102 * and other state information.
103 * @param comms The comms object for the presentation. The HTML will
104 * be written to the response.
105 */
106 public static void displayComms(HttpPresentationComms comms) {
107 try {
108 HttpPresentationOutputStream out = comms.response.getOutputStream();
109 out.println("<HR><P>");
110 out.println(getRequestTables(comms.request));
111 out.println("<P>");
112 out.println(getCGIEnvironmentTable(comms.request));
113 out.println("<P><HR>");
114 } catch (Exception except) {
115 throw new FatalExceptionError(except);
116 }
117 }
118
119 /**
120 * Generates tables containing request debug information.
121 * Debug information includes request headers and cookies.
122 *
123 * @param request Object from which debug data is compiled.
124 * @exception Exception Throws all exceptions.
125 */
126 public static
127 String getRequestTables(HttpPresentationRequest request)
128 throws Exception
129 {
130 HtmlTable tbl = new HtmlTable();
131 HtmlTableRow row;
132 HtmlTableDataCell td;
133
134 tbl.setBorderWidth(tableBorderWidth*2);
135 tbl.setPercentWidth(tablePercentWidth);
136
137 addRequestHeaders(request, httpRequestHeader, tbl);
138 addRequestCookies(request, tbl);
139
140 return tbl.toString();
141 }
142
143 /**
144 * Generates a table containing all CGI environment information.
145 * The table is in html format.
146 *
147 * @param request Object from which CGI data is compiled.
148 * @exception Exception Throws all exceptions.
149 */
150 public static
151 String getCGIEnvironmentTable(HttpPresentationRequest request)
152 throws Exception
153 {
154 HtmlTable tbl = new HtmlTable();
155 tbl.setBorderWidth(tableBorderWidth);
156 tbl.setPercentWidth(tablePercentWidth);
157 addCGIEnvironmentData(request, tbl);
158 return tbl.toString();
159 }
160
161 /**
162 * This method adds rows to a table that contain the
163 * CGI environment data.
164 *
165 * @param request Object from which CGI data is compiled.
166 * @param tbl Table to which to add the data.
167 * @exception Exception Throws all exceptions.
168 */
169 protected static
170 void addCGIEnvironmentData(HttpPresentationRequest request,
171 HtmlTable tbl)
172 throws Exception
173 {
174 HtmlTableRow row;
175 HtmlTableDataCell td;
176 HtmlTableHeaderCell th;
177
178 row = new HtmlTableRow();
179 th = new HtmlTableHeaderCell("<FONT SIZE=+1>CGI Environment</FONT>");
180 th.setColSpan(2);
181 th.setHorizontalAlignment(th.ALIGN_CENTER);
182 row.addCell(th);
183 row.setColSpan(2);
184 row.setBackgroundColor("#999999");
185 tbl.addRow(row);
186
187 row = new HtmlTableRow();
188 td = new HtmlTableDataCell("<B>Name</B>");
189 td.setHorizontalAlignment(td.ALIGN_CENTER);
190 row.addCell(td);
191 td = new HtmlTableDataCell("<B>Value</B>");
192 td.setHorizontalAlignment(td.ALIGN_LEFT);
193 row.addCell(td);
194 tbl.addRow(row);
195
196 row = new HtmlTableRow();
197 td = new HtmlTableDataCell("DOCUMENT_URI");
198 td.setHorizontalAlignment(td.ALIGN_CENTER);
199 row.addCell(td);
200 row.addCell(new HtmlTableDataCell(request.getRequestURI()));
201 tbl.addRow(row);
202
203 row = new HtmlTableRow();
204 td = new HtmlTableDataCell("SERVER_NAME");
205 td.setHorizontalAlignment(td.ALIGN_CENTER);
206 row.addCell(td);
207 row.addCell(new HtmlTableDataCell(request.getServerName()));
208 tbl.addRow(row);
209
210 row = new HtmlTableRow();
211 td = new HtmlTableDataCell("SERVER_PROTOCOL");
212 td.setHorizontalAlignment(td.ALIGN_CENTER);
213 row.addCell(td);
214 row.addCell(new HtmlTableDataCell(request.getProtocol()));
215 tbl.addRow(row);
216
217 row = new HtmlTableRow();
218 td = new HtmlTableDataCell("SERVER_PORT");
219 td.setHorizontalAlignment(td.ALIGN_CENTER);
220 row.addCell(td);
221 row.addCell(new HtmlTableDataCell(Integer.toString(request.getServerPort())));
222 tbl.addRow(row);
223
224 row = new HtmlTableRow();
225 td = new HtmlTableDataCell("REQUEST_METHOD");
226 td.setHorizontalAlignment(td.ALIGN_CENTER);
227 row.addCell(td);
228 row.addCell(new HtmlTableDataCell(request.getMethod()));
229 tbl.addRow(row);
230
231 row = new HtmlTableRow();
232 td = new HtmlTableDataCell("PATH_INFO");
233 td.setHorizontalAlignment(td.ALIGN_CENTER);
234 row.addCell(td);
235 row.addCell(new HtmlTableDataCell(request.getPathInfo()));
236 tbl.addRow(row);
237
238 row = new HtmlTableRow();
239 td = new HtmlTableDataCell("PATH_TRANSLATED");
240 td.setHorizontalAlignment(td.ALIGN_CENTER);
241 row.addCell(td);
242 row.addCell(new HtmlTableDataCell(request.getPathTranslated()));
243 tbl.addRow(row);
244
245 row = new HtmlTableRow();
246 td = new HtmlTableDataCell("APPLICATION");
247 td.setHorizontalAlignment(td.ALIGN_CENTER);
248 row.addCell(td);
249 row.addCell(new HtmlTableDataCell(request.getApplicationPath()));
250 tbl.addRow(row);
251
252 row = new HtmlTableRow();
253 td = new HtmlTableDataCell("PRESENTATION");
254 td.setHorizontalAlignment(td.ALIGN_CENTER);
255 row.addCell(td);
256 row.addCell(new HtmlTableDataCell(request.getPresentationObjectPath()));
257 tbl.addRow(row);
258
259 row = new HtmlTableRow();
260 td = new HtmlTableDataCell("RELATIVE PRESENTATION");
261 td.setHorizontalAlignment(td.ALIGN_CENTER);
262 row.addCell(td);
263 row.addCell(new HtmlTableDataCell(request.getPresentationObjectRelativePath()));
264 tbl.addRow(row);
265
266 row = new HtmlTableRow();
267 td = new HtmlTableDataCell("QUERY_STRING");
268 td.setHorizontalAlignment(td.ALIGN_CENTER);
269 row.addCell(td);
270 row.addCell(new HtmlTableDataCell(request.getQueryString()));
271 tbl.addRow(row);
272
273 row = new HtmlTableRow();
274 td = new HtmlTableDataCell("REMOTE_HOST");
275 td.setHorizontalAlignment(td.ALIGN_CENTER);
276 row.addCell(td);
277 row.addCell(new HtmlTableDataCell(request.getRemoteHost()));
278 tbl.addRow(row);
279
280 row = new HtmlTableRow();
281 td = new HtmlTableDataCell("REMOTE_ADDR");
282 td.setHorizontalAlignment(td.ALIGN_CENTER);
283 row.addCell(td);
284 row.addCell(new HtmlTableDataCell(request.getRemoteAddr()));
285 tbl.addRow(row);
286
287 row = new HtmlTableRow();
288 td = new HtmlTableDataCell("AUTH_TYPE");
289 td.setHorizontalAlignment(td.ALIGN_CENTER);
290 row.addCell(td);
291 row.addCell(new HtmlTableDataCell(request.getAuthType()));
292 tbl.addRow(row);
293
294 row = new HtmlTableRow();
295 td = new HtmlTableDataCell("REMOTE_USER");
296 td.setHorizontalAlignment(td.ALIGN_CENTER);
297 row.addCell(td);
298 row.addCell(new HtmlTableDataCell(request.getRemoteUser()));
299 tbl.addRow(row);
300
301 row = new HtmlTableRow();
302 td = new HtmlTableDataCell("CONTENT_TYPE");
303 td.setHorizontalAlignment(td.ALIGN_CENTER);
304 row.addCell(td);
305 row.addCell(new HtmlTableDataCell(request.getContentType()));
306 tbl.addRow(row);
307
308 row = new HtmlTableRow();
309 td = new HtmlTableDataCell("CONTENT_LENGTH");
310 td.setHorizontalAlignment(td.ALIGN_CENTER);
311 row.addCell(td);
312 row.addCell(new HtmlTableDataCell(Integer.toString(request.getContentLength())));
313 tbl.addRow(row);
314 }
315
316 /**
317 * Adds request headers data rows to a table.
318 *
319 * @param request Object from which http request header data is compiled.
320 * @param httpRequestHeader List of header names that should be displayed.
321 * @param tbl Table to which to add the data.
322 * @exception Exception Throws all exceptions.
323 */
324 private static void addRequestHeaders(HttpPresentationRequest request,
325 String [] httpRequestHeader,
326 HtmlTable tbl)
327 throws Exception {
328 HtmlTableRow row;
329 HtmlTableDataCell td;
330 HtmlTableHeaderCell th;
331
332 row = new HtmlTableRow();
333 th = new HtmlTableHeaderCell("<FONT SIZE=+1>Request Headers</FONT>");
334 th.setColSpan(2);
335 th.setHorizontalAlignment(th.ALIGN_CENTER);
336 row.addCell(th);
337 row.setColSpan(2);
338 row.setBackgroundColor("#999999");
339 tbl.addRow(row);
340
341 row = new HtmlTableRow();
342 td = new HtmlTableDataCell("<B>Name</B>");
343 td.setHorizontalAlignment(td.ALIGN_CENTER);
344 row.addCell(td);
345 td = new HtmlTableDataCell("<B>Value</B>");
346 td.setHorizontalAlignment(td.ALIGN_LEFT);
347 row.addCell(td);
348 tbl.addRow(row);
349
350 for (int idx=0; idx<httpRequestHeader.length; idx++) {
351 row = new HtmlTableRow();
352 td = new HtmlTableDataCell(httpRequestHeader[idx]);
353 td.setHorizontalAlignment(td.ALIGN_CENTER);
354 row.addCell(td);
355 String val = request.getHeader(httpRequestHeader[idx]);
356 if (val != null) {
357 td = new HtmlTableDataCell(val);
358 row.addCell(td);
359 tbl.addRow(row);
360 }
361 }
362 }
363
364 /**
365 * Generates a table containing http request headers.
366 * Only headers that are set are displayed.
367 * The table is in html format.
368 *
369 * @param request from which http request header data is compiled.
370 * @param httpRequestHeader List that that specifies the
371 * set of headers that should be displayed.
372 * @exception Exception Throws all exceptions.
373 */
374 private static String getRequestHeaderTable(HttpPresentationRequest request,
375 String [] httpRequestHeader)
376 throws Exception {
377 HtmlTable tbl = new HtmlTable();
378 tbl.setBorderWidth(tableBorderWidth);
379 tbl.setPercentWidth(tablePercentWidth);
380 addRequestHeaders(request, httpRequestHeader, tbl);
381 return tbl.toString();
382 }
383
384 /**
385 * Generates a table containing http request header values.
386 * The table is in html format. Only headers that have values
387 * are displayed.
388 *
389 * @param request Object from which http request header data is compiled.
390 * @exception Exception Throws all exceptions.
391 */
392 private static String getRequestHeaderTable(HttpPresentationRequest request)
393 throws Exception {
394 return getRequestHeaderTable(request, httpRequestHeader);
395 }
396
397
398 /**
399 * Adds request cookie setting to table.
400 * The table is in html format.
401 *
402 * @param request Object from which http request header data is compiled.
403 * @param tbl Table to which to add the data.
404 * @exception Exception Throws all exceptions.
405 */
406 private static void addRequestCookies(HttpPresentationRequest request, HtmlTable tbl)
407 throws Exception{
408 HtmlTableRow row;
409 HtmlTableDataCell td;
410 HtmlTableHeaderCell th;
411
412 row = new HtmlTableRow();
413 th = new HtmlTableHeaderCell("<FONT SIZE=+1>Request Cookies</FONT>");
414 th.setColSpan(2);
415 th.setHorizontalAlignment(th.ALIGN_CENTER);
416 row.addCell(th);
417 row.setColSpan(2);
418 row.setBackgroundColor("#999999");
419 tbl.addRow(row);
420
421 row = new HtmlTableRow();
422 td = new HtmlTableDataCell("<B>Name</B>");
423 td.setHorizontalAlignment(td.ALIGN_CENTER);
424 row.addCell(td);
425 td = new HtmlTableDataCell("<B>Value</B>");
426 td.setHorizontalAlignment(td.ALIGN_LEFT);
427 row.addCell(td);
428 tbl.addRow(row);
429
430 Cookie [] cookies = request.getCookies();
431 if (cookies != null) {
432 for (int i=0; i<cookies.length; i++) {
433 row = new HtmlTableRow();
434 td = new HtmlTableDataCell(cookies[i].getName());
435 td.setHorizontalAlignment(td.ALIGN_CENTER);
436 row.addCell(td);
437 td = new HtmlTableDataCell(cookies[i].getValue());
438 td.setHorizontalAlignment(td.ALIGN_LEFT);
439 row.addCell(td);
440 tbl.addRow(row);
441 }
442 }
443 }
444
445 /**
446 * Generates a table containing http request cookie values.
447 * The table is in html format.
448 *
449 * @param request Object from which http request header data is compiled.
450 * @exception Exception Throws all exceptions.
451 */
452 private static String getRequestCookieTable(HttpPresentationRequest request)
453 throws Exception {
454 HtmlTable tbl = new HtmlTable();
455 tbl.setBorderWidth(tableBorderWidth);
456 tbl.setPercentWidth(tablePercentWidth);
457 addRequestCookies(request, tbl);
458 return tbl.toString();
459 }
460 }