Source code: com/voytechs/html/component/ServletFrame.java
1 /*
2 * File: ServletFrame.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: ServletFrame.java,v 1.1.1.1 2002/01/23 23:52:48 voytechs Exp $
6 ********************************************
7 * $Log: ServletFrame.java,v $
8 * Revision 1.1.1.1 2002/01/23 23:52:48 voytechs
9 * Initial public release, BETA 1.0 - voytechs
10 *
11 */
12 package com.voytechs.html.component;
13
14 import com.voytechs.html.io.HtmlWriter;
15 import com.voytechs.html.event.ServletDispatcher;
16 import com.voytechs.html.util.*;
17 import com.voytechs.html.event.EventException;
18 import com.voytechs.html.util.LogFacility;
19
20 import java.lang.*;
21 import java.util.*;
22 import java.io.IOException;
23
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 /**
28 * A ServletFrame object is a adaptor for Servlets. Lets a servlet initialize the top level
29 * frame using servlet specific parameters. Lower level objects do not know that servlet
30 * is actually controlling the HTML output. Also additional HTML specific functions such
31 * as HTTP MIME headers and other servlet specific or servlet bound resources are defined by
32 * overriding lower level object methods.
33 */
34 public class ServletFrame
35 extends Frame {
36 /* Internal attributes */
37
38 private ServletDispatcher servletDispatcher = null;
39 private ServletConfig servletConfig = null;
40 private String servletName = "NotSet";
41 private String servletPath = "/servlet";
42 private HttpServletRequest servletRequest = null;
43 private HttpServletResponse servletResponse = null;
44 private ServletAuthentication auth = null;
45
46 /**
47 * Sets up a default Servlet compatible dispatcher.
48 * @param servletName Name of the servlet. The name of the servlet in the URI.
49 * @param servletPath URI directory path component where the servlet is found/mapped.
50 * @param servletConfig The Serlvet Config object passed to the servlet via the init() method.
51 */
52 public ServletFrame(String servletName, String servletPath, ServletConfig servletConfig) {
53 super(new ServletDispatcher(), new ServletAuthentication());
54
55 servletDispatcher = (ServletDispatcher)getDispatcher();
56 auth = (ServletAuthentication)getAuthentication();
57
58 this.servletConfig = servletConfig;
59 this.servletName = servletName;
60 this.servletPath = servletPath;
61 // setUriPath(servletPath + "/" + servletName);
62 setUriPath(servletName);
63 }
64
65 /**
66 * Allows to set externaly defined dispatcher.
67 * @param servletName Name of the servlet. The name of the servlet in the URI.
68 * @param servletPath URI directory path component where the servlet is found/mapped.
69 * @param servletConfig The Serlvet Config object passed to the servlet via the init() method.
70 * @param dispatcher Externally defined event dispatcher.
71 * @param auth Externally defined authentication module for decoding servlet requests.
72 */
73 public ServletFrame(String servletName,
74 String servletPath,
75 ServletConfig servletConfig,
76 ServletDispatcher dispatcher,
77 ServletAuthentication auth) {
78 super(dispatcher, auth);
79
80 this.servletDispatcher = dispatcher;
81 this.auth = auth;
82
83 this.servletConfig = servletConfig;
84 this.servletName = servletName;
85 this.servletPath = servletPath;
86 // setUriPath(servletPath + "/" + servletName);
87 setUriPath(servletName);
88 }
89
90 /**
91 * Returns the name of this servlet.
92 */
93 public String getName() {
94 return(servletName);
95 }
96
97 /**
98 * Returns the directory path where the servlet is found/mapped by the web server.
99 */
100 public String getPath() {
101 return(servletPath);
102 }
103
104 /**
105 * Returns the servlet config object passed to the servlet's init() method.
106 */
107 public ServletConfig getServletConfig() {
108 return(servletConfig);
109 }
110
111 /**
112 * Returns the current raw servlet request object passed to the servlet doGet() and doPost() methods.
113 */
114 public HttpServletRequest getServletRequest() {
115 return(servletRequest);
116 }
117
118 /**
119 * Returns the current raw servlet response object passed to the servlet doGet() and doPost() methods.
120 */
121 public HttpServletResponse getServletResponse() {
122 return(servletResponse);
123 }
124
125 /**
126 * We are ready to finish initializing the component tree. Needs to be called after the
127 * component tree is build and all of the components are bound to their parents.
128 */
129 public void initTree() throws EventException {
130
131 super.init(this, getDispatcher());
132 }
133
134 /**
135 * When a servlet is called to either doGet() or doPost(), this method is called to
136 * allow our GUI engine and especially the dispatcher scan all of the parameters passed
137 * from the client and dispatch events to listeners as appropriate.
138 * @param req The original ServletRequest object passed to the servlet.
139 * @exception ServletException any servlet related errors with accessing the ServletRequest object.
140 * @exception EventException any dispatcher related errors. Malformed Events or internal dispatcher errors.
141 */
142 public void scanServletRequest(HttpServletRequest req)
143 throws ServletException, EventException {
144
145 /**
146 * Authentication module needs to scan the request before dispatcher so that events
147 * can be generated and sent to the dispatcher.
148 */
149 auth.scanServletRequest(req);
150
151
152 servletDispatcher.scanEvent(req);
153 }
154
155 /**
156 * This method is called by the servlet to generate a reponse to a HTTP request. The response is placed
157 * directly on the output stream found in the ServletResponse object. This is where the paint method
158 * of every GUI component is called to produce output on the output stream. The component tree is traversed
159 * and paint() of every component is called.
160 * @param res the origianl ServletResponse object passed to he servlet.
161 * @exception ServletException any servlet related errors with accessing the ServletRequest object.
162 * @exception EventException any dispatcher related errors. Malformed Events or internal dispatcher errors.
163 */
164 public void generateResponse(HttpServletResponse res)
165 throws ServletException, IOException {
166
167 LogFacility.log.println("ServletFrame::generateResponse()");
168
169 res.setContentType("text/html");
170 HtmlWriter out = new HtmlWriter(res.getOutputStream());
171
172 paint(out);
173
174 /* Flush the buffer out */
175 out.flush();
176 }
177
178 /**
179 * This method allows some kind of a pre-paint scan accross the component tree if desired. Other methods
180 * can be called from here before generateResponse() method is invoked.
181 * @param req The original ServletRequest object passed to the servlet.
182 * @param res The original ServletResponse object passed to the servlet.
183 * @exception ServletException any servlet related errors with accessing the ServletRequest object.
184 * @exception EventException any dispatcher related errors. Malformed Events or internal dispatcher errors.
185 */
186 public void preScan(HttpServletRequest req, HttpServletResponse res)
187 throws ServletException, IOException {
188
189 servletRequest = req;
190 servletResponse = res;
191
192 auth.preScan(req, res);
193 }
194
195 /**
196 * This method allows some kind of a post-paint scan accross the component tree if desired. Other methods
197 * can be called from here after generateResponse() method is invoked.
198 * @param res The original ServletResponse object passed to the servlet.
199 * @exception ServletException any servlet related errors with accessing the ServletRequest object.
200 * @exception EventException any dispatcher related errors. Malformed Events or internal dispatcher errors.
201 */
202 public void postScan(HttpServletResponse res)
203 throws ServletException, IOException {
204
205
206 /*
207 * Invalidate the servletResponse object because we are closing the output stream. Nothing more
208 * can writter to it. So if any component asks for it, null will be returned after this point.
209 */
210 servletResponse = null;
211 }
212
213 /**
214 * Test function for ServletFrame
215 * @param args command line arguments
216 */
217 public static void main(String [] args) {
218 }
219
220 } /* END OF: ServletFrame */