1 package org.apache.turbine.util; 2 3 18 19 import java.net.URLEncoder ; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import org.apache.commons.lang.StringUtils; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import org.apache.ecs.html.A; 34 35 import org.apache.turbine.Turbine; 36 import org.apache.turbine.TurbineConstants; 37 import org.apache.turbine.util.parser.ParameterParser; 38 import org.apache.turbine.util.parser.ParserUtils; 39 import org.apache.turbine.util.uri.URI; 40 import org.apache.turbine.util.uri.URIConstants; 41 42 69 public class DynamicURI 70 implements URI 71 { 72 73 public static final String HTTP = URIConstants.HTTP; 74 75 76 public static final String HTTPS = URIConstants.HTTPS; 77 78 79 private static Log log = LogFactory.getLog(DynamicURI.class); 80 81 82 protected ServerData sd = null; 83 84 85 protected RunData data = null; 86 87 88 protected String reference = null; 89 90 93 94 public HttpServletResponse res = null; 95 96 97 protected List pathInfo = null; 98 99 100 protected List queryData = null; 101 102 103 protected boolean hasPathInfo = false; 104 105 106 protected boolean hasQueryData = false; 107 108 109 protected boolean redirect = false; 110 111 112 protected static final int PATH_INFO = 0; 113 114 115 protected static final int QUERY_DATA = 1; 116 117 118 private boolean initialized = false; 119 120 125 public DynamicURI(RunData data) 126 { 127 init(data); 128 } 129 130 134 public DynamicURI() 135 { 136 } 137 138 144 public DynamicURI(RunData data, String screen) 145 { 146 this(data); 147 setScreen(screen); 148 } 149 150 157 public DynamicURI(RunData data, String screen, String action) 158 { 159 this(data, screen); 160 setAction(action); 161 } 162 163 171 public DynamicURI(RunData data,String screen, 172 String action,boolean redirect) 173 { 174 this(data, screen, action); 175 this.redirect = redirect; 176 } 177 178 185 public DynamicURI(RunData data, String screen, boolean redirect) 186 { 187 this(data, screen); 188 this.redirect = redirect; 189 } 190 191 197 public DynamicURI(RunData data, boolean redirect) 198 { 199 this(data); 200 this.redirect = redirect; 201 } 202 203 208 public DynamicURI(ServerData sd) 209 { 210 init(sd); 211 } 212 213 219 public DynamicURI(ServerData sd, String screen) 220 { 221 this(sd); 222 setScreen(screen); 223 } 224 225 232 public DynamicURI(ServerData sd, String screen, String action) 233 { 234 this(sd, screen); 235 setAction(action); 236 } 237 238 246 public DynamicURI(ServerData sd, String screen, 247 String action, boolean redirect) 248 { 249 this(sd, screen, action); 250 this.redirect = redirect; 251 } 252 253 260 public DynamicURI(ServerData serverData, String screen, boolean redirect) 261 { 262 this(serverData, screen); 263 this.redirect = redirect; 264 } 265 266 272 public DynamicURI(ServerData serverData, boolean redirect) 273 { 274 this(serverData); 275 this.redirect = redirect; 276 this.initialized = true; 277 } 278 279 284 public void init(RunData data) 285 { 286 init(data.getServerData()); 287 this.data = data; 288 this.res = data.getResponse(); 289 } 290 291 296 public void init(ServerData serverData) 297 { 298 this.sd = (ServerData) serverData.clone(); 299 this.pathInfo = new ArrayList (); 300 this.queryData = new ArrayList (); 301 this.reference = null; 302 this.initialized = true; 303 } 304 305 316 protected void add(int type, String name, String value) 317 { 318 assertInitialized(); 319 Object [] tmp = new Object [2]; 320 tmp[0] = ParserUtils.convertAndTrim(name); 321 tmp[1] = value; 322 switch (type) 323 { 324 case PATH_INFO: 325 this.pathInfo.add(tmp); 326 this.hasPathInfo = true; 327 break; 328 case QUERY_DATA: 329 this.queryData.add(tmp); 330 this.hasQueryData = true; 331 break; 332 } 333 } 334 335 348 protected void add(int type, ParameterParser pp) 349 { 350 for( Iterator iter = pp.keySet().iterator(); iter.hasNext(); ) 351 { 352 String key = (String ) iter.next(); 353 if (!key.equalsIgnoreCase(URIConstants.CGI_ACTION_PARAM) && 354 !key.equalsIgnoreCase(URIConstants.CGI_SCREEN_PARAM) && 355 !key.equalsIgnoreCase(URIConstants.CGI_TEMPLATE_PARAM)) 356 { 357 String [] values = pp.getStrings(key); 358 for (int i = 0; i < values.length; i++) 359 { 360 add(type, key, values[i]); 361 } 362 } 363 } 364 } 365 366 373 public DynamicURI addPathInfo(String name, Object value) 374 { 375 add(PATH_INFO, name, value.toString()); 376 return this; 377 } 378 379 386 public DynamicURI addPathInfo(String name, String value) 387 { 388 add(PATH_INFO, name, value); 389 return this; 390 } 391 392 399 public DynamicURI addPathInfo(String name, double value) 400 { 401 add(PATH_INFO, name, Double.toString(value)); 402 return this; 403 } 404 405 412 public DynamicURI addPathInfo(String name, int value) 413 { 414 add(PATH_INFO, name, new Integer (value).toString()); 415 return this; 416 } 417 418 425 public DynamicURI addPathInfo(String name, long value) 426 { 427 add(PATH_INFO, name, new Long (value).toString()); 428 return this; 429 } 430 431 438 public DynamicURI addPathInfo(ParameterParser pp) 439 { 440 add(PATH_INFO, pp); 441 return this; 442 } 443 444 451 public DynamicURI addQueryData(String name, Object value) 452 { 453 add(QUERY_DATA, name, value.toString()); 454 return this; 455 } 456 457 464 public DynamicURI addQueryData(String name, String value) 465 { 466 add(QUERY_DATA, name, value); 467 return this; 468 } 469 470 477 public DynamicURI addQueryData(String name, double value) 478 { 479 add(QUERY_DATA, name, Double.toString(value)); 480 return this; 481 } 482 483 490 public DynamicURI addQueryData(String name, int value) 491 { 492 add(QUERY_DATA, name, new Integer (value).toString()); 493 return this; 494 } 495 496 503 public DynamicURI addQueryData(String name, long value) 504 { 505 add(QUERY_DATA, name, new Long (value).toString()); 506 return this; 507 } 508 509 516 public DynamicURI addQueryData(ParameterParser pp) 517 { 518 add(QUERY_DATA, pp); 519 return this; 520 } 521 522 538 public String getA(String name) 539 { 540 return new A(this.toString(), name).toString(); 541 } 542 543 548 public String getScriptName() 549 { 550 String result = getServerData().getScriptName(); 551 return (StringUtils.isEmpty(result) ? "" : result); 552 } 553 554 559 public String getContextPath() 560 { 561 String result = getServerData().getContextPath(); 562 return (StringUtils.isEmpty(result) ? "" : result); 563 } 564 565 570 public String getReference() 571 { 572 assertInitialized(); 573 return (StringUtils.isEmpty(this.reference) ? "" : this.reference); 574 } 575 576 581 public String getServerName() 582 { 583 String result = getServerData().getServerName(); 584 return (StringUtils.isEmpty(result) ? "" : result); 585 } 586 587 592 public int getServerPort() 593 { 594 int result = getServerData().getServerPort(); 595 return (result==0 ? URIConstants.HTTP_PORT : result); 596 } 597 598 603 public String getServerScheme() 604 { 605 String result = getServerData().getServerScheme(); 606 return (StringUtils.isEmpty(result) ? "" : result); 607 } 608 609 619 protected void remove(int type, String name) 620 { 621 assertInitialized(); 622 try 623 { 624 switch (type) 625 { 626 case PATH_INFO: 627 for (Iterator iter = this.pathInfo.iterator(); 628 iter.hasNext();) 629 { 630 Object [] tmp = (Object []) iter.next(); 631 if (ParserUtils.convertAndTrim(name) 632 .equals((String ) tmp[0])) 633 { 634 iter.remove(); 635 } 636 } 637 if (hasPathInfo && this.pathInfo.size() == 0) 638 { 639 this.hasPathInfo = false; 640 } 641 break; 642 case QUERY_DATA: 643 for (Iterator iter = this.pathInfo.iterator(); 644 iter.hasNext();) 645 { 646 Object [] tmp = (Object []) iter.next(); 647 if (ParserUtils.convertAndTrim(name) 648 .equals((String ) tmp[0])) 649 { 650 iter.remove(); 651 } 652 } 653 if (hasQueryData && this.queryData.size() == 0) 654 { 655 this.hasQueryData = false; 656 } 657 break; 658 } 659 } 660 catch (Exception e) 661 { 662 log.error("Could not remove "+name, e); 663 } 664 } 665 666 669 public void removePathInfo() 670 { 671 assertInitialized(); 672 this.pathInfo.clear(); 673 this.hasPathInfo = false; 674 } 675 676 681 public void removePathInfo(String name) 682 { 683 remove(PATH_INFO, name); 684 } 685 686 689 public void removeQueryData() 690 { 691 assertInitialized(); 692 this.queryData.clear(); 693 this.hasQueryData = false; 694 } 695 696 701 public void removeQueryData(String name) 702 { 703 remove(QUERY_DATA, name); 704 } 705 706 713 protected String renderPathInfo(List data) 714 { 715 String key = null; 716 String value = null; 717 String tmp = null; 718 StringBuffer out = new StringBuffer (); 719 for( Iterator iter = data.iterator(); iter.hasNext(); ) 720 { 721 Object [] stuff = (Object []) iter.next(); 722 key = URLEncoder.encode((String ) stuff[0]); 723 tmp = (String ) stuff[1]; 724 if (tmp == null || tmp.length() == 0) 725 { 726 value = "null"; 727 } 728 else 729 { 730 value = URLEncoder.encode(tmp); 731 } 732 733 if (out.length() > 0) 734 { 735 out.append("/"); 736 } 737 out.append(key); 738 out.append("/"); 739 out.append(value); 740 } 741 return out.toString(); 742 } 743 744 751 protected String renderQueryString(List data) 752 { 753 String key = null; 754 String value = null; 755 String tmp = null; 756 StringBuffer out = new StringBuffer (); 757 for( Iterator iter = data.iterator(); iter.hasNext(); ) 758 { 759 Object [] stuff = (Object []) iter.next(); 760 key = URLEncoder.encode((String ) stuff[0]); 761 tmp = (String ) stuff[1]; 762 if (tmp == null || tmp.length() == 0) 763 { 764 value = "null"; 765 } 766 else 767 { 768 value = URLEncoder.encode(tmp); 769 } 770 771 if (out.length() > 0) 772 { 773 out.append("&"); 774 } 775 out.append(key); 776 out.append("="); 777 out.append(value); 778 } 779 return out.toString(); 780 } 781 782 791 public DynamicURI setAction(String action) 792 { 793 add(PATH_INFO, URIConstants.CGI_ACTION_PARAM, action); 794 return this; 795 } 796 797 806 public DynamicURI setActionEvent(String actionName, String eventName) 807 { 808 setAction(actionName).addPathInfo( 809 "eventSubmit_" + eventName, eventName); 810 return this; 811 } 812 813 822 public DynamicURI setScreen(String screen) 823 { 824 add(PATH_INFO, URIConstants.CGI_SCREEN_PARAM, screen); 825 return this; 826 } 827 828 834 public DynamicURI setScriptName(String name) 835 { 836 getServerData().setScriptName(name); 837 return this; 838 } 839 840 846 public DynamicURI setContextPath(String contextPath) 847 { 848 getServerData().setContextPath(contextPath); 849 return this; 850 } 851 852 858 public DynamicURI setReference(String reference) 859 { 860 this.reference = reference; 861 return this; 862 } 863 864 870 public DynamicURI setServerName(String name) 871 { 872 getServerData().setServerName(name); 873 return this; 874 } 875 876 882 public DynamicURI setServerPort(int port) 883 { 884 getServerData().setServerPort(port); 885 return this; 886 } 887 888 895 public DynamicURI setSecure() 896 { 897 return setSecure(443); 898 } 899 900 907 public DynamicURI setSecure(int port) 908 { 909 boolean useSSL = 910 Turbine.getConfiguration() 911 .getBoolean(TurbineConstants.USE_SSL_KEY, 912 TurbineConstants.USE_SSL_DEFAULT); 913 914 setServerScheme(useSSL ? URIConstants.HTTPS : URIConstants.HTTP); 915 setServerPort(port); 916 917 return this; 918 } 919 920 926 public DynamicURI setServerScheme(String scheme) 927 { 928 getServerData().setServerScheme(scheme); 929 return this; 930 } 931 932 950 public String toString() 951 { 952 assertInitialized(); 953 StringBuffer output = new StringBuffer (); 954 output.append(getServerScheme()); 955 output.append(URIConstants.URI_SCHEME_SEPARATOR); 956 output.append(getServerName()); 957 if ((getServerScheme().equals(URIConstants.HTTP) 958 && getServerPort() != URIConstants.HTTP_PORT) 959 || (getServerScheme().equals(URIConstants.HTTPS) 960 && getServerPort() != URIConstants.HTTPS_PORT) 961 ) 962 { 963 output.append(":"); 964 output.append(getServerPort()); 965 } 966 output.append(getContextPath()); 967 output.append(getScriptName()); 968 if (this.hasPathInfo) 969 { 970 output.append("/"); 971 output.append(renderPathInfo(this.pathInfo)); 972 } 973 if (this.hasQueryData) 974 { 975 output.append("?"); 976 output.append(renderQueryString(this.queryData)); 977 } 978 if (this.reference != null) 979 { 980 output.append("#"); 981 output.append(this.getReference()); 982 } 983 984 if (this.res != null) 988 { 989 if (this.redirect) 990 return res.encodeRedirectURL(output.toString()); 991 else 992 return res.encodeURL(output.toString()); 993 } 994 else 995 { 996 return output.toString(); 997 } 998 } 999 1000 1010 public static String toString(RunData data) 1011 { 1012 StringBuffer output = new StringBuffer (); 1013 HttpServletRequest request = data.getRequest(); 1014 1015 output.append(data.getServerScheme()); 1016 output.append(URIConstants.URI_SCHEME_SEPARATOR); 1017 output.append(data.getServerName()); 1018 1019 if ((data.getServerScheme().equals(URIConstants.HTTP) && 1020 data.getServerPort() != URIConstants.HTTP_PORT) || 1021 (data.getServerScheme().equals(URIConstants.HTTPS) && 1022 data.getServerPort() != URIConstants.HTTPS_PORT)) 1023 { 1024 output.append(":"); 1025 output.append(data.getServerPort()); 1026 } 1027 1028 output.append(data.getServerData().getContextPath()); 1029 1030 output.append(data.getServerData().getScriptName()); 1031 1032 if (request.getPathInfo() != null) 1033 { 1034 output.append(request.getPathInfo()); 1035 } 1036 1037 if (request.getQueryString() != null) 1038 { 1039 output.append("?"); 1040 output.append(request.getQueryString()); 1041 } 1042 return output.toString(); 1043 } 1044 1045 1050 public ServerData getServerData() 1051 { 1052 assertInitialized(); 1053 return this.sd; 1054 } 1055 1056 1063 public void setServerData(ServerData serverData) 1064 { 1065 this.sd = serverData; 1066 } 1067 1068 1071 protected void assertInitialized() 1072 { 1073 if (!this.initialized) 1074 { 1075 throw new IllegalStateException ("Not initialized"); 1076 } 1077 } 1078} 1079 | Popular Tags |