1 5 package mc.formgenerator.servlets; 6 7 import mc.formgenerator.servlets.util.*; 8 9 import hero.util.HeroException; 10 11 import java.io.File ; 12 import java.io.FileNotFoundException ; 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.net.MalformedURLException ; 16 import java.net.URI ; 17 import java.rmi.RemoteException ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import hero.interfaces.*; 23 24 import javax.ejb.CreateException ; 25 import javax.naming.NamingException ; 26 import javax.security.auth.login.LoginException ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.TransformerException ; 35 36 import org.w3c.dom.Document ; 37 import org.xml.sax.SAXException ; 38 39 import mc.formgenerator.bonita.BonitaActivityAdapter; 40 import mc.formgenerator.bonita.BonitaActivityExecutor; 41 import mc.formgenerator.bonita.BonitaProjectAdapter; 42 import mc.formgenerator.bonita.BonitaProjectExecutor; 43 import mc.formgenerator.bonita.DocumentParser; 44 import mc.formgenerator.ggf.GGF; 45 46 import org.apache.log4j.Category; 47 import org.chiba.xml.xforms.config.Config; 48 import org.chiba.xml.xforms.exception.XFormsException; 49 import org.chiba.adapter.ChibaAdapter; 50 import org.chiba.adapter.web.ServletAdapter; 51 52 56 public class ServletBonitaForm extends HttpServlet { 57 58 private ServletAdapter servletAdapter = null; 60 61 private GGF ggf = null; 63 64 private Boolean isActivity; 66 67 private String projectName = null; 69 70 private String activityName = null; 72 73 private String mode = null; 75 76 private static Category cat = Category.getInstance(ServletView.class); 78 private static final String FORM_PARAM_NAME = "form"; 79 private static final String XSL_PARAM_NAME = "xslt"; 80 private static final String CSS_PARAM_NAME = "css"; 81 private static final String ACTIONURL_PARAM_NAME = "action_url"; 82 83 87 private String configPath = null; 89 90 private String contextRoot = null; 92 93 private String uploadDir = null; 95 96 private String stylesPath = null; 97 98 99 104 public String getServletInfo() { 105 return "Servlet Controller for Chiba Bonita's XForms Processor"; 106 } 107 108 111 public void destroy() { 112 } 113 114 115 120 private void xformProcessing(Document instance, PrintWriter writer, HttpServletRequest request, String actionURL, HttpSession session) 121 throws TransformerConfigurationException , XFormsException, 122 FileNotFoundException , TransformerException , IOException { 123 124 String xmlBase = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath(); 126 Document formDocument = this.ggf.runGGF(instance,xmlBase); 128 129 try{ 132 String xslFile = request.getParameter(XSL_PARAM_NAME); 133 String css = request.getParameter(CSS_PARAM_NAME); 134 this.servletAdapter = setupServletAdapter(actionURL, session, formDocument, xslFile, css); 135 updateContext(request, session, instance); 136 137 storeContextParams(request); 139 this.servletAdapter.init(); 140 this.servletAdapter.executeHandler(); 141 this.servletAdapter.buildUI(writer); 142 } catch (Exception e) { 143 e.printStackTrace(); 145 } 146 } 147 148 159 private void writingUI(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, String actionURL, HttpSession session) 160 throws LoginException , TransformerConfigurationException , 161 FileNotFoundException , TransformerException , 162 IOException , Exception { 163 164 ProjectSessionHome projecth = null; 165 166 ProjectSession project = null; 167 168 Document instance = null; 170 171 this.projectName = request.getParameter("projectName"); 173 174 this.activityName = request.getParameter("activityName"); 176 177 this.mode = request.getParameter("mode"); 179 180 String action = request.getParameter("action"); 182 183 if(this.projectName != null){ 185 186 if(this.activityName != null){ 188 189 if (action != null){ 190 191 if(action.equals("stop")){ 192 193 BonitaActivityExecutor bonitaActivityExecutor = new BonitaActivityExecutor(); 195 196 bonitaActivityExecutor.endActivity(this.activityName, this.projectName); 198 199 response.sendRedirect("/formgenerator/ServletWorkList"); 201 202 return; 203 } 204 else{ 205 this.isActivity = new Boolean (true); 206 207 BonitaActivityAdapter bonitaActivityAdapter = new BonitaActivityAdapter(); 209 210 instance = bonitaActivityAdapter.getActivityData(this.projectName, activityName); 212 } 213 } 214 else{ 215 this.isActivity = new Boolean (true); 216 217 BonitaActivityAdapter bonitaActivityAdapter = new BonitaActivityAdapter(); 219 220 instance = bonitaActivityAdapter.getActivityData(this.projectName, activityName); 222 } 223 } 224 else{ 225 226 this.isActivity = new Boolean (false); 228 229 230 234 projecth = ProjectSessionUtil.getHome(); 236 237 project = projecth.create(); 239 240 this.projectName=project.instantiateProject(this.projectName); 242 243 BonitaProjectAdapter bonitaProjectAdapter = new BonitaProjectAdapter(); 245 246 instance = bonitaProjectAdapter.getProjectData(this.projectName); 248 } 249 250 DocumentParser parser = new DocumentParser(); 251 252 if(parser.hasProperties(instance)){ 254 this.xformProcessing(instance, writer, request, actionURL, session); 256 } 257 else{ 258 this.callExecutor(instance); 260 if (request.getParameter("forward")!=null) 261 { 262 response.sendRedirect(request.getParameter("forward")); 263 } 264 else 265 { 266 if(this.isActivity.booleanValue() == true){ 268 269 if(this.mode.equals("consumer")) 270 response.sendRedirect("/formgenerator/ServletActivityToDoList?projectName=" + projectName); 272 else 273 response.sendRedirect("/formgenerator/ServletWorkList"); 275 } 276 else{ 277 if(this.mode.equals("consumer")) 278 response.sendRedirect("/formgenerator/ServletInstancesProjects"); 280 else 281 response.sendRedirect("/formgenerator/ServletWorkList"); 283 } 284 } 285 } 286 } 287 else 288 (new MessageServlet()).writeMessage(writer, "PARAMETER FOR projectName IS MISSING CANNOT PERFORM OPERATION"); 290 } 291 292 293 294 295 300 private void callExecutor(Document filledDocument) 301 throws MalformedURLException , SAXException , IOException , 302 RemoteException , NamingException , CreateException , HeroException{ 303 304 if(this.isActivity.booleanValue() == true){ 305 306 BonitaActivityExecutor bonitaActivityExecutor = new BonitaActivityExecutor(); 308 309 bonitaActivityExecutor.actOnActivity(filledDocument, this.mode); 311 } 312 else{ 313 314 BonitaProjectExecutor bonitaProjectExecutor = new BonitaProjectExecutor(); 316 317 bonitaProjectExecutor.startBonitaProject(filledDocument); 319 } 320 } 321 322 323 324 325 331 public void init(ServletConfig config) throws ServletException { 332 super.init(config); 333 334 cat.info("--------------- initing ChibaView... ---------------"); 335 contextRoot = getServletConfig().getServletContext().getRealPath(""); 337 if (contextRoot == null) 338 contextRoot = getServletConfig().getServletContext().getRealPath("."); 339 340 String path = getServletConfig().getInitParameter("chiba.config"); 342 343 if (path != null) { 345 configPath = getServletConfig().getServletContext().getRealPath(path); 346 } 347 348 path = getServletConfig().getInitParameter("chiba.xforms.stylesPath"); 351 352 if (path != null) { 354 stylesPath = getServletConfig().getServletContext().getRealPath(path); 355 cat.info("stylesPath: " + stylesPath); 356 } 357 358 uploadDir = getServletConfig().getInitParameter("chiba.upload"); 361 if (uploadDir != null) { 363 if (uploadDir.toUpperCase().indexOf("WEB-INF") >= 0) { 364 throw new ServletException ("Chiba security constraint: uploadDir '" + uploadDir + "' not allowed"); 365 } 366 } 367 } 368 369 370 371 372 376 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 377 378 this.servletAdapter = null; 379 380 HttpSession session = request.getSession(true); 382 383 String userLanguage = request.getHeader("Accept-Language"); 385 386 if (request.getParameter("forward")!=null) 388 session.setAttribute("forward",request.getParameter("forward")); 389 else 390 session.removeAttribute("forward"); 391 392 try { 393 response.setContentType("text/html"); 395 396 PrintWriter out = response.getWriter(); 398 399 String actionURL = getActionURL(request, response); 401 402 if (request.getParameter("transform")!=null){ 404 this.ggf = new GGF(this.contextRoot, actionURL,request.getParameter("transform"), userLanguage); 405 } 406 else this.ggf = new GGF(this.contextRoot, actionURL, userLanguage); 408 409 410 writingUI(out, request, response, actionURL, session); 412 413 session.setAttribute("formgenerator.GGF", this.ggf); 415 416 session.setAttribute("formgenerator.isActivity", this.isActivity); 418 419 session.setAttribute("formgenerator.projectName", this.projectName); 421 422 session.setAttribute("formgenerator.activityName", this.activityName); 424 425 session.setAttribute("formgenerator.mode", this.mode); 427 428 session.setAttribute("formgenerator.sAdapter", this.servletAdapter); 430 431 out.close(); 433 434 } catch (Exception e) { 435 e.printStackTrace(); 437 } 438 } 439 440 448 protected void updateContext(HttpServletRequest request, HttpSession session,Document formDocument) { 449 this.servletAdapter.setContextProperty(ServletAdapter.USERAGENT, request.getHeader("User-Agent")); 450 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SERVLET_REQUEST, request); 451 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SESSION_OBJECT, session); 452 this.servletAdapter.setContextProperty("instance",formDocument); 453 } 454 455 456 460 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 461 { 462 HttpSession session = request.getSession(true); 464 this.servletAdapter = null; 465 466 PrintWriter out = response.getWriter(); 468 469 try { 470 this.servletAdapter = (ServletAdapter)session.getAttribute("formgenerator.sAdapter"); 472 473 this.ggf = (GGF)session.getAttribute("formgenerator.GGF"); 475 session.setAttribute("formgenerator.GGF", this.ggf); 477 478 this.isActivity = (Boolean )session.getAttribute("formgenerator.isActivity"); 480 session.setAttribute("formgenerator.isActivity", this.isActivity); 482 483 this.projectName = (String )session.getAttribute("formgenerator.projectName"); 485 session.setAttribute("formgenerator.projectName", this.projectName); 487 488 this.activityName = (String )session.getAttribute("formgenerator.activityName"); 490 session.setAttribute("formgenerator.activityName", this.activityName); 492 493 this.mode = (String )session.getAttribute("formgenerator.mode"); 495 496 497 if ((this.servletAdapter == null)||(this.ggf == null)||(this.isActivity == null)||(this.projectName == null)){ 498 throw new IOException ("Invalid session - Cannot retrieve servletAdapter or ggf or isActivity or projectName in the session"); 500 } 501 502 updateContextPost(request, session); 503 504 this.servletAdapter.executeHandler(); 505 506 Document filledDocument = (Document )this.servletAdapter.getChibaBean().getContainer().getModel("model").getInstance("instance").getInstanceDocument(); 508 509 if (filledDocument == null) { 510 throw new ServletException (Config.getInstance().getErrorMessage("session-invalid")); 511 } 512 513 514 515 response.setContentType("text/html"); 517 518 521 522 this.callExecutor(filledDocument); 524 525 527 String forward = (String )session.getAttribute("forward"); 529 if ((forward!=null)&&!forward.equals("")) 530 response.sendRedirect(forward); 531 532 else 534 if(this.isActivity.booleanValue() == true){ 535 536 if(this.mode.equals("consumer")) 537 response.sendRedirect("/formgenerator/ServletActivityToDoList?projectName=" + projectName); 539 else 540 response.sendRedirect("/formgenerator/ServletWorkList"); 542 } 543 else{ 544 if(this.mode.equals("consumer")) 545 response.sendRedirect("/formgenerator/ServletInstancesProjects"); 547 else 548 response.sendRedirect("/formgenerator/ServletWorkList"); 550 } 551 552 553 this.servletAdapter.buildUI(out); 556 557 out.close(); 559 560 } catch (Exception e) { 561 e.printStackTrace(); 563 } 564 } 565 566 567 575 protected void updateContextPost(HttpServletRequest request, HttpSession session) { 576 this.servletAdapter.setContextProperty(ServletAdapter.USERAGENT, request.getHeader("User-Agent")); 577 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SERVLET_REQUEST, request); 578 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SESSION_OBJECT, session); 579 } 580 581 582 592 private ServletAdapter setupServletAdapter(String actionURL, 593 HttpSession session, 594 Document formDocument, 595 String xslFile, 596 String cssFile) { 597 ServletAdapter aAdapter = new ServletAdapter(); 599 aAdapter.setContextRoot(contextRoot); 600 if ((configPath != null) && !(configPath.equals(""))) { 601 aAdapter.setConfigPath(configPath); 602 } 603 aAdapter.setFormDocument(formDocument); 604 aAdapter.setStylesheetPath(stylesPath); 605 aAdapter.setActionUrl(actionURL); 606 aAdapter.setUploadDir(uploadDir); 607 608 if (xslFile != null) { 609 aAdapter.setStylesheet(xslFile); 610 if (cat.isDebugEnabled()) { 611 cat.debug("using xsl stylesheet: " + xslFile); 612 } 613 } 614 if (cssFile != null) { 615 aAdapter.setCSS(cssFile); 616 if (cat.isDebugEnabled()) { 617 cat.debug("using css stylesheet: " + cssFile); 618 } 619 } 620 621 Map servletMap = new HashMap (); 622 servletMap.put(ChibaAdapter.SESSION_ID, session.getId()); 623 aAdapter.setContextProperty(ChibaAdapter.SUBMISSION_RESPONSE, servletMap); 624 625 return aAdapter; 626 } 627 628 private void storeContextParams(HttpServletRequest request) { 629 Enumeration params = request.getParameterNames(); 630 String s; 631 while (params.hasMoreElements()) { 632 s = (String ) params.nextElement(); 633 if (!(s.equals(FORM_PARAM_NAME) || s.equals(XSL_PARAM_NAME) || s.equals(CSS_PARAM_NAME) || s.equals(ACTIONURL_PARAM_NAME))) { 635 String value = request.getParameter(s); 636 this.servletAdapter.setContextProperty(s, value); 637 if (cat.isDebugEnabled()) { 638 cat.debug("added request param '" + s + "' added to context"); 639 } 640 } 641 } 642 } 643 644 645 651 private String getActionURL(HttpServletRequest request, HttpServletResponse response) { 652 String defaultActionURL = 653 request.getScheme() 654 + "://" 655 + request.getServerName() 656 + ":" 657 + request.getServerPort() 658 + request.getContextPath() 659 + request.getServletPath(); 660 String encodedDefaultActionURL = response.encodeURL(defaultActionURL); 661 int sessIdx = encodedDefaultActionURL.indexOf(";jsession"); 662 String sessionId = null; 663 if (sessIdx > -1) { 664 sessionId = encodedDefaultActionURL.substring(sessIdx); 665 } 666 String actionURL = request.getParameter(ACTIONURL_PARAM_NAME); 667 if (null == actionURL) { 668 actionURL = encodedDefaultActionURL; 669 } else if (null != sessionId) { 670 actionURL += sessionId; 671 } 672 673 cat.info("actionURL: " + actionURL); 674 actionURL = response.encodeURL(actionURL); 676 return actionURL; 677 } 678 } 679 | Popular Tags |