1 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 49 public class HarmoniseServlet extends HttpServlet { 50 51 private static final int LOGON_FAILED_PAGE = 1; 52 static final String ALLOW_SHOWXML_PNAME = "ALLOW_SHOWXML"; 53 54 public static String PNAME_SCHEDULER = "SCHEDULER"; 55 56 protected AbstractDataStoreInterface m_dbintrf = null; 57 protected Scheduler m_scheduler = null; 58 protected SessionCleaner m_sessionCleaner = null; 59 60 64 static final String MAXFILESIZE_PNAME = "MAX_UPLOADED_FILESIZE_MB"; 65 66 70 static final String TEMPDIR_PNAME = "UPLOADED_TEMPFILE_DIR"; 71 private boolean m_allow_show_xml = true; 72 73 76 private static final Logger m_logger = Logger.getLogger(HarmoniseServlet.class.getName()); 77 78 81 public void init(ServletConfig config) throws ServletException { 82 super.init(config); 83 84 try { 85 m_dbintrf = DataStoreInterfaceFactory.getDataStoreInterface(); 86 87 m_sessionCleaner = SessionCleaner.getInstance(m_dbintrf); 89 90 Thread session_thread = new Thread (m_sessionCleaner); 91 session_thread.start(); 92 93 m_allow_show_xml = 94 ConfigSettings.getBoolProperty( 95 ALLOW_SHOWXML_PNAME, 96 "true"); 97 98 if (ConfigSettings 100 .getProperty(PNAME_SCHEDULER, "OFF") 101 .equals("ON")) { 102 m_scheduler = Scheduler.instance(m_dbintrf); 103 104 Thread schedule_thread = new Thread (m_scheduler); 105 schedule_thread.start(); 106 107 } 108 109 } catch (Exception e) { 110 throw new ServletException(e.getMessage()); 111 } 112 } 113 114 117 public void doGet(HttpServletRequest request, HttpServletResponse response) 118 throws ServletException, IOException { 119 doPost(request, response); 120 } 121 122 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 m_TEMP_FILE_DIR = 140 ConfigSettings.getProperty( 141 TEMPDIR_PNAME, 142 "c:\\Harmonise\\temp"); 143 144 String 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 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 sSessionId = state.getSessionId(); 164 165 if ((sSessionId == null) || (sSessionId.length() == 0)) { 166 pageEngine = new WebPageEngine(this.m_dbintrf); 168 pageEngine.assignSessionId(state); 169 } else { 170 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 ( 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 response.setContentType(page.getXSL().getOutputType()); 215 pageEngine.render(page, state, outstream); 216 } 217 } 218 219 220 } else if (sSchedule != null) { 221 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 thread = new Thread (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 e) { 261 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 additional_info = ""; 271 HarmoniseExceptionHandler e_handler = new HarmoniseExceptionHandler(); 272 e_handler.handle(additional_info, e, request, response, out); 273 } catch (Exception 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 290 public String getServletInfo() { 291 return "This is the Harmonise Servlet"; 292 } 293 294 297 public void destroy() { 298 m_scheduler.stopRunning(); 299 m_sessionCleaner.stopRunning(); 300 } 301 302 } | Popular Tags |