1 package org.apache.jetspeed.util; 2 3 18 19 import org.apache.jetspeed.portal.Portlet; 21 import org.apache.jetspeed.portal.PortletControl; 22 import org.apache.jetspeed.om.registry.PortletEntry; 23 import org.apache.jetspeed.services.Registry; 24 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 25 import org.apache.jetspeed.services.logging.JetspeedLogger; 26 import org.apache.jetspeed.services.resources.JetspeedResources; 27 import org.apache.jetspeed.services.rundata.JetspeedRunData; 28 29 import org.apache.turbine.util.ParameterParser; 31 import org.apache.turbine.util.RunData; 32 import org.apache.turbine.util.template.TemplateLink; 33 34 35 86 87 public class URILookup 88 { 89 92 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(URILookup.class.getName()); 93 104 public static final int TYPE_HOME = 0; 105 113 public static final int TYPE_INFO = 1; 114 115 123 public static final int TYPE_EDIT_ACCOUNT = 3; 124 132 public static final int TYPE_CUSTOMIZE = 4; 133 140 public static final int TYPE_LOGIN = 5; 141 149 public static final int TYPE_BACK = 6; 150 151 158 public static final int TYPE_ENROLLMENT = 7; 159 160 163 public static final int SUBTYPE_NONE = 0; 164 167 public static final int SUBTYPE_RESTORE = 1; 168 172 public static final int SUBTYPE_MAXIMIZE = 2; 173 176 public static final int SUBTYPE_MARK = 3; 177 180 public static final int SUBTYPE_LOGOUT = 4; 181 184 public static final int SUBTYPE_ACCEPT_LOGIN = 5; 185 188 public static final int SUBTYPE_SAVE = 6; 189 190 199 public static String getURI(int aType, 200 int aSubType, 201 RunData rundata) 202 throws JetspeedException 203 { 204 return getURI(aType, 205 aSubType, 206 null, 207 (String )null, 208 rundata); 209 } 210 211 220 public static String getURI(int aType, 221 int aSubType, 222 Portlet aPortlet, 223 RunData rundata) 224 throws JetspeedException 225 { 226 return getURI(aType, 227 aSubType, 228 null, 229 aPortlet, 230 rundata); 231 } 232 233 242 public static String getURI(int aType, 243 int aSubType, 244 String aPortletName, 245 RunData rundata) 246 throws JetspeedException 247 { 248 return getURI(aType, 249 aSubType, 250 null, 251 aPortletName, 252 rundata); 253 } 254 255 266 public static String getURI(int aType, 267 int aSubType, 268 String userData, 269 Portlet aPortlet, 270 RunData rundata) 271 throws JetspeedException 272 { 273 if (aPortlet!=null) { 274 aPortlet = getRealPortlet(aPortlet); 275 return getURI(aType, 276 aSubType, 277 userData, 278 aPortlet.getName(), 279 rundata); 280 } 281 else { 282 return getURI(aType, 283 aSubType, 284 userData, 285 (String )null, 286 rundata); 287 } 288 } 289 290 301 public static String getURI(int aType, 302 int aSubType, 303 String userData, 304 String aPortletName, 305 RunData rundata) 306 throws JetspeedException 307 { 308 String newURI = null; 309 310 String propertiesParameter = "URILookup."; 311 312 TemplateLink uri = new TemplateLink( rundata ); 313 314 if (aType==TYPE_HOME) 315 { 316 propertiesParameter += "home."; 317 318 if (aSubType==SUBTYPE_RESTORE) 319 { 320 propertiesParameter += "restore."; 321 322 newURI = getMarkedPage( rundata ); 323 } 324 else if (aSubType==SUBTYPE_MAXIMIZE) 325 { 326 propertiesParameter += "maximize."; 327 328 if (aPortletName==null) 329 { 330 throw new JetspeedException( "A portlet is required to return an URI." ); 331 } 332 uri.setAction( ACTION_MARKPAGE ); 333 uri.addPathInfo( "portlet", aPortletName ); 334 } 335 else if (aSubType==SUBTYPE_LOGOUT) 336 { 337 propertiesParameter += "logout."; 338 339 uri.setAction( ACTION_LOGOUT ); 340 } 341 else if (aSubType==SUBTYPE_ACCEPT_LOGIN) 342 { 343 propertiesParameter += "acceptlogin."; 344 345 uri.setAction( ACTION_ACCEPT_LOGIN ); 346 } 347 else if (aSubType!=SUBTYPE_NONE) 348 { 349 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 350 } 351 } 352 else if (aType==TYPE_INFO) 353 { 354 propertiesParameter += "info."; 355 356 if (aPortletName==null) 357 { 358 throw new JetspeedException( "A portlet is required to return an URI." ); 359 } 360 361 uri.setPage( SCREEN_INFO ); 362 uri.addPathInfo( "portlet", aPortletName ); 363 364 if (aSubType==SUBTYPE_MARK) 365 { 366 propertiesParameter += "mark."; 367 368 uri.setAction( ACTION_MARKPAGE ); 369 } 370 else if (aSubType!=SUBTYPE_NONE) 371 { 372 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 373 } 374 } 375 else if (aType==TYPE_EDIT_ACCOUNT) 376 { 377 propertiesParameter += "editaccount."; 378 379 uri.setPage( SCREEN_EDIT_ACCOUNT ); 380 381 if (aSubType==SUBTYPE_NONE) 382 { 383 uri.setAction( ACTION_PREPARE_SCREEN_EDIT_ACCOUNT ); 384 } 385 else if (aSubType==SUBTYPE_MARK) 386 { 387 propertiesParameter += "mark."; 388 389 if (aPortletName==null) 390 { 391 throw new JetspeedException( "A portlet is required to return an URI." ); 392 } 393 394 uri.setAction( ACTION_MARKPAGE ); 397 uri.addPathInfo( "portlet", aPortletName ); 398 } 399 else 400 { 401 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 402 } 403 } 404 else if (aType==TYPE_CUSTOMIZE) 405 { 406 propertiesParameter += "customize."; 407 408 uri.setPage( SCREEN_CUSTOMIZE ); 409 410 if( aPortletName != null ) 411 { 412 uri.addPathInfo( "portlet", aPortletName ); 413 } 414 if (aSubType==SUBTYPE_NONE) 415 { 416 if (ACTION_CUSTOMIZER!=null) uri.setAction( ACTION_CUSTOMIZER ); 417 } 418 else if (aSubType==SUBTYPE_SAVE) 419 { 420 propertiesParameter += "save."; 421 422 uri.setAction( ACTION_CUSTOMIZER_SAVE ); 423 } 424 else 425 { 426 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 427 } 428 } 429 else if (aType==TYPE_LOGIN) 430 { 431 propertiesParameter += "login."; 432 433 if (aSubType==SUBTYPE_NONE) 434 { 435 uri.setPage( SCREEN_LOGIN ); 436 } 437 else 438 { 439 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 440 } 441 } 442 else if (aType==TYPE_BACK) 443 { 444 propertiesParameter += "back."; 445 446 if (aSubType==SUBTYPE_NONE) 447 { 448 newURI = getMarkedPage( rundata ); 449 } 450 else 451 { 452 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 453 } 454 } 455 else if (aType==TYPE_ENROLLMENT) 456 { 457 propertiesParameter += "enrollment."; 458 uri.setPage( SCREEN_NEWACCOUNT ); 459 } 460 else 461 { 462 throw new JetspeedException( "Incorrect Type / Subtype combination." ); 463 } 464 465 if (newURI==null) 466 { 467 newURI = uri.toString(); 468 } 469 470 propertiesParameter += "uri"; 471 String propertiesParameterValue = JetspeedResources.getString( propertiesParameter, null ); 472 473 if (propertiesParameterValue!=null) 474 { 475 if ( logger.isInfoEnabled() ) 477 { 478 logger.info("URILookup: replaced uri "+newURI+" with "+propertiesParameterValue); 479 } 480 newURI = propertiesParameterValue; 481 } 482 483 if (userData!=null) 484 { 485 newURI = addURIParameter(newURI, userData); 486 } 487 488 if (newURI.indexOf(";jsessionid")!=-1) 490 { 491 newURI = newURI.substring(0,newURI.indexOf(";jsessionid")); 492 } 493 494 newURI = rundata.getResponse().encodeURL( newURI ); 496 497 if (newURI.startsWith("/")) 500 { 501 newURI = newURI.substring(1); 502 } 503 504 return newURI; 505 } 506 507 513 public static int getURIType(RunData rundata) 514 { 515 return getURIType( null, rundata ); 516 } 517 518 528 public static int getURIType(Portlet aPortlet, RunData rundata) 529 { 530 if (aPortlet!=null) 531 { 532 aPortlet = getRealPortlet(aPortlet); 533 534 if (aPortlet.getName().equals(((JetspeedRunData)rundata).getPortlet())) 535 { 536 if (rundata.getScreen()!=null) 537 { 538 if (rundata.getScreen().equals(SCREEN_INFO)) return TYPE_INFO; 539 } 540 } 541 } 542 543 if (rundata.getScreen()!=null) 544 { 545 if (rundata.getScreen().equals(SCREEN_CUSTOMIZE)) return TYPE_CUSTOMIZE; 546 else if (rundata.getScreen().equals(SCREEN_NEWACCOUNT)) return TYPE_ENROLLMENT; 547 else if (rundata.getScreen().equals(SCREEN_EDIT_ACCOUNT)) return TYPE_EDIT_ACCOUNT; 548 else if (rundata.getScreen().equals(SCREEN_LOGIN)) return TYPE_LOGIN; 549 } 550 551 return TYPE_HOME; 552 } 553 554 568 public static int getURISubType(Portlet aPortlet, RunData rundata) 569 throws JetspeedException 570 { 571 if (rundata.getAction()!=null) 572 { 573 if (rundata.getAction().equals(ACTION_ACCEPT_LOGIN)) return SUBTYPE_ACCEPT_LOGIN; 574 else if (rundata.getAction().equals(ACTION_LOGOUT)) return SUBTYPE_LOGOUT; 575 } 576 577 String value = (String )rundata.getRequest().getParameter("type"); 578 579 if (value!=null) 580 { 581 if (value.equalsIgnoreCase("save")) return SUBTYPE_SAVE; 582 } 583 584 if (aPortlet==null) throw new JetspeedException( "A portlet is required." ); 585 586 aPortlet = getRealPortlet(aPortlet); 587 588 if (aPortlet.getName().equals(((JetspeedRunData)rundata).getPortlet())) 589 { 590 if ((rundata.getScreen()==null) || ( SCREEN_HOME.equals(rundata.getScreen())) ) { 593 return SUBTYPE_MAXIMIZE; 594 } 595 } 596 597 return SUBTYPE_NONE; 598 } 599 600 607 public static String getURIUserData(RunData rundata) 608 { 609 return rundata.getParameters().getString("info"); 610 } 611 612 618 public static String getWebAppBaseDirURI(RunData rundata) 619 { 620 String ctxtPath = JetspeedResources.getString( JetspeedResources.CONTENT_ROOT_URL_KEY, ""); 621 String port = ""; 623 if( "http".equals( rundata.getServerScheme() ) && 624 rundata.getServerPort() != 80 ) { 625 port += ":" + rundata.getServerPort(); 626 } 627 if( "https".equals( rundata.getServerScheme() ) && 628 rundata.getServerPort() != 443 ) { 629 port += ":" + rundata.getServerPort(); 630 } 631 try { 632 ctxtPath = rundata.getRequest().getContextPath()+ctxtPath; 633 } catch (Exception e) { 634 logger.error( "Servlet container probably not 2.2", e ); 636 } 637 return rundata.getServerScheme()+"://"+ 638 rundata.getServerName()+ 639 port + ctxtPath; 640 } 641 642 647 public static void markCurrentPage(RunData rundata) 648 { 649 javax.servlet.http.HttpSession session = rundata.getSession(); 650 if (session != null) { 651 String uri = replaceTurbineURIParameter(rundata.getRequest().getRequestURI(), "action", null ); 653 654 session.setAttribute("URILookup_MarkedPage",uri); 655 } 658 } 659 660 666 public static void markPage(String aURI, RunData rundata) 667 { 668 javax.servlet.http.HttpSession session = rundata.getSession(); 669 if (session != null) { 670 aURI = replaceTurbineURIParameter(aURI, "action", null ); 672 673 session.setAttribute("URILookup_MarkedPage",aURI); 674 } 677 } 678 679 685 public static String getMarkedPage(RunData rundata) 686 { 687 return getMarkedPage( rundata, true ); 688 } 689 690 697 public static String getMarkedPage(RunData rundata, boolean relative) 698 { 699 javax.servlet.http.HttpSession session = rundata.getSession(); 700 if (session != null) { 701 String markedPage = (String )session.getAttribute("URILookup_MarkedPage"); 702 if ((markedPage!=null) && (relative)) { 705 int idx = markedPage.indexOf("://"); 707 if (idx!=-1) { idx = markedPage.indexOf("/",idx+3); if (idx!=-1) { idx = markedPage.indexOf("/",idx+1); if (idx!=-1) { markedPage = markedPage.substring(idx); 713 } 714 } 715 } 716 } 717 return markedPage; 718 } 719 return null; 720 } 721 722 732 public static final PortletEntry getEntry( ParameterParser params ) throws Exception 733 { 734 735 String name = params.getString("portlet"); 736 737 return (PortletEntry)Registry.getEntry(Registry.PORTLET, name ); 738 739 } 740 741 749 private static Portlet getRealPortlet( Portlet portlet ) 750 { 751 752 while (portlet instanceof PortletControl) { 753 portlet = ((PortletControl)portlet).getPortlet(); 754 } 755 756 return portlet; 757 } 758 759 768 private static String replaceTurbineURIParameter( String uri, String parameter, String value) 769 { 770 int idx = uri.indexOf("/" + parameter + "/"); 771 if (idx!=-1) { 772 int idx2 = uri.indexOf("/",idx+parameter.length()+2); 773 if (idx2==-1) idx2 = uri.length(); 775 uri = uri.substring(0,idx) + uri.substring(idx2); 776 } 777 if (value!=null) { 778 if (!uri.endsWith("/")) uri += "/"; 779 uri += parameter + "/" + value; 780 } 781 return uri; 782 } 783 784 791 private static String resetURIParameter( String uri ) 792 { 793 if (uri.indexOf("?")!=-1) { 794 uri = uri.substring(0,uri.indexOf("?")); 795 } 796 return uri; 797 } 798 799 807 private static String addURIParameter( String uri, String parameter, String value) 808 { 809 parameter = URIEncoder.encode( parameter ); 810 value = URIEncoder.encode( value ); 811 if (uri.indexOf("?")!=-1) { 812 int idx = uri.indexOf( parameter + "=", uri.indexOf("?")); 813 if (idx!=-1) { int idx2 = uri.indexOf("&", idx); 815 if (idx2==-1) idx2 = uri.length(); 817 uri = uri.substring(0,idx) + uri.substring(idx2); 818 } 819 } 820 return addURIParameter( uri, parameter + "=" + value ); 821 } 822 823 830 private static String addURIParameter( String uri, 831 String data) 832 { 833 if (uri.indexOf("?")!=-1) uri += "&"; 834 else uri += "?"; 835 uri += data; 836 return uri; 837 } 838 839 private static final String SCREEN_INFO = "Info"; 840 private static final String SCREEN_HOME = JetspeedResources.getString( "template.homepage" ); 841 private static final String SCREEN_CUSTOMIZE = JetspeedResources.getString( "customizer.screen" ); 843 private static final String SCREEN_LOGIN = JetspeedResources.getString( "template.login" ); 844 private static final String SCREEN_NEWACCOUNT = "NewAccount"; 845 private static final String SCREEN_EDIT_ACCOUNT = "EditAccount"; 846 847 private static final String ACTION_CUSTOMIZER = JetspeedResources.getString( "customizer.action" ); 848 private static final String ACTION_MARKPAGE = "MarkRefPage"; 849 private static final String ACTION_LOGOUT = JetspeedResources.getString( "action.logout" ); 850 private static final String ACTION_ACCEPT_LOGIN = JetspeedResources.getString( "action.login" ); 851 private static final String ACTION_CUSTOMIZER_SAVE = "SavePageConfig"; 852 private static final String ACTION_PREPARE_SCREEN_EDIT_ACCOUNT = "PrepareScreenEditAccount"; 853 } 854 | Popular Tags |