KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > wEd


1 /* $Id: wEd.java,v 1.26 2001/04/27 17:28:31 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14 package webEditor;
15
16 import java.io.*;
17 import java.lang.reflect.*;
18 import java.util.*;
19
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import org.w3c.dom.*;
23
24 import webEditor.core.*;
25 import webEditor.util.*;
26 import webEditor.interfaces.*;
27
28 /**
29  * webEditor main class
30  *
31  * We will try to mantaing this class simply as a
32  * dispatcher for the rest of the classes in the package
33  *
34  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
35  */

36
37 public class wEd extends HttpServlet {
38
39     // Location of the general configuration file for this editor
40
// You must set this property to the proper value for your system
41
String JavaDoc wEd_root = null;
42
43     String JavaDoc tmpDir = null;
44
45     configuration initConfig = null;
46
47    /**
48     * Performs the initial variables load.
49     * @param initTable Initial values
50     * @return void
51     */

52     public void initialLoad (configuration initTable) {
53
54     /* Load conf parameteres into variables */
55     this.tmpDir = initTable.readValue ("directories", "tmpDir");
56     }
57
58
59    /**
60     * The servlet engine will call this method to handle the request.
61     * @param request HTTP request
62     * @param response HTTP response
63     * @return void
64     */

65     public void doGet (HttpServletRequest request, HttpServletResponse response)
66     throws ServletException,
67            IOException {
68         PrintWriter out;
69         HttpParameterParser myParser = new HttpParameterParser (request);
70         Hashtable parameters = new Hashtable ();
71         Hashtable headers = new Hashtable ();
72
73     // set content type and other response header fields first
74
response.setContentType("text/html");
75
76     // then write the data of the response
77
out = response.getWriter();
78
79         // Parse the conf file and load the conf values in a Hash table
80
this.wEd_root = this.getInitParameter("wEd_root");
81     if (this.wEd_root == null) {
82         // Ok, no wEd_root variable, so we rise an application Exception
83
this.wEd_error ("wEd_root environment variable not defined",out);
84         return;
85     }
86
87     // We check now for the existence of the configuration file
88
File checker = new File (this.wEd_root + "/general.xml");
89     if ( !checker.exists() ) {
90         this.wEd_error ("webEditor configuration file (" +
91                 this.wEd_root + "/general.xml) doesn't exists",out);
92         return;
93     }
94
95     this.initConfig = new configuration (this.wEd_root + "/general.xml");
96     this.initialLoad (this.initConfig);
97     // We are going to store the wEd_root variable in a new config category
98
this.initConfig.storeValue ("webEditor_Root", "wEd_root", this.wEd_root);
99
100     // Now, we check the servlet parameters & HTTP headers
101
parameters = myParser.getAllParameters (this.tmpDir);
102     this.initConfig.storeCategory ("HTTP Parameters", parameters);
103     headers = myParser.getAllHeaders ();
104     this.initConfig.storeCategory ("HTTP Headers", headers);
105
106
107     String JavaDoc opMode = this.initConfig.readValue ("HTTP Parameters", "mode");
108
109     if (opMode == null) {
110         // Default operation mode
111
opMode = "composition";
112     }
113
114 try {
115     // We use reflection to invoque the proper method
116
Method m = getClass().getMethod(opMode, new Class JavaDoc[] {HttpServletRequest.class, PrintWriter.class} );
117     m.invoke(this,new Object JavaDoc[] {request, out});
118 }
119 catch (Exception JavaDoc e) {
120     out.println ("Method not supported");
121 }
122
123     out.close();
124     }
125
126    /**
127     * The servlet engine will call this method to handle the request.
128     * @param request HTTP request
129     * @param response HTTP response
130     * @return void
131     */

132     public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException,
133            IOException
134     {
135         this.doGet (request , response);
136     }
137
138    /**
139     * Handler of the composition interface.
140     * @param request HTTP request
141     * @param out Output of the servlet
142     * @return void
143     */

144     public void composition (HttpServletRequest request, PrintWriter out)
145     throws ServletException, IOException
146     {
147     compositionMode myIndex = new compositionMode (this.initConfig);
148     myIndex.presentation (request, out);
149     }
150
151    /**
152     * Handler of the configuration interface.
153     * @param request HTTP request
154     * @param out Output of the servlet
155     * @return void
156     */

157     public void config (HttpServletRequest request, PrintWriter out)
158     throws ServletException, IOException
159     {
160         configMode myConfig = new configMode(this.initConfig);
161     myConfig.showConfig (request, out);
162     }
163
164    /**
165     * Handler of the edition interface.
166     * @param request HTTP request
167     * @param out Output of the servlet
168     * @return void
169     */

170     public void edit (HttpServletRequest request, PrintWriter out)
171     throws ServletException, IOException
172     {
173     editMode myEditor = new editMode (initConfig);
174     myEditor.edition (request, out);
175     }
176
177    /**
178     * Handler of the presentation interface.
179     * @param request HTTP request
180     * @param out Output of the servlet
181     * @return void
182     */

183     public void presentation (HttpServletRequest request, PrintWriter out)
184     throws ServletException, IOException
185     {
186     presentationMode myIndex = new presentationMode (this.initConfig);
187     myIndex.presentation (request, out);
188     }
189
190    /**
191     * Handler of the images interface.
192     * @param request HTTP request
193     * @param out Output of the servlet
194     * @return void
195     */

196     public void images (HttpServletRequest request, PrintWriter out)
197     throws ServletException, IOException
198     {
199     imageMode myImage = new imageMode (this.initConfig);
200     myImage.edition (request, out);
201     }
202
203    /**
204     * Shows the error screen
205     * @param request HTTP request
206     * @return void
207     */

208     public void wEd_error (String JavaDoc errorString, PrintWriter out)
209     {
210     String JavaDoc outputString = "<center><font face=\"Arial\" >" +
211                 errorString + "</font></center>";
212
213     out.println (outputString);
214     }
215
216 }
217
Popular Tags