1 package org.apache.turbine.util.uri; 2 3 18 19 import java.net.URLEncoder ; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.commons.lang.StringUtils; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.apache.turbine.util.RunData; 32 import org.apache.turbine.util.ServerData; 33 import org.apache.turbine.util.parser.ParameterParser; 34 import org.apache.turbine.util.parser.ParserUtils; 35 36 48 49 public class TurbineURI 50 extends BaseURI 51 { 52 53 private static Log log = LogFactory.getLog(TurbineURI.class); 54 55 56 private List [] dataVectors = null; 57 58 66 67 70 public TurbineURI() 71 { 72 super(); 73 init(); 74 } 75 76 81 public TurbineURI(RunData runData) 82 { 83 super(runData); 84 init(); 85 } 86 87 93 public TurbineURI(RunData runData, boolean redirect) 94 { 95 super(runData, redirect); 96 init(); 97 } 98 99 105 public TurbineURI(RunData runData, String screen) 106 { 107 this(runData); 108 setScreen(screen); 109 } 110 111 118 public TurbineURI(RunData runData, String screen, boolean redirect) 119 { 120 this(runData, redirect); 121 setScreen(screen); 122 } 123 124 131 public TurbineURI(RunData runData, String screen, String action) 132 { 133 this(runData, screen); 134 setAction(action); 135 } 136 137 145 public TurbineURI(RunData runData, String screen, String action, boolean redirect) 146 { 147 this(runData, screen, redirect); 148 setAction(action); 149 } 150 151 156 public TurbineURI(ServerData serverData) 157 { 158 super(serverData); 159 init(); 160 } 161 162 168 public TurbineURI(ServerData serverData, boolean redirect) 169 { 170 super(serverData, redirect); 171 init(); 172 } 173 174 180 public TurbineURI(ServerData serverData, String screen) 181 { 182 this(serverData); 183 setScreen(screen); 184 } 185 186 193 public TurbineURI(ServerData serverData, String screen, boolean redirect) 194 { 195 this(serverData, redirect); 196 setScreen(screen); 197 } 198 199 206 public TurbineURI(ServerData serverData, String screen, String action) 207 { 208 this(serverData, screen); 209 setAction(action); 210 } 211 212 220 public TurbineURI(ServerData serverData, String screen, String action, 221 boolean redirect) 222 { 223 this(serverData, screen, redirect); 224 setAction(action); 225 } 226 227 233 public TurbineURI(String screen, String action) 234 { 235 this(); 236 setScreen(screen); 237 setAction(action); 238 } 239 240 248 249 252 private void init() 253 { 254 dataVectors = new List [2]; 255 dataVectors[PATH_INFO] = new ArrayList (); 256 dataVectors[QUERY_DATA] = new ArrayList (); 257 } 258 259 268 public void setAction(String action) 269 { 270 if(StringUtils.isNotEmpty(action)) 271 { 272 add(PATH_INFO, CGI_ACTION_PARAM, action); 273 } 274 else 275 { 276 clearAction(); 277 } 278 } 279 280 286 public void setEvent(String event) 287 { 288 add(PATH_INFO, EVENT_PREFIX + event, event); 289 } 290 291 302 public void setActionEvent(String action, String event) 303 { 304 setAction(action); 305 if(StringUtils.isNotEmpty(event)) 306 { 307 setEvent(event); 308 } 309 } 310 311 314 public void clearAction() 315 { 316 removePathInfo(CGI_ACTION_PARAM); 317 } 318 319 328 public void setScreen(String screen) 329 { 330 if(StringUtils.isNotEmpty(screen)) 331 { 332 add(PATH_INFO, CGI_SCREEN_PARAM, screen); 333 } 334 else 335 { 336 clearScreen(); 337 } 338 } 339 340 343 public void clearScreen() 344 { 345 removePathInfo(CGI_SCREEN_PARAM); 346 } 347 348 355 356 357 363 public void addPathInfo(ParameterParser pp) 364 { 365 add(PATH_INFO, pp); 366 } 367 368 374 public void addPathInfo(List list) 375 { 376 add(PATH_INFO, list); 377 } 378 379 385 public void addPathInfo(String name, Object value) 386 { 387 add(PATH_INFO, name, value.toString()); 388 } 389 390 396 public void addPathInfo(String name, String value) 397 { 398 add(PATH_INFO, name, value); 399 } 400 401 407 public void addPathInfo(String name, double value) 408 { 409 add(PATH_INFO, name, Double.toString(value)); 410 } 411 412 418 public void addPathInfo(String name, int value) 419 { 420 add(PATH_INFO, name, Integer.toString(value)); 421 } 422 423 429 public void addPathInfo(String name, long value) 430 { 431 add(PATH_INFO, name, Long.toString(value)); 432 } 433 434 440 public void addQueryData(String name, Object value) 441 { 442 add(QUERY_DATA, name, value.toString()); 443 } 444 445 451 public void addQueryData(String name, String value) 452 { 453 add(QUERY_DATA, name, value); 454 } 455 456 462 public void addQueryData(String name, double value) 463 { 464 add(QUERY_DATA, name, Double.toString(value)); 465 } 466 467 473 public void addQueryData(String name, int value) 474 { 475 add(QUERY_DATA, name, Integer.toString(value)); 476 } 477 478 484 public void addQueryData(String name, long value) 485 { 486 add(QUERY_DATA, name, Long.toString(value)); 487 } 488 489 495 public void addQueryData(ParameterParser pp) 496 { 497 add(QUERY_DATA, pp); 498 } 499 500 505 public void addQueryData(List list) 506 { 507 add(QUERY_DATA, list); 508 } 509 510 515 public boolean hasPathInfo() 516 { 517 return !dataVectors[PATH_INFO].isEmpty(); 518 } 519 520 523 public void removePathInfo() 524 { 525 dataVectors[PATH_INFO].clear(); 526 } 527 528 533 public void removePathInfo(String name) 534 { 535 remove(PATH_INFO, name); 536 } 537 538 543 public boolean hasQueryData() 544 { 545 return !dataVectors[QUERY_DATA].isEmpty(); 546 } 547 548 551 public void removeQueryData() 552 { 553 dataVectors[QUERY_DATA].clear(); 554 } 555 556 561 public void removeQueryData(String name) 562 { 563 remove (QUERY_DATA, name); 564 } 565 566 572 public void clearResponse() 573 { 574 setResponse(null); 575 } 576 577 578 597 public String getAbsoluteLink() 598 { 599 StringBuffer output = new StringBuffer (); 600 601 getSchemeAndPort(output); 602 603 buildRelativeLink(output); 604 605 return encodeResponse(output.toString()); 609 } 610 611 630 public String getRelativeLink() 631 { 632 StringBuffer output = new StringBuffer (); 633 634 buildRelativeLink(output); 635 636 return encodeResponse(output.toString()); 640 } 641 642 647 private void buildRelativeLink(StringBuffer output) 648 { 649 getContextAndScript(output); 650 651 if (hasPathInfo()) 652 { 653 output.append('/'); 654 getPathInfoAsString(output); 655 } 656 657 if (hasReference()) 658 { 659 output.append('#'); 660 output.append(getReference()); 661 } 662 663 if (hasQueryData()) 664 { 665 output.append('?'); 666 getQueryDataAsString(output); 667 } 668 } 669 670 676 public List getPathInfo() 677 { 678 return dataVectors[PATH_INFO]; 679 } 680 681 688 689 public void setPathInfo(List pathInfo) 690 { 691 dataVectors[PATH_INFO] = pathInfo; 692 } 693 694 700 public List getQueryData() 701 { 702 return dataVectors[QUERY_DATA]; 703 } 704 705 712 713 public void setQueryData(List queryData) 714 { 715 dataVectors[QUERY_DATA] = queryData; 716 } 717 718 725 public String toString() 726 { 727 return getAbsoluteLink(); 728 } 729 730 738 739 744 private void getPathInfoAsString(StringBuffer output) 745 { 746 doEncode(output, dataVectors[PATH_INFO], '/', '/'); 747 } 748 749 754 private void getQueryDataAsString(StringBuffer output) 755 { 756 doEncode(output, dataVectors[QUERY_DATA], '&', '='); 757 } 758 759 767 private void doEncode(StringBuffer output, Collection list, char fieldDelim, char valueDelim) 768 { 769 if(!list.isEmpty()) 770 { 771 for(Iterator it = list.iterator(); it.hasNext();) 772 { 773 URIParam uriParam = (URIParam) it.next(); 774 String key = URLEncoder.encode(uriParam.getKey()); 775 String val = String.valueOf(uriParam.getValue()); 776 777 output.append(key); 778 output.append(valueDelim); 779 780 if(StringUtils.isEmpty(val)) 781 { 782 log.debug("Found a null value for " + key); 784 output.append("null"); 785 } 786 else 787 { 788 output.append(URLEncoder.encode(val)); 789 } 790 791 if (it.hasNext()) 792 { 793 output.append(fieldDelim); 794 } 795 } 796 } 797 } 798 799 810 protected void add(int type, 811 String name, 812 String value) 813 { 814 URIParam uriParam = new URIParam(ParserUtils.convertAndTrim(name), value); 815 816 dataVectors[type].add(uriParam); } 818 819 832 protected void add(int type, 833 ParameterParser pp) 834 { 835 for(Iterator it = pp.keySet().iterator(); it.hasNext();) 836 { 837 String key = (String ) it.next(); 838 839 if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) && 840 !key.equalsIgnoreCase(CGI_SCREEN_PARAM)) 841 { 842 String [] values = pp.getStrings(key); 843 for (int i = 0; i < values.length; i++) 844 { 845 add(type, key, values[i]); 846 } 847 } 848 } 849 } 850 851 864 protected void add(int type, 865 List list) 866 { 867 for (Iterator it = list.iterator(); it.hasNext();) 868 { 869 URIParam uriParam = (URIParam) it.next(); 873 dataVectors[type].add(uriParam); 874 } 875 } 876 877 887 protected void remove (int type, 888 String name) 889 { 890 Collection c = dataVectors[type]; 891 String key = ParserUtils.convertAndTrim(name); 892 893 for (Iterator it = c.iterator(); it.hasNext();) 894 { 895 URIParam uriParam = (URIParam) it.next(); 896 897 if (key.equals(uriParam.getKey())) 898 { 899 it.remove(); 900 } 901 } 902 } 903 } 904 | Popular Tags |