1 5 package mc.formgenerator.servlets; 6 7 import hero.util.HeroException; 9 10 import java.io.File ; 11 import java.io.FileNotFoundException ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.net.MalformedURLException ; 15 import java.rmi.RemoteException ; 17 import java.util.Enumeration ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import hero.interfaces.*; 21 22 import javax.ejb.CreateException ; 23 import javax.naming.NamingException ; 24 import javax.security.auth.login.LoginException ; 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 34 import mc.formgenerator.bonita.BonitaActivityAdapter; 35 import mc.formgenerator.bonita.BonitaActivityExecutor; 36 import mc.formgenerator.bonita.BonitaProjectAdapter; 37 import mc.formgenerator.bonita.BonitaProjectExecutor; 38 import mc.formgenerator.bonita.DocumentParser; 39 import mc.formgenerator.ggf.GGF; 40 import mc.formgenerator.servlets.util.MessageServlet; 41 42 import org.chiba.adapter.web.ServletAdapter; 43 import org.chiba.adapter.ChibaAdapter; 44 import org.chiba.xml.xforms.config.Config; 45 import org.chiba.xml.xforms.exception.XFormsException; 46 import org.w3c.dom.Document ; 47 import org.xml.sax.SAXException ; 48 49 import org.apache.log4j.Category; 50 51 55 public class ServletView extends HttpServlet { 56 57 private ServletAdapter servletAdapter = null; 59 60 private GGF ggf = null; 62 63 private Boolean isActivity; 65 66 private String projectName = null; 68 69 private String activityName = null; 71 72 private String mode = null; 74 75 private static Category cat = Category.getInstance(ServletView.class); 77 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 86 private String configPath = null; 88 89 private String contextRoot = null; 91 92 private String uploadDir = null; 94 95 private String stylesPath = null; 96 97 98 103 public String getServletInfo() { 104 return "Servlet Controller for Chiba XForms Processor"; 105 } 106 107 110 public void destroy() { 111 } 112 113 118 private void xformProcessing(Document instance, PrintWriter writer, HttpServletRequest request, String actionURL, HttpSession session) 119 throws TransformerConfigurationException , XFormsException, 120 FileNotFoundException , TransformerException , IOException { 121 122 try{ 124 String formPath = request.getParameter(FORM_PARAM_NAME); 126 System.out.println("===> formpath: "+formPath); 128 129 if (formPath == null) { 130 throw new IOException ("File: " + formPath + " not found"); 131 } 132 133 String xslFile = request.getParameter(XSL_PARAM_NAME); 134 String css = request.getParameter(CSS_PARAM_NAME); 135 this.servletAdapter = setupServletAdapter(actionURL, session, formPath, xslFile, css); 136 updateContext(request, session, instance); 137 138 storeContextParams(request); 140 this.servletAdapter.init(); 141 this.servletAdapter.executeHandler(); 142 this.servletAdapter.buildUI(writer); 143 } catch (Exception e) { 144 e.printStackTrace(); 146 } 147 } 148 149 160 private void writingUI(PrintWriter writer, HttpServletRequest request, HttpServletResponse response, String actionURL, HttpSession session) 161 throws LoginException , TransformerConfigurationException , 162 FileNotFoundException , TransformerException , 163 IOException , Exception { 164 165 ProjectSessionHome projecth = null; 166 167 ProjectSession project = null; 168 169 Document instance = null; 171 172 this.projectName = request.getParameter("projectName"); 174 175 this.activityName = request.getParameter("activityName"); 177 178 this.mode = request.getParameter("mode"); 180 181 String action = request.getParameter("action"); 183 184 if(this.projectName != null){ 186 187 if(this.activityName != null){ 189 190 if (action != null){ 191 192 if(action.equals("stop")){ 193 194 BonitaActivityExecutor bonitaActivityExecutor = new BonitaActivityExecutor(); 196 197 bonitaActivityExecutor.endActivity(this.activityName, this.projectName); 199 200 response.sendRedirect("/formgenerator/ServletWorkList"); 202 203 return; 204 } 205 else{ 206 this.isActivity = new Boolean (true); 207 208 BonitaActivityAdapter bonitaActivityAdapter = new BonitaActivityAdapter(); 210 211 instance = bonitaActivityAdapter.getActivityData(this.projectName, activityName); 213 } 214 } 215 else{ 216 this.isActivity = new Boolean (true); 217 218 BonitaActivityAdapter bonitaActivityAdapter = new BonitaActivityAdapter(); 220 221 instance = bonitaActivityAdapter.getActivityData(this.projectName, activityName); 223 System.out.println("xformsDocument Act : "+GGF.toString(instance)); 224 } 225 } 226 else{ 227 228 this.isActivity = new Boolean (false); 230 231 projecth = ProjectSessionUtil.getHome(); 233 234 project = projecth.create(); 236 237 this.projectName=project.instantiateProject(this.projectName); 239 240 BonitaProjectAdapter bonitaProjectAdapter = new BonitaProjectAdapter(); 242 243 instance = bonitaProjectAdapter.getProjectData(this.projectName); 245 System.out.println("xformsDocument Proj : "+GGF.toString(instance)); 246 } 247 248 DocumentParser parser = new DocumentParser(); 249 250 if(parser.hasProperties(instance)){ 252 this.xformProcessing(instance, writer, request, actionURL, session); 254 } 255 else{ 256 this.callExecutor(instance); 258 if (request.getParameter("forward")!=null) 259 { 260 response.sendRedirect(request.getParameter("forward")); 261 } 262 else 263 { 264 if(this.isActivity.booleanValue() == true){ 266 267 if(this.mode.equals("consumer")) 268 response.sendRedirect("/formgenerator/ServletActivityToDoList?projectName=" + projectName); 270 else 271 response.sendRedirect("/formgenerator/ServletWorkList"); 273 } 274 else{ 275 if(this.mode.equals("consumer")) 276 response.sendRedirect("/formgenerator/ServletInstancesProjects"); 278 else 279 response.sendRedirect("/formgenerator/ServletWorkList"); 281 } 282 } 283 } 284 } 285 else 286 (new MessageServlet()).writeMessage(writer, "PARAMETER FOR projectName IS MISSING CANNOT PERFORM OPERATION"); 288 } 289 290 291 292 293 298 private void callExecutor(Document filledDocument) 299 throws MalformedURLException , SAXException , IOException , 300 RemoteException , NamingException , CreateException , HeroException{ 301 302 System.out.println("===> callExecutor test"); 303 if(this.isActivity.booleanValue() == true){ 304 305 BonitaActivityExecutor bonitaActivityExecutor = new BonitaActivityExecutor(); 307 308 bonitaActivityExecutor.actOnActivity(filledDocument, this.mode); 310 } 311 else{ 312 313 BonitaProjectExecutor bonitaProjectExecutor = new BonitaProjectExecutor(); 315 316 bonitaProjectExecutor.startBonitaProject(filledDocument); 318 } 319 } 320 321 327 public void init(ServletConfig config) throws ServletException { 328 super.init(config); 329 330 contextRoot = getServletConfig().getServletContext().getRealPath(""); 332 if (contextRoot == null) 333 contextRoot = getServletConfig().getServletContext().getRealPath("."); 334 System.out.println("===> contextRoot: "+ contextRoot); 335 336 String path = getServletConfig().getInitParameter("chiba.config"); 338 339 if (path != null) { 341 configPath = getServletConfig().getServletContext().getRealPath(path); 342 } 343 System.out.println("===> configpath : "+ configPath); 344 345 path = getServletConfig().getInitParameter("chiba.xforms.stylesPath"); 348 349 if (path != null) { 351 stylesPath = getServletConfig().getServletContext().getRealPath(path); 352 cat.info("stylesPath: " + stylesPath); 353 } 354 System.out.println("===> stylespath : "+ stylesPath); 355 356 uploadDir = getServletConfig().getInitParameter("chiba.upload"); 359 System.out.println("===> uploadDir : "+ uploadDir); 360 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 374 protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException , IOException 375 { 376 System.out.println("===> GET"); 377 378 this.servletAdapter = null; 379 HttpSession session = request.getSession(true); 380 381 String userLanguage = request.getHeader("Accept-Language"); 383 System.out.println("===> userLanguage : "+userLanguage); 384 385 if (request.getParameter("forward")!=null) 387 session.setAttribute("forward",request.getParameter("forward")); 388 else 389 session.removeAttribute("forward"); 390 391 try { 392 response.setContentType("text/html"); 393 java.io.PrintWriter out = response.getWriter(); 394 395 String actionURL = getActionURL(request, response); 397 398 if (request.getParameter("transform")!=null){ 400 this.ggf = new GGF(this.contextRoot, actionURL,request.getParameter("transform"), userLanguage); 401 } 402 else this.ggf = new GGF(this.contextRoot, actionURL, userLanguage); 404 405 406 writingUI(out, request, response, actionURL, session); 408 409 session.setAttribute("formgenerator.GGF", this.ggf); 411 412 session.setAttribute("formgenerator.isActivity", this.isActivity); 414 415 session.setAttribute("formgenerator.projectName", this.projectName); 417 418 session.setAttribute("formgenerator.activityName", this.activityName); 420 421 session.setAttribute("formgenerator.mode", this.mode); 423 424 session.setAttribute("formgenerator.sAdapter", this.servletAdapter); 426 427 out.close(); 428 429 } catch (Exception e) { 430 e.printStackTrace(); 432 } 433 } 434 435 443 protected void updateContext(HttpServletRequest request, HttpSession session,Document formDocument) { 444 this.servletAdapter.setContextProperty(ServletAdapter.USERAGENT, request.getHeader("User-Agent")); 445 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SERVLET_REQUEST, request); 446 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SESSION_OBJECT, session); 447 this.servletAdapter.setContextProperty("instance",formDocument); 448 } 449 450 451 459 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 460 { 461 HttpSession session = request.getSession(true); 463 this.servletAdapter = null; 464 465 try { 466 System.out.println("===> ServletAdapter"); 468 this.servletAdapter = (ServletAdapter)session.getAttribute("formgenerator.sAdapter"); 469 470 System.out.println("===> GGF"); 472 this.ggf = (GGF)session.getAttribute("formgenerator.GGF"); 473 session.setAttribute("formgenerator.GGF", this.ggf); 475 476 this.isActivity = (Boolean )session.getAttribute("formgenerator.isActivity"); 478 session.setAttribute("formgenerator.isActivity", this.isActivity); 480 481 this.projectName = (String )session.getAttribute("formgenerator.projectName"); 483 System.out.println("===> projectName : "+projectName); 484 session.setAttribute("formgenerator.projectName", this.projectName); 486 487 this.activityName = (String )session.getAttribute("formgenerator.activityName"); 489 session.setAttribute("formgenerator.activityName", this.activityName); 491 492 this.mode = (String )session.getAttribute("formgenerator.mode"); 494 495 496 if ((this.servletAdapter == null)||(this.ggf == null)||(this.isActivity == null)||(this.projectName == null)){ 497 throw new IOException ("Invalid session - Cannot retrieve servletAdapter or ggf or isActivity or projectName in the session"); 499 } 500 501 updateContextPost(request, session); 502 503 this.servletAdapter.executeHandler(); 504 System.out.println("===> 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 System.out.println("===> filledDocument : " + GGF.toString(filledDocument)); 513 514 515 550 551 response.setContentType("text/html"); 553 554 this.callExecutor(filledDocument); 556 557 559 String forward = (String )session.getAttribute("forward"); 561 if ((forward!=null)&&!forward.equals("")) 562 response.sendRedirect(forward); 563 564 else 566 if(this.isActivity.booleanValue() == true){ 567 568 if(this.mode.equals("consumer")) 569 response.sendRedirect("/formgenerator/ServletActivityToDoList?projectName=" + projectName); 571 else 572 response.sendRedirect("/formgenerator/ServletWorkList"); 574 } 575 else{ 576 if(this.mode.equals("consumer")) 577 response.sendRedirect("/formgenerator/ServletInstancesProjects"); 579 else 580 response.sendRedirect("/formgenerator/ServletWorkList"); 582 } 583 584 this.servletAdapter.buildUI(response.getWriter()); 586 response.getWriter().close(); 587 } catch (Exception e) { 588 e.printStackTrace(); 590 } 591 } 592 593 601 protected void updateContextPost(HttpServletRequest request, HttpSession session) { 602 this.servletAdapter.setContextProperty(ServletAdapter.USERAGENT, request.getHeader("User-Agent")); 603 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SERVLET_REQUEST, request); 604 this.servletAdapter.setContextProperty(ServletAdapter.HTTP_SESSION_OBJECT, session); 605 } 606 607 617 private ServletAdapter setupServletAdapter(String actionURL, 618 HttpSession session, 619 String formPath, 620 String xslFile, 621 String cssFile) { 622 ServletAdapter aAdapter = new ServletAdapter(); 624 aAdapter.setContextRoot(contextRoot); 625 if ((configPath != null) && !(configPath.equals(""))) { 626 aAdapter.setConfigPath(configPath); 627 } 628 aAdapter.setFormPath(formPath); 629 aAdapter.setStylesheetPath(stylesPath); 630 aAdapter.setActionUrl(actionURL); 631 aAdapter.setUploadDir(uploadDir); 632 633 if (xslFile != null) { 634 aAdapter.setStylesheet(xslFile); 635 if (cat.isDebugEnabled()) { 636 cat.debug("using xsl stylesheet: " + xslFile); 637 } 638 } 639 if (cssFile != null) { 640 aAdapter.setCSS(cssFile); 641 if (cat.isDebugEnabled()) { 642 cat.debug("using css stylesheet: " + cssFile); 643 } 644 } 645 646 Map servletMap = new HashMap (); 647 servletMap.put(ChibaAdapter.SESSION_ID, session.getId()); 648 aAdapter.setContextProperty(ChibaAdapter.SUBMISSION_RESPONSE, servletMap); 649 650 return aAdapter; 651 } 652 653 private void storeContextParams(HttpServletRequest request) { 654 Enumeration params = request.getParameterNames(); 655 String s; 656 while (params.hasMoreElements()) { 657 s = (String ) params.nextElement(); 658 if (!(s.equals(FORM_PARAM_NAME) || s.equals(XSL_PARAM_NAME) || s.equals(CSS_PARAM_NAME) || s.equals(ACTIONURL_PARAM_NAME))) { 660 String value = request.getParameter(s); 661 this.servletAdapter.setContextProperty(s, value); 662 if (cat.isDebugEnabled()) { 663 cat.debug("added request param '" + s + "' added to context"); 664 } 665 } 666 } 667 } 668 669 private String getActionURL(HttpServletRequest request, HttpServletResponse response) { 670 String defaultActionURL = 671 request.getScheme() 672 + "://" 673 + request.getServerName() 674 + ":" 675 + request.getServerPort() 676 + request.getContextPath() 677 + request.getServletPath(); 678 String encodedDefaultActionURL = response.encodeURL(defaultActionURL); 679 int sessIdx = encodedDefaultActionURL.indexOf(";jsession"); 680 String sessionId = null; 681 if (sessIdx > -1) { 682 sessionId = encodedDefaultActionURL.substring(sessIdx); 683 } 684 String actionURL = request.getParameter(ACTIONURL_PARAM_NAME); 685 if (null == actionURL) { 686 actionURL = encodedDefaultActionURL; 687 } else if (null != sessionId) { 688 actionURL += sessionId; 689 } 690 691 cat.info("actionURL: " + actionURL); 692 actionURL = response.encodeURL(actionURL); 694 return actionURL; 695 } 696 740 } | Popular Tags |