KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > view > servlet > HarmoniseServlet


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.rm.view.servlet;
21
22 import java.io.*;
23 import java.util.logging.*;
24
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
29 import org.openharmonise.commons.net.MimeTypeMapping;
30 import org.openharmonise.rm.HarmoniseExceptionHandler;
31 import org.openharmonise.rm.config.ConfigSettings;
32 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
33 import org.openharmonise.rm.factory.HarmoniseObjectFactory;
34 import org.openharmonise.rm.publishing.*;
35 import org.openharmonise.rm.publishing.renderers.impl.TextRenderer;
36 import org.openharmonise.rm.resources.publishing.WebPage;
37 import org.openharmonise.rm.sessions.SessionCleaner;
38 import org.openharmonise.rm.tasks.Scheduler;
39 import org.openharmonise.rm.view.servlet.utils.HttpRequestManager;
40
41
42 /**
43  * HarmoniseServlet is the central point of entry to the Harmonise system via the web.
44  *
45  * @author Michael Bell
46  * @version $Revision: 1.2 $
47  *
48  */

49 public class HarmoniseServlet extends HttpServlet {
50
51     private static final int LOGON_FAILED_PAGE = 1;
52     static final String JavaDoc ALLOW_SHOWXML_PNAME = "ALLOW_SHOWXML";
53
54     public static String JavaDoc PNAME_SCHEDULER = "SCHEDULER";
55
56     protected AbstractDataStoreInterface m_dbintrf = null;
57     protected Scheduler m_scheduler = null;
58     protected SessionCleaner m_sessionCleaner = null;
59     
60     /**
61      * <code>String</code> constant for the configuration parameter that sets
62      * the maximum file size for upload
63      */

64     static final String JavaDoc MAXFILESIZE_PNAME = "MAX_UPLOADED_FILESIZE_MB";
65     
66     /**
67      * <code>String</code> constant for the configuration parameter that sets
68      * the directory which uploaded files are uploaded to
69      */

70     static final String JavaDoc TEMPDIR_PNAME = "UPLOADED_TEMPFILE_DIR";
71     private boolean m_allow_show_xml = true;
72     
73     /**
74      * Logger for this class
75      */

76     private static final Logger m_logger = Logger.getLogger(HarmoniseServlet.class.getName());
77
78     /**
79      * Initialize global variables
80      */

81     public void init(ServletConfig config) throws ServletException {
82         super.init(config);
83
84         try {
85             m_dbintrf = DataStoreInterfaceFactory.getDataStoreInterface();
86
87             //TODO should move this session cleaner code elsewhere if poss
88
m_sessionCleaner = SessionCleaner.getInstance(m_dbintrf);
89
90             Thread JavaDoc session_thread = new Thread JavaDoc(m_sessionCleaner);
91             session_thread.start();
92
93             m_allow_show_xml =
94                 ConfigSettings.getBoolProperty(
95                     ALLOW_SHOWXML_PNAME,
96                     "true");
97
98             //TODO should move the control of this thread out of the servlet
99
if (ConfigSettings
100                 .getProperty(PNAME_SCHEDULER, "OFF")
101                 .equals("ON")) {
102                 m_scheduler = Scheduler.instance(m_dbintrf);
103
104                 Thread JavaDoc schedule_thread = new Thread JavaDoc(m_scheduler);
105                 schedule_thread.start();
106                 
107             }
108
109         } catch (Exception JavaDoc e) {
110             throw new ServletException(e.getMessage());
111         }
112     }
113
114     /* (non-Javadoc)
115      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
116      */

117     public void doGet(HttpServletRequest request, HttpServletResponse response)
118         throws ServletException, IOException {
119         doPost(request, response);
120     }
121
122     /* (non-Javadoc)
123      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
124      */

125     public void doPost(
126         HttpServletRequest request,
127         HttpServletResponse response)
128         throws ServletException, IOException {
129         OutputStream outstream = response.getOutputStream();
130
131         try {
132             
133             int max_filesize_mb =
134                 ConfigSettings.getIntProperty(
135                     MAXFILESIZE_PNAME,
136                     "1");
137             int m_MAX_FILESIZE = max_filesize_mb * 1024 * 1024;
138
139             String JavaDoc m_TEMP_FILE_DIR =
140                 ConfigSettings.getProperty(
141                     TEMPDIR_PNAME,
142                     "c:\\Harmonise\\temp");
143
144             String JavaDoc sSchedule = "status";
145             sSchedule = request.getParameter("scheduler");
146
147             if (sSchedule == null) {
148                 int nPage = 1;
149                 WebPageEngine pageEngine = null;
150                 WebPage page = null;
151                 HttpRequestManager req_mgr = null;
152                 String JavaDoc sShowXML = "state";
153                 sShowXML = request.getParameter("show_xml");
154
155                 req_mgr = new HttpRequestManager(request, m_MAX_FILESIZE, m_TEMP_FILE_DIR);
156
157                 State state = new State(m_dbintrf);
158
159                 state.populate(req_mgr);
160
161                 nPage = state.getCurrentPageId();
162
163                 String JavaDoc sSessionId = state.getSessionId();
164
165                 if ((sSessionId == null) || (sSessionId.length() == 0)) {
166                     //Create a WebPageEngine with a brand new session
167
pageEngine = new WebPageEngine(this.m_dbintrf);
168                     pageEngine.assignSessionId(state);
169                 } else {
170                     //Create a WebPageEngine with the current session
171
pageEngine =
172                         WebPageEngineCache.getInstance(
173                             m_dbintrf).getWebPageEngine(
174                             sSessionId);
175                 }
176
177                 page =
178                     (WebPage) HarmoniseObjectFactory.instantiateHarmoniseObject(
179                         this.m_dbintrf,
180                         WebPage.class.getName(),
181                         nPage);
182
183                 if (sShowXML == null) {
184                     response.setContentType(page.getXSL().getOutputType());
185                     pageEngine.render(page, state, outstream);
186                 } else {
187
188                     if (m_allow_show_xml == false) {
189                         throw new RuntimeException JavaDoc(
190                             "Cannot show_xml, property:"
191                                 + ALLOW_SHOWXML_PNAME
192                                 + " is "
193                                 + m_allow_show_xml
194                                 + " violating address:"
195                                 + request.getRemoteAddr());
196                     } else if (sShowXML.equalsIgnoreCase("state") == true) {
197                         response.setContentType(MimeTypeMapping.XML.getMimeType());
198                         TextRenderer.render(state, outstream);
199                     } else if (
200                         sShowXML.equalsIgnoreCase("xmloutput") == true
201                             || sShowXML.equalsIgnoreCase("xml") == true) {
202                         response.setContentType(MimeTypeMapping.XML.getMimeType());
203                         TextRenderer textRenderer = new TextRenderer();
204                         pageEngine.render(page, state, textRenderer, outstream);
205                     } else if (
206                         sShowXML.equals("original") == true
207                             || sShowXML.equals("xmloriginal") == true) {
208                                 response.setContentType(MimeTypeMapping.XML.getMimeType());
209                         TextRenderer.render(
210                             page.getXML().getDocument(),
211                             outstream);
212                     } else {
213                         //let page engine deal with it of sShowXML wasn't recognised
214
response.setContentType(page.getXSL().getOutputType());
215                         pageEngine.render(page, state, outstream);
216                     }
217                 }
218                 
219
220             } else if (sSchedule != null) {
221                 //TODO should move this out of the servlet
222
OutputStreamWriter outwriter =
223                     new OutputStreamWriter(outstream, "UTF-8");
224                 PrintWriter out = new PrintWriter(outwriter);
225                 out.println("<html><head></head><body>");
226                 out.println("<h1>Schedule Info</h1>");
227
228                 if (m_scheduler != null) {
229                     if (sSchedule.equalsIgnoreCase("init")) {
230                         if (m_scheduler.isRunning() == false) {
231                             Thread JavaDoc thread = new Thread JavaDoc(m_scheduler);
232                             thread.start();
233                         }
234
235                         out.println("<p>Scheduler started.....</p>");
236                     } else if (sSchedule.equalsIgnoreCase("status")) {
237                         out.println(m_scheduler.getStatusReport(false));
238                     } else if (sSchedule.equalsIgnoreCase("status_v")) {
239                         out.println(m_scheduler.getStatusReport(true));
240                     } else if (sSchedule.equalsIgnoreCase("stop")) {
241                         if (m_scheduler.isRunning() == true) {
242                             m_scheduler.stopRunning();
243                             out.println("<p>Scheduler stopped.....</p>");
244                         }
245                     } else if (sSchedule.equalsIgnoreCase("clear")) {
246                         m_scheduler.clearExceptionHistory();
247                         out.println(
248                             "<p>Exception history has been cleared.</p>");
249                     }
250                 } else {
251                     out.println("<p>Scheduler not started.....</p>");
252                 }
253
254                 out.println("</body></html>");
255                 if (out != null) {
256                     out.close();
257                 }
258             }
259
260         } catch (Throwable JavaDoc e) {
261             //catching all throwable so that we can control what the user sees
262
//in almost all cases
263
m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
264
265             OutputStreamWriter outwriter =
266                 new OutputStreamWriter(outstream, "UTF-8");
267             PrintWriter out = new PrintWriter(outwriter);
268
269             try {
270                 String JavaDoc additional_info = "";
271                 HarmoniseExceptionHandler e_handler = new HarmoniseExceptionHandler();
272                 e_handler.handle(additional_info, e, request, response, out);
273             } catch (Exception JavaDoc ex) {
274                 out.println("Exception whilst handling exception:");
275                 out.println(ex);
276                 ex.printStackTrace(out);
277                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
278             }
279             
280         } finally {
281
282             outstream.close();
283
284         }
285     }
286
287     /* (non-Javadoc)
288      * @see javax.servlet.Servlet#getServletInfo()
289      */

290     public String JavaDoc getServletInfo() {
291         return "This is the Harmonise Servlet";
292     }
293
294     /* (non-Javadoc)
295      * @see javax.servlet.Servlet#destroy()
296      */

297     public void destroy() {
298         m_scheduler.stopRunning();
299         m_sessionCleaner.stopRunning();
300     }
301
302 }
Popular Tags