1 64 package com.jcorporate.expresso.core.controller; 65 66 import com.jcorporate.expresso.core.controller.session.HTTPPersistentSession; 67 import com.jcorporate.expresso.core.i18n.Messages; 68 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 69 import com.jcorporate.expresso.core.misc.CurrentLogin; 70 import com.jcorporate.expresso.core.misc.StringUtil; 71 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 72 import com.jcorporate.expresso.core.misc.upload.FileItem; 73 import com.jcorporate.expresso.core.security.User; 74 import org.apache.log4j.Logger; 75 import org.apache.struts.action.ActionForm; 76 import org.apache.struts.action.ActionMapping; 77 import org.apache.struts.upload.MultipartRequestHandler; 78 79 import javax.servlet.Servlet ; 80 import javax.servlet.ServletException ; 81 import javax.servlet.ServletRequest ; 82 import javax.servlet.ServletResponse ; 83 import javax.servlet.http.HttpServletRequest ; 84 import javax.servlet.http.HttpServletResponse ; 85 import javax.servlet.http.HttpSession ; 86 import java.util.Enumeration ; 87 import java.util.Hashtable ; 88 import java.util.Locale ; 89 import java.util.StringTokenizer ; 90 import java.util.Vector ; 91 92 93 99 public class ServletControllerRequest 100 extends ControllerRequest 101 implements Cloneable { 102 private static Logger log = Logger.getLogger(ServletControllerRequest.class); 103 104 112 private ServletResponse myServletResponse = null; 113 private ServletRequest myServletRequest = null; 114 private Servlet myCallingServlet = null; 115 private ActionMapping myMapping = null; 116 private ActionForm myForm = null; 117 118 119 122 public ServletControllerRequest() { 123 124 } 125 126 132 public synchronized void setServletResponse(ServletResponse newResponse) { 133 myServletResponse = newResponse; 134 } 135 136 142 public synchronized void setServletRequest(ServletRequest newRequest) { 143 myServletRequest = newRequest; 144 } 145 146 152 public void setCallingServlet(Servlet newServlet) { 153 myCallingServlet = newServlet; 154 } 155 156 162 public Servlet getCallingServlet() { 163 return myCallingServlet; 164 } 165 166 173 public synchronized void setMapping(ActionMapping newMapping) { 174 this.myMapping = newMapping; 175 } 176 177 206 public synchronized ActionMapping getMapping() { 207 return myMapping; 208 } 209 210 218 public synchronized void setForm(ActionForm newForm) { 219 this.myForm = newForm; 220 } 221 222 251 public synchronized ActionForm getForm() { 252 return myForm; 253 } 254 255 271 public ServletRequest getServletRequest() { 272 return myServletRequest; 273 } 274 275 291 public ServletResponse getServletResponse() { 292 return myServletResponse; 293 } 294 295 312 public HttpServletRequest getHttpServletRequest() { 313 return (HttpServletRequest ) myServletRequest; 314 } 315 316 317 334 public HttpServletResponse getHttpServletResponse() { 335 return (HttpServletResponse ) myServletResponse; 336 } 337 338 350 private static Hashtable parseParamsReq(HttpServletRequest req, 351 Controller controller) 352 throws ControllerException { 353 if (controller == null) { 354 throw new IllegalArgumentException ("ServletControllerRequest." + 355 "parseParamsReq(): Parameter 'controller' must not be null"); 356 } 357 358 String controllerName = controller.getClass().getName(); 359 Hashtable params = new Hashtable (); 360 params.put(Controller.CONTROLLER_PARAM_KEY, controllerName); 361 362 if (req == null) { 363 throw new IllegalArgumentException ("Request may not be null here"); 364 } 365 366 String oneName = null; 367 String oneValue = null; 368 log.debug("Regular query string Parameters:"); 369 370 for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) { 371 oneName = (String ) e.nextElement(); 372 oneValue = StringUtil.notNull(req.getParameter(oneName)); 373 params.put(oneName, oneValue); 374 375 if (log.isDebugEnabled()) { 376 log.debug("Parameter '" + oneName + "', value '" + oneValue + 377 "'"); 378 } 379 } 380 381 382 log.debug("End regular query string parameters"); 383 384 Hashtable newParams = addButtonParams(params); 385 386 return newParams; 387 } 389 390 391 397 private static Hashtable addButtonParams(Hashtable params) 398 throws ControllerException { 399 log.debug("Adding button parameters"); 400 401 String oneParamName = null; 402 Object oneParamValue = null; 403 boolean gotControllerFromButton = false; 404 boolean gotStateFromButton = false; 405 Hashtable newParams = new Hashtable (); 406 407 for (Enumeration e = params.keys(); e.hasMoreElements();) { 408 oneParamName = (String ) e.nextElement(); 409 oneParamValue = params.get(oneParamName); 410 411 boolean addThisParam = true; 413 414 if (oneParamName.equals(Controller.CONTROLLER_PARAM_KEY) && gotControllerFromButton) { 416 417 addThisParam = false; 420 421 } else if (oneParamName.equals(Controller.STATE_PARAM_KEY) && gotStateFromButton) { 422 423 addThisParam = false; 426 } else if (oneParamName.endsWith("_params")) { 427 428 String buttonName = oneParamName.substring(0, 430 oneParamName.indexOf("_params")); 431 432 if (!"u".equals(params.get(buttonName + "_encoding"))) { 434 435 newParams.put(oneParamName, 437 URLUTF8Encoder.encode((String ) oneParamValue)); 438 439 newParams.put(buttonName + "_encoding", "u"); 441 442 addThisParam = false; 444 } 445 } 446 if (addThisParam) { 448 newParams.put(oneParamName, oneParamValue); 449 } 450 if (oneParamName.startsWith("button_")) { 451 452 String buttonName = oneParamName.substring(oneParamName.indexOf("_") + 1); 455 456 464 if (buttonName.endsWith(".x")) { 465 buttonName = buttonName.substring(0, buttonName.length() - 2); 466 } 467 if (buttonName.endsWith(".y")) { 468 buttonName = buttonName.substring(0, buttonName.length() - 2); 469 } 470 471 if (log.isDebugEnabled()) { 472 log.debug("There is a button parameter called '" + 473 buttonName + "'"); 474 } 475 476 newParams.put("button", buttonName); 477 478 479 String paramString = (String ) params.get(buttonName + 480 "_params"); 481 482 if (paramString != null) { 483 log.debug("Button parameters:"); 484 485 String encodeType = (String ) params.get(buttonName + 488 "_encoding"); 489 490 if ("u".equals(encodeType)) { 491 492 paramString = URLUTF8Encoder.decode(paramString); 494 } 495 496 497 498 StringTokenizer stkpm = null; 499 stkpm = new StringTokenizer (paramString, "&"); 500 501 while (stkpm.hasMoreTokens()) { 502 String pairValue = stkpm.nextToken(); 503 504 pairValue = URLUTF8Encoder.decode(pairValue); 507 508 String paramName = pairValue; 509 String paramValue = ""; 510 int position = pairValue.indexOf("="); 511 if (position != -1) { 512 paramName = pairValue.substring(0, position); 513 position++; if (position < pairValue.length()) { 515 paramValue = pairValue.substring(position); 516 } 517 } 518 519 newParams.put(paramName, paramValue); 520 521 if (paramName.equals(Controller.CONTROLLER_PARAM_KEY)) { 522 gotControllerFromButton = true; 523 } else if (paramName.equals(Controller.STATE_PARAM_KEY)) { 524 gotStateFromButton = true; 525 } 526 527 if (log.isDebugEnabled()) { 528 log.debug("Parameter '" + paramName + "', value '" 529 + paramValue + "'"); 530 } 531 } 532 533 534 log.debug("End button parameters"); 535 } else { 536 throw new ControllerException("Button '" + buttonName + 537 "' was clicked, but no " + 538 "button parameters field called '" + 539 buttonName + 540 "_params' was found."); 541 } 542 } 543 544 } 545 546 547 return newParams; 548 } 549 550 551 571 public static ServletControllerRequest parseParamsMultiPart(MultipartRequestHandler mp, 572 ActionMapping mapping, 573 ActionForm form, 574 HttpServletRequest request, 575 HttpServletResponse response, 576 Controller controller) 577 throws ServletException , ControllerException { 578 String controllerName = controller.getClass().getName(); 579 580 ServletControllerRequest cparams = new ServletControllerRequest(); 582 cparams.setMapping(mapping); 583 cparams.setForm(form); 584 cparams.setParameters(null); 585 586 Hashtable params = new Hashtable (); 587 params.put(Controller.CONTROLLER_PARAM_KEY, controllerName); 588 589 Hashtable textParams = mp.getTextElements(); 590 Hashtable fileParams = mp.getFileElements(); 591 String oneKey = null; 592 593 for (Enumeration te = textParams.keys(); te.hasMoreElements();) { 594 oneKey = (String ) te.nextElement(); 595 params.put(oneKey, textParams.get(oneKey)); 596 log.debug("Got '" + oneKey + "', '" + 597 (String ) textParams.get(oneKey) + "'"); 598 } 599 for (Enumeration fe = fileParams.keys(); fe.hasMoreElements();) { 600 oneKey = (String ) fe.nextElement(); 601 602 FileItem oneFileItem = (FileItem) fileParams.get(oneKey); 603 Vector oneFile = new Vector (); 604 oneFile.addElement(oneFileItem.getFileName()); 605 oneFile.addElement(oneFileItem.getStoreLocation().getPath()); 606 params.put(oneKey, oneFile); 607 log.debug("Got file '" + oneKey + "', file '" + 608 oneFileItem.getFileName() + "'"); 609 } 610 611 HttpSession session = request.getSession(false); 612 String userName = null; 613 int uid = 0; 614 615 if (session != null) { 616 Object o = GenericSession.getAttribute(request, "CurrentLogin"); 617 618 if (o != null) { 619 CurrentLogin cl = (CurrentLogin) o; 620 uid = cl.getUid(); 621 userName = cl.getUserName(); 622 623 if (log.isDebugEnabled()) { 624 log.debug("Login established as user '" + userName + 625 "', uid " + uid); 626 } 627 } else { 628 log.debug("No CurrentLogin object in session - assuming 'none'"); 629 } 630 } else { 631 userName = User.UNKNOWN_USER; 632 } 633 634 cparams.setUser(userName); 635 cparams.setUid(uid); 636 637 638 cparams.setSession(new HTTPPersistentSession(request, response)); 639 cparams.setServletRequest(request); 640 cparams.setServletResponse(response); 641 cparams.setDataContext(getDBName(request)); 642 643 Hashtable finalParams = addButtonParams(params); 647 Object oneParamValue = null; 648 String oneParamName = null; 649 650 for (Enumeration ep = finalParams.keys(); ep.hasMoreElements();) { 651 oneParamName = (String ) ep.nextElement(); 652 oneParamValue = finalParams.get(oneParamName); 653 654 if (oneParamValue instanceof String ) { 655 cparams.setParameter(oneParamName, (String ) oneParamValue); 656 } else if (oneParamValue instanceof Vector ) { 657 Vector v = (Vector ) oneParamValue; 658 Enumeration ee = v.elements(); 659 String clientFileName = (String ) ee.nextElement(); 660 String serverFileName = (String ) ee.nextElement(); 661 cparams.setFileParameter(oneParamName, clientFileName, 662 serverFileName); 663 } 664 } 665 666 667 return cparams; 668 } 669 670 671 683 public static ServletControllerRequest parseParams(ActionMapping mapping, 684 ActionForm form, 685 HttpServletRequest request, 686 HttpServletResponse response, 687 Controller controller) 688 throws ServletException , ControllerException { 689 Hashtable params = parseParamsReq(request, controller); 691 HttpSession session = request.getSession(false); 692 ServletControllerRequest cparams = new ServletControllerRequest(); 693 cparams.setMapping(mapping); 694 cparams.setForm(form); 695 String userName = null; 696 int uid = 0; 697 698 if (session != null) { 699 Object o = GenericSession.getAttribute(request, "CurrentLogin"); 700 701 if (o != null) { 702 CurrentLogin cl = (CurrentLogin) o; 703 uid = cl.getUid(); 704 userName = cl.getUserName(); 705 706 if (log.isDebugEnabled()) { 707 log.debug("Login established as user '" + userName + 708 "', uid " + uid); 709 } 710 } else { 711 log.debug("No CurrentLogin object in session - assuming 'none'"); 712 } 713 } else { 714 userName = User.UNKNOWN_USER; 715 log.debug("No session available - user is none"); 716 } 717 718 cparams.setUser(userName); 719 cparams.setUid(uid); 720 721 Object oneParamValue = null; 722 String oneParamName = null; 723 724 cparams.setParameters(null); 727 boolean newRouting = (request.getAttribute("usedRouting") == null); 728 729 for (Enumeration ep = params.keys(); ep.hasMoreElements();) { 730 oneParamName = (String ) ep.nextElement(); 731 oneParamValue = params.get(oneParamName); 732 733 734 737 boolean routingParam = (oneParamName.equals(Controller.CONTROLLER_PARAM_KEY) || oneParamName.equals( 738 Controller.STATE_PARAM_KEY)); 739 if (newRouting || (!newRouting && !routingParam)) { 740 if (oneParamValue instanceof String ) { 741 cparams.setParameter(oneParamName, (String ) oneParamValue); 742 } else if (oneParamValue instanceof Vector ) { 743 Vector v = (Vector ) oneParamValue; 744 Enumeration ee = v.elements(); 745 String clientFileName = (String ) ee.nextElement(); 746 String serverFileName = (String ) ee.nextElement(); 747 cparams.setFileParameter(oneParamName, clientFileName, 748 serverFileName); 749 } 750 } 751 } 752 753 754 755 cparams.setSession(new HTTPPersistentSession(request, response)); 756 cparams.setServletRequest(request); 757 cparams.setServletResponse(response); 758 759 cparams.setDataContext(getDBName(request)); 760 761 return cparams; 762 } 763 764 779 private static String getDBName(HttpServletRequest request) { 780 try { 781 CurrentLogin cl = (CurrentLogin) GenericSession.getAttribute(request, CurrentLogin.LOGIN_KEY); 782 783 if (cl == null) { 784 return "default"; 785 } 786 787 return cl.getDBName(); 788 } catch (ServletException se) { 789 log.error(se); 790 791 return "default"; 792 } 793 } 794 795 796 public Locale getLocale() { 797 Locale l = null; 798 799 try { 800 if (myServletRequest != null) { 801 Object o = GenericSession.getAttribute((HttpServletRequest ) myServletRequest, 802 Messages.LOCALE_KEY); 803 804 if (o != null) { 805 l = (Locale ) o; 806 } 807 } 808 } catch (ServletException se) { 809 log.error(se); 810 } 811 if (l == null) { 812 l = Locale.getDefault(); 813 } 814 815 return l; 816 } 817 818 825 public Object clone() { 826 ServletControllerRequest scr = (ServletControllerRequest) super.clone(); 827 scr.myCallingServlet = this.myCallingServlet; 828 scr.myServletRequest = this.myServletRequest; 829 scr.myServletResponse = this.myServletResponse; 830 scr.myMapping = this.myMapping; 831 832 return scr; 833 } 834 835 843 public String [] getParamValues(String paramName) { 844 HttpServletRequest hreq = (HttpServletRequest ) getServletRequest(); 845 return hreq.getParameterValues(paramName); 846 } 847 848 } 849 | Popular Tags |