001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.rm.view.servlet;
021:
022: import java.io.*;
023: import java.util.logging.*;
024:
025: import javax.servlet.*;
026: import javax.servlet.http.*;
027:
028: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
029: import org.openharmonise.commons.net.MimeTypeMapping;
030: import org.openharmonise.rm.HarmoniseExceptionHandler;
031: import org.openharmonise.rm.config.ConfigSettings;
032: import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
033: import org.openharmonise.rm.factory.HarmoniseObjectFactory;
034: import org.openharmonise.rm.publishing.*;
035: import org.openharmonise.rm.publishing.renderers.impl.TextRenderer;
036: import org.openharmonise.rm.resources.publishing.WebPage;
037: import org.openharmonise.rm.sessions.SessionCleaner;
038: import org.openharmonise.rm.tasks.Scheduler;
039: import org.openharmonise.rm.view.servlet.utils.HttpRequestManager;
040:
041: /**
042: * HarmoniseServlet is the central point of entry to the Harmonise system via the web.
043: *
044: * @author Michael Bell
045: * @version $Revision: 1.2 $
046: *
047: */
048: public class HarmoniseServlet extends HttpServlet {
049:
050: private static final int LOGON_FAILED_PAGE = 1;
051: static final String ALLOW_SHOWXML_PNAME = "ALLOW_SHOWXML";
052:
053: public static String PNAME_SCHEDULER = "SCHEDULER";
054:
055: protected AbstractDataStoreInterface m_dbintrf = null;
056: protected Scheduler m_scheduler = null;
057: protected SessionCleaner m_sessionCleaner = null;
058:
059: /**
060: * <code>String</code> constant for the configuration parameter that sets
061: * the maximum file size for upload
062: */
063: static final String MAXFILESIZE_PNAME = "MAX_UPLOADED_FILESIZE_MB";
064:
065: /**
066: * <code>String</code> constant for the configuration parameter that sets
067: * the directory which uploaded files are uploaded to
068: */
069: static final String TEMPDIR_PNAME = "UPLOADED_TEMPFILE_DIR";
070: private boolean m_allow_show_xml = true;
071:
072: /**
073: * Logger for this class
074: */
075: private static final Logger m_logger = Logger
076: .getLogger(HarmoniseServlet.class.getName());
077:
078: /**
079: * Initialize global variables
080: */
081: public void init(ServletConfig config) throws ServletException {
082: super .init(config);
083:
084: try {
085: m_dbintrf = DataStoreInterfaceFactory
086: .getDataStoreInterface();
087:
088: //TODO should move this session cleaner code elsewhere if poss
089: m_sessionCleaner = SessionCleaner.getInstance(m_dbintrf);
090:
091: Thread session_thread = new Thread(m_sessionCleaner);
092: session_thread.start();
093:
094: m_allow_show_xml = ConfigSettings.getBoolProperty(
095: ALLOW_SHOWXML_PNAME, "true");
096:
097: //TODO should move the control of this thread out of the servlet
098: if (ConfigSettings.getProperty(PNAME_SCHEDULER, "OFF")
099: .equals("ON")) {
100: m_scheduler = Scheduler.instance(m_dbintrf);
101:
102: Thread schedule_thread = new Thread(m_scheduler);
103: schedule_thread.start();
104:
105: }
106:
107: } catch (Exception e) {
108: throw new ServletException(e.getMessage());
109: }
110: }
111:
112: /* (non-Javadoc)
113: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
114: */
115: public void doGet(HttpServletRequest request,
116: HttpServletResponse response) throws ServletException,
117: IOException {
118: doPost(request, response);
119: }
120:
121: /* (non-Javadoc)
122: * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
123: */
124: public void doPost(HttpServletRequest request,
125: HttpServletResponse response) throws ServletException,
126: IOException {
127: OutputStream outstream = response.getOutputStream();
128:
129: try {
130:
131: int max_filesize_mb = ConfigSettings.getIntProperty(
132: MAXFILESIZE_PNAME, "1");
133: int m_MAX_FILESIZE = max_filesize_mb * 1024 * 1024;
134:
135: String m_TEMP_FILE_DIR = ConfigSettings.getProperty(
136: TEMPDIR_PNAME, "c:\\Harmonise\\temp");
137:
138: String sSchedule = "status";
139: sSchedule = request.getParameter("scheduler");
140:
141: if (sSchedule == null) {
142: int nPage = 1;
143: WebPageEngine pageEngine = null;
144: WebPage page = null;
145: HttpRequestManager req_mgr = null;
146: String sShowXML = "state";
147: sShowXML = request.getParameter("show_xml");
148:
149: req_mgr = new HttpRequestManager(request,
150: m_MAX_FILESIZE, m_TEMP_FILE_DIR);
151:
152: State state = new State(m_dbintrf);
153:
154: state.populate(req_mgr);
155:
156: nPage = state.getCurrentPageId();
157:
158: String sSessionId = state.getSessionId();
159:
160: if ((sSessionId == null) || (sSessionId.length() == 0)) {
161: //Create a WebPageEngine with a brand new session
162: pageEngine = new WebPageEngine(this .m_dbintrf);
163: pageEngine.assignSessionId(state);
164: } else {
165: //Create a WebPageEngine with the current session
166: pageEngine = WebPageEngineCache.getInstance(
167: m_dbintrf).getWebPageEngine(sSessionId);
168: }
169:
170: page = (WebPage) HarmoniseObjectFactory
171: .instantiateHarmoniseObject(this .m_dbintrf,
172: WebPage.class.getName(), nPage);
173:
174: if (sShowXML == null) {
175: response.setContentType(page.getXSL()
176: .getOutputType());
177: pageEngine.render(page, state, outstream);
178: } else {
179:
180: if (m_allow_show_xml == false) {
181: throw new RuntimeException(
182: "Cannot show_xml, property:"
183: + ALLOW_SHOWXML_PNAME + " is "
184: + m_allow_show_xml
185: + " violating address:"
186: + request.getRemoteAddr());
187: } else if (sShowXML.equalsIgnoreCase("state") == true) {
188: response.setContentType(MimeTypeMapping.XML
189: .getMimeType());
190: TextRenderer.render(state, outstream);
191: } else if (sShowXML.equalsIgnoreCase("xmloutput") == true
192: || sShowXML.equalsIgnoreCase("xml") == true) {
193: response.setContentType(MimeTypeMapping.XML
194: .getMimeType());
195: TextRenderer textRenderer = new TextRenderer();
196: pageEngine.render(page, state, textRenderer,
197: outstream);
198: } else if (sShowXML.equals("original") == true
199: || sShowXML.equals("xmloriginal") == true) {
200: response.setContentType(MimeTypeMapping.XML
201: .getMimeType());
202: TextRenderer.render(
203: page.getXML().getDocument(), outstream);
204: } else {
205: //let page engine deal with it of sShowXML wasn't recognised
206: response.setContentType(page.getXSL()
207: .getOutputType());
208: pageEngine.render(page, state, outstream);
209: }
210: }
211:
212: } else if (sSchedule != null) {
213: //TODO should move this out of the servlet
214: OutputStreamWriter outwriter = new OutputStreamWriter(
215: outstream, "UTF-8");
216: PrintWriter out = new PrintWriter(outwriter);
217: out.println("<html><head></head><body>");
218: out.println("<h1>Schedule Info</h1>");
219:
220: if (m_scheduler != null) {
221: if (sSchedule.equalsIgnoreCase("init")) {
222: if (m_scheduler.isRunning() == false) {
223: Thread thread = new Thread(m_scheduler);
224: thread.start();
225: }
226:
227: out.println("<p>Scheduler started.....</p>");
228: } else if (sSchedule.equalsIgnoreCase("status")) {
229: out.println(m_scheduler.getStatusReport(false));
230: } else if (sSchedule.equalsIgnoreCase("status_v")) {
231: out.println(m_scheduler.getStatusReport(true));
232: } else if (sSchedule.equalsIgnoreCase("stop")) {
233: if (m_scheduler.isRunning() == true) {
234: m_scheduler.stopRunning();
235: out
236: .println("<p>Scheduler stopped.....</p>");
237: }
238: } else if (sSchedule.equalsIgnoreCase("clear")) {
239: m_scheduler.clearExceptionHistory();
240: out
241: .println("<p>Exception history has been cleared.</p>");
242: }
243: } else {
244: out.println("<p>Scheduler not started.....</p>");
245: }
246:
247: out.println("</body></html>");
248: if (out != null) {
249: out.close();
250: }
251: }
252:
253: } catch (Throwable e) {
254: //catching all throwable so that we can control what the user sees
255: //in almost all cases
256: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
257:
258: OutputStreamWriter outwriter = new OutputStreamWriter(
259: outstream, "UTF-8");
260: PrintWriter out = new PrintWriter(outwriter);
261:
262: try {
263: String additional_info = "";
264: HarmoniseExceptionHandler e_handler = new HarmoniseExceptionHandler();
265: e_handler.handle(additional_info, e, request, response,
266: out);
267: } catch (Exception ex) {
268: out.println("Exception whilst handling exception:");
269: out.println(ex);
270: ex.printStackTrace(out);
271: response
272: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
273: }
274:
275: } finally {
276:
277: outstream.close();
278:
279: }
280: }
281:
282: /* (non-Javadoc)
283: * @see javax.servlet.Servlet#getServletInfo()
284: */
285: public String getServletInfo() {
286: return "This is the Harmonise Servlet";
287: }
288:
289: /* (non-Javadoc)
290: * @see javax.servlet.Servlet#destroy()
291: */
292: public void destroy() {
293: m_scheduler.stopRunning();
294: m_sessionCleaner.stopRunning();
295: }
296:
297: }
|