01: // Copyright (c) 2002 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.servlet;
05:
06: import gnu.kawa.xml.*;
07: import java.io.*;
08: import javax.servlet.*;
09: import javax.servlet.http.*;
10:
11: /** A Consumer that sends output to a ServletResponse.
12: * This is the initial result destination when running a KawaServlet.
13: */
14:
15: public class ServletPrinter extends HttpPrinter {
16: HttpServletResponse response;
17:
18: public ServletPrinter(HttpServletResponse response)
19: throws IOException {
20: super (response.getOutputStream());
21: this .response = response;
22: }
23:
24: public ServletPrinter(OutputStream ostream) {
25: super (ostream);
26: }
27:
28: public void addHeader(String label, String value) {
29: if (label.equalsIgnoreCase("Content-type")) {
30: super .sawContentType = value;
31: response.setContentType(value);
32: } else if (label.equalsIgnoreCase("Status")) {
33: int lval = value.length();
34: int code = 0;
35: int i;
36: for (i = 0; i < lval; i++) {
37: if (i >= lval) {
38: try {
39: response.sendError(code);
40: } catch (java.io.IOException ex) {
41: System.err.println("caught " + ex);
42: }
43: break;
44: }
45: char ch = value.charAt(i);
46: int digit = Character.digit(ch, 10);
47: if (digit >= 0)
48: code = 10 * code + digit;
49: else {
50: if (ch == ' ')
51: i++;
52: try {
53: response.sendError(code, value.substring(i));
54: } catch (java.io.IOException ex) {
55: System.err.println("caught " + ex);
56: }
57: break;
58: }
59: }
60: } else
61: response.addHeader(label, value);
62: }
63:
64: public void printHeaders() {
65: }
66: }
|