1 16 17 package org.apache.jetspeed.util.template; 18 19 import org.apache.jetspeed.om.profile.Entry; 21 import org.apache.jetspeed.om.profile.Profile; 22 import org.apache.jetspeed.om.profile.Portlets; 23 import org.apache.jetspeed.om.profile.ProfileException; 24 import org.apache.jetspeed.om.profile.ProfileLocator; 25 import org.apache.jetspeed.portal.Portlet; 26 import org.apache.jetspeed.services.Profiler; 27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 28 import org.apache.jetspeed.services.logging.JetspeedLogger; 29 import org.apache.jetspeed.services.resources.JetspeedResources; 30 import org.apache.jetspeed.services.rundata.JetspeedRunData; 31 import org.apache.jetspeed.util.template.JetspeedLink; 32 33 import org.apache.turbine.util.RunData; 35 import org.apache.turbine.util.DynamicURI; 36 import org.apache.turbine.services.pull.ApplicationTool; 37 38 50 public class BaseJetspeedLink implements ApplicationTool, JetspeedLink 51 { 52 55 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedLink.class.getName()); 56 57 60 private JetspeedRunData rundata = null; 61 62 66 private ProfileLocator locator = null; 67 68 72 private Profile profile = null; 73 74 78 private boolean initDone = false; 79 80 83 protected Portlet activePortlet = null; 84 85 88 public BaseJetspeedLink() 89 { 90 } 91 92 97 public BaseJetspeedLink(RunData data) 98 { 99 init((Object ) data); 100 } 101 102 108 public void init(RunData rundata) 109 { 110 init((Object ) rundata); 111 } 112 113 120 public DynamicURI addQueryData(String name, Object value) 121 { 122 try 123 { 124 return getRoot().addQueryData(name, value); 125 } 126 catch (ProfileException e) 127 { 128 logger.error("Exception", e); 129 return null; 130 } 131 } 132 133 140 public DynamicURI addPathInfo(String name, Object value) 141 { 142 try 143 { 144 return getRoot().addPathInfo(name, value); 145 } 146 catch (ProfileException e) 147 { 148 logger.error("Exception", e); 149 return null; 150 } 151 } 152 153 161 public DynamicURI setPortalElement(String peid) 162 { 163 if (initDone == false) 164 { 165 initLink(); 166 } 167 if (profile.getDocument().getEntryById(peid) != null) 168 { 169 return getPortletById(peid); 170 } 171 else 172 { 173 return setPaneById(peid); 174 } 175 } 176 177 185 public DynamicURI setPortletById(String peid) 186 { 187 return getPortletById(peid); 188 } 189 190 197 public DynamicURI getHomePage() 198 { 199 return getLink(JetspeedLink.DEFAULT, null, "", JetspeedLink.DEFAULT, null, "", "", "", "", ""); 200 } 201 202 208 public DynamicURI getTemplate() 209 { 210 String template = rundata.getRequestedTemplate(); 211 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, template, null, null, null); 212 } 213 214 222 public DynamicURI setTemplate(String template) 223 { 224 return getTemplate(template); 225 } 226 227 235 public DynamicURI setAction(String action) 236 { 237 return getAction(action); 238 } 239 240 250 public DynamicURI setAction(String action, Portlets portlets) 251 { 252 return getAction(action, (Portlets) portlets); 253 } 254 255 264 public DynamicURI setAction(String action, Portlet portlet) 265 { 266 return getAction(action, (Portlet) portlet); 267 } 268 269 279 public DynamicURI setAction(String action, Entry entry) 280 { 281 return getAction(action, (Entry) entry); 282 } 283 284 293 public DynamicURI setAction(String action, String peid) 294 { 295 return getAction(action, (String ) peid); 296 } 297 298 306 public DynamicURI setGroup(String group) 307 { 308 return getGroup(group); 309 } 310 311 320 public DynamicURI setGroup(String group, String page) 321 { 322 return getGroup(group, page); 323 } 324 325 333 public DynamicURI setPage() 334 { 335 return getPage(); 336 } 337 338 347 public DynamicURI setPage(String page) 348 { 349 return getPage(page); 350 } 351 352 361 public DynamicURI setPage(String page, String paneName) 362 { 363 return getPage(page, paneName); 364 } 365 366 367 375 public DynamicURI setRole(String role) 376 { 377 return getRole(role); 378 } 379 380 389 public DynamicURI setRole(String role, String page) 390 { 391 return getRole(role, page); 392 } 393 394 402 public DynamicURI setUser(String user) 403 { 404 return getUser(user); 405 } 406 407 416 public DynamicURI setUser(String user, String page) 417 { 418 return getUser(user, page); 419 } 420 421 429 public DynamicURI setPaneById(String paneId) 430 { 431 return getPaneById(paneId); 432 } 433 434 441 public DynamicURI setPaneByName(String paneName) 442 { 443 return getPaneByName(paneName); 444 } 445 446 480 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue, String actionName, String templateName, String mediaType, String language, String country) 481 { 482 String uriPathType = null; 483 String uriPathElement = null; 484 try 485 { 486 DynamicURI uri = getRoot(); 487 488 switch (rootType) 490 { 491 case JetspeedLink.DEFAULT: 492 case JetspeedLink.CURRENT: 493 break; 494 case JetspeedLink.GROUP: 495 uriPathType = Profiler.PARAM_GROUP; 496 break; 497 case JetspeedLink.ROLE: 498 uriPathType = Profiler.PARAM_ROLE; 499 break; 500 case JetspeedLink.USER: 501 uriPathType = Profiler.PARAM_USER; 502 break; 503 } 504 505 if (rootType != JetspeedLink.CURRENT) 506 { 507 uri.removePathInfo(Profiler.PARAM_GROUP); 509 uri.removePathInfo(Profiler.PARAM_ROLE); 510 uri.removePathInfo(Profiler.PARAM_USER); 511 512 if ((rootType != JetspeedLink.DEFAULT) && (rootValue != null) && (rootValue.trim().length() > 0)) 513 { 514 uri.addPathInfo(uriPathType, rootValue); 515 } 516 } 517 518 if (pageName != null) 520 { 521 uri.removePathInfo(Profiler.PARAM_PAGE); 522 if (pageName.trim().length() > 0) 523 { 524 uri.addPathInfo(Profiler.PARAM_PAGE, pageName); 525 } 526 } 527 528 switch (elementType) 530 { 531 case JetspeedLink.CURRENT: 532 case JetspeedLink.DEFAULT: 533 break; 534 case JetspeedLink.PANE_ID: 535 uriPathElement = JetspeedResources.PATH_PANEID_KEY; 536 break; 537 case JetspeedLink.PANE_NAME: 538 uriPathElement = JetspeedResources.PATH_PANENAME_KEY; 539 break; 540 case JetspeedLink.PORTLET_ID: 541 uriPathElement = JetspeedResources.PATH_PORTLETID_KEY; 542 break; 543 case JetspeedLink.PORTLET_NAME: 544 uriPathElement = JetspeedResources.PATH_PORTLET_KEY; 545 break; 546 case JetspeedLink.PORTLET_ID_QUERY: 547 if (logger.isDebugEnabled()) 548 { 549 logger.debug("BaseJetspeedLink: elementValue = " + elementValue); 550 } 551 uriPathElement = JetspeedResources.PATH_PORTLETID_KEY; 552 ProfileLocator baseLocator = Profiler.createLocator(); 553 Profile baseProfile = null; 554 switch (rootType) 555 { 556 case JetspeedLink.DEFAULT: 557 break; 558 case JetspeedLink.CURRENT: 559 baseProfile = rundata.getProfile(); 560 break; 561 case JetspeedLink.GROUP: 562 baseLocator.setGroupByName(rootValue); 563 break; 564 case JetspeedLink.ROLE: 565 baseLocator.setRoleByName(rootValue); 566 break; 567 case JetspeedLink.USER: 568 try 569 { 570 if (logger.isDebugEnabled()) 571 { 572 logger.debug("BaseJetspeedLink: rootValue user = " + rootValue); 573 } 574 baseLocator.setUser(org.apache.jetspeed.services.JetspeedSecurity.getUser(rootValue)); 575 } 576 catch (Exception se) 577 { 578 logger.error("Exception", se); 579 return null; 580 } 581 break; 582 } 583 584 if ((rootType != JetspeedLink.CURRENT) && (rootType != JetspeedLink.DEFAULT)) 585 { 586 if (mediaType != null && mediaType.length() > 0) 587 { 588 baseLocator.setMediaType(mediaType); 589 } 590 if (language != null && language.length() > 0) 591 { 592 baseLocator.setLanguage(language); 593 } 594 if (country != null && country.length() > 0) 595 { 596 baseLocator.setCountry(country); 597 } 598 if (pageName != null && pageName.length() > 0) 599 { 600 baseLocator.setName(pageName); 601 } 602 baseProfile = Profiler.getProfile(baseLocator); 603 } 604 605 if (logger.isDebugEnabled()) 606 { 607 logger.debug("BaseJetspeedLink: baseLocator = " + baseLocator.getPath()); 608 } 609 610 if ((baseProfile != null) && (elementValue != null)) 611 { 612 if (logger.isDebugEnabled()) 613 { 614 logger.debug("BaseJetspeedLink: baseProfile = " + baseProfile.toString()); 615 } 616 if (baseProfile.getDocument() != null) 617 { 618 if (logger.isDebugEnabled()) 619 { 620 logger.debug("BaseJetspeedLink: baseProfile.getDocment() = " + baseProfile.getDocument()); 621 } 622 Entry entry = baseProfile.getDocument().getEntry(elementValue); 623 if (entry != null) 624 { 625 if (logger.isDebugEnabled()) 626 { 627 logger.debug("BaseJetspeedLink: entry id = " + entry.getId()); 628 } 629 elementValue = entry.getId(); 630 } 631 else 632 { 633 elementValue = null; 634 } 635 } 636 } 637 638 break; 639 } 640 641 if (elementType != JetspeedLink.CURRENT) 642 { 643 uri.removePathInfo(JetspeedResources.PATH_PANEID_KEY); 645 uri.removePathInfo(JetspeedResources.PATH_PANENAME_KEY); 646 uri.removePathInfo(JetspeedResources.PATH_PORTLETID_KEY); 647 uri.removePathInfo(JetspeedResources.PATH_PORTLET_KEY); 648 649 if ((elementType != JetspeedLink.DEFAULT) && (elementValue != null) && (elementValue.length() > 0)) 650 { 651 uri.addPathInfo(uriPathElement, elementValue); 652 } 653 } 654 655 if (templateName != null) 657 { 658 uri.removePathInfo(JetspeedResources.PATH_TEMPLATE_KEY); 659 if (templateName.length() > 0) 660 { 661 uri.addPathInfo(JetspeedResources.PATH_TEMPLATE_KEY, templateName); 662 } 663 } 664 665 if (actionName != null) 667 { 668 uri.removeQueryData(JetspeedResources.PATH_ACTION_KEY); 669 if (actionName.length() > 0) 670 { 671 uri.addQueryData(JetspeedResources.PATH_ACTION_KEY, actionName); 672 } 673 } 674 675 if (mediaType != null) 677 { 678 uri.removePathInfo(Profiler.PARAM_MEDIA_TYPE); 679 if (mediaType.length() > 0) 680 { 681 uri.addPathInfo(Profiler.PARAM_MEDIA_TYPE, mediaType); 682 } 683 } 684 685 if (language != null) 687 { 688 uri.removePathInfo(Profiler.PARAM_LANGUAGE); 689 if (language.length() > 0) 690 { 691 uri.addPathInfo(Profiler.PARAM_LANGUAGE, language); 692 } 693 } 694 695 if (country != null) 697 { 698 uri.removePathInfo(Profiler.PARAM_COUNTRY); 699 if (country.length() > 0) 700 { 701 uri.addPathInfo(Profiler.PARAM_COUNTRY, country); 702 } 703 } 704 705 return uri; 706 } 707 catch (ProfileException e) 708 { 709 logger.error("Exception", e); 710 return null; 711 } 712 } 713 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue, String actionName, String templateName, String mediaType, String language) 714 { 715 return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, templateName, mediaType, language, null); 716 } 717 718 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue, String actionName, String templateName, String mediaType) 719 { 720 return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, templateName, mediaType, null, null); 721 } 722 723 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue, String actionName, String templateName) 724 { 725 return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, actionName, null, null, null); 726 } 727 728 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue, String actionName) 729 { 730 return getLink(rootType, rootValue, pageName, elementType, elementValue, actionName, null, null, null, null); 731 } 732 733 735 public DynamicURI getLink(int rootType, String rootValue, String pageName, int elementType, String elementValue) 736 { 737 return getLink(rootType, rootValue, pageName, elementType, elementValue, null, null, null, null, null); 738 } 739 740 746 public DynamicURI getAction(String action) 747 { 748 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.CURRENT, null, action, null, null, null, null); 749 } 750 751 759 public DynamicURI getAction(String action, Entry entry) 760 { 761 if (entry != null) 762 { 763 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, entry.getId(), null, action, null, null, null); 764 } 765 else 766 { 767 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, null, action, null, null, null); 768 } 769 } 770 771 778 public DynamicURI getAction(String action, Portlet portlet) 779 { 780 if (portlet != null) 781 { 782 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, portlet.getID(), action, null, null, null, null); 783 } 784 else 785 { 786 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, action, null, null, null, null); 787 } 788 } 789 790 798 public DynamicURI getAction(String action, Portlets portlets) 799 { 800 if (portlets != null) 801 { 802 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, portlets.getId(), action, null, null, null, null); 803 } 804 else 805 { 806 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, null, action, null, null, null, null); 807 } 808 } 809 810 818 public DynamicURI getAction(String action, String peid) 819 { 820 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.PORTLET_ID, peid, action, null, null, null, null); 821 } 822 823 829 public DynamicURI getGroup(String group) 830 { 831 return getLink(JetspeedLink.GROUP, group, "", JetspeedLink.DEFAULT, null, null, null, null, null, null); 832 } 833 834 841 public DynamicURI getGroup(String group, String page) 842 { 843 return getLink(JetspeedLink.GROUP, group, page, JetspeedLink.DEFAULT, null, null, null, null, null, null); 844 } 845 851 public DynamicURI getPage() 852 { 853 return getLink(JetspeedLink.CURRENT, null, "", JetspeedLink.DEFAULT, null, null, null, null, null, null); 854 } 855 856 863 public DynamicURI getPage(String page) 864 { 865 return getLink(JetspeedLink.CURRENT, null, page, JetspeedLink.DEFAULT, null, null, null, null, null, null); 866 } 867 868 876 public DynamicURI getPage(String page, String paneName) 877 { 878 return getLink(JetspeedLink.CURRENT, null, page, JetspeedLink.PANE_NAME, paneName, null, null, null, null, null); 879 } 880 886 public DynamicURI getPaneById(String paneId) 887 { 888 return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PANE_ID, paneId, null, null, null, null, null); 889 } 890 891 897 public DynamicURI getPaneByName(String paneName) 898 { 899 return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PANE_NAME, paneName, null, null, null, null, null); 900 } 901 902 908 public DynamicURI getPortletById(String peid) 909 { 910 return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PORTLET_ID, peid, null, null, null, null, null); 911 } 912 921 public DynamicURI getPortletByName(String portletName) 922 { 923 return getLink(JetspeedLink.CURRENT, null, this.getPageName(), JetspeedLink.PORTLET_NAME, portletName, null, null, null, null, null); 924 } 925 931 public DynamicURI getRole(String role) 932 { 933 return getLink(JetspeedLink.ROLE, role, "", JetspeedLink.DEFAULT, null, null, null, null, null, null); 934 } 935 936 943 public DynamicURI getRole(String role, String page) 944 { 945 return getLink(JetspeedLink.ROLE, role, page, JetspeedLink.DEFAULT, null, null, null, null, null, null); 946 } 947 948 954 public DynamicURI getTemplate(String template) 955 { 956 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, template, null, null, null); 957 } 958 959 965 public DynamicURI getUser(String user) 966 { 967 return getLink(JetspeedLink.USER, user, "", JetspeedLink.DEFAULT, null, null, null, null, null, null); 968 } 969 970 977 public DynamicURI getUser(String user, String page) 978 { 979 return getLink(JetspeedLink.USER, user, page, JetspeedLink.DEFAULT, null, null, null, null, null, null); 980 } 981 982 private void initLink() 983 { 984 if (initDone == true) 985 { 986 return; 987 } 988 989 try 990 { 991 profile = rundata.getProfile(); 993 994 if (profile == null) 996 { 997 profile = Profiler.getProfile(rundata); 1000 rundata.setProfile(profile); 1001 logger.warn("BaseJetspeedLink: profile in rundata was null"); 1002 } 1003 } 1004 catch (ProfileException e) 1005 { 1006 logger.error("Exception", e); 1007 } 1008 if (profile != null) 1009 { 1010 if ((profile instanceof ProfileLocator) == true) 1012 { 1013 locator = (ProfileLocator) profile; 1014 } 1015 } 1016 initDone = true; 1017 } 1018 1019 1025 private DynamicURI getRoot() throws ProfileException 1026 { 1027 DynamicURI uri = null; 1028 initLink(); 1029 if (locator != null) 1030 { 1031 uri = Profiler.makeDynamicURI(rundata, locator); 1032 } 1033 1034 if (uri == null) 1035 { 1036 uri = new DynamicURI(rundata); 1037 } 1038 1039 if (JetspeedResources.getBoolean("force.ssl", false)) 1041 { 1042 uri.setSecure(); 1043 } 1044 1045 return uri; 1046 } 1047 1048 1053 public String toString() 1054 { 1055 try 1056 { 1057 return getRoot().toString(); 1058 } 1059 catch (ProfileException e) 1060 { 1061 logger.error("Exception", e); 1062 return null; 1063 } 1064 } 1065 1066 1071 public String getActionKey() 1072 { 1073 return JetspeedResources.PATH_ACTION_KEY; 1074 } 1075 1076 1081 public boolean getAnonymous() 1082 { 1083 initLink(); 1084 try 1085 { 1086 return locator.getAnonymous(); 1087 } 1088 catch (Exception e) 1089 { 1090 logger.error("Exception", e); 1091 return true; 1092 } 1093 } 1094 1095 1100 public String getCountry() 1101 { 1102 initLink(); 1103 try 1104 { 1105 return locator.getCountry(); 1106 } 1107 catch (Exception e) 1108 { 1109 logger.error("Exception", e); 1110 return null; 1111 } 1112 } 1113 1114 1119 public String getGroupName() 1120 { 1121 initLink(); 1122 try 1123 { 1124 return locator.getGroupName(); 1125 } 1126 catch (Exception e) 1127 { 1128 logger.error("Exception", e); 1129 return null; 1130 } 1131 } 1132 1133 1138 public String getLanguage() 1139 { 1140 initLink(); 1141 try 1142 { 1143 return locator.getLanguage(); 1144 } 1145 catch (Exception e) 1146 { 1147 logger.error("Exception", e); 1148 return null; 1149 } 1150 } 1151 1152 1157 public String getMediaType() 1158 { 1159 initLink(); 1160 try 1161 { 1162 return locator.getMediaType(); 1163 } 1164 catch (Exception e) 1165 { 1166 logger.error("Exception", e); 1167 return null; 1168 } 1169 } 1170 1171 1176 public String getPageName() 1177 { 1178 initLink(); 1179 try 1180 { 1181 return locator.getName(); 1182 } 1183 catch (Exception e) 1184 { 1185 logger.error("Exception", e); 1186 return null; 1187 } 1188 } 1189 1190 1195 public String getRoleName() 1196 { 1197 initLink(); 1198 try 1199 { 1200 return locator.getRoleName(); 1201 } 1202 catch (Exception e) 1203 { 1204 logger.error("Exception", e); 1205 return null; 1206 } 1207 } 1208 1209 1214 public String getUserName() 1215 { 1216 initLink(); 1217 try 1218 { 1219 return locator.getUserName(); 1220 } 1221 catch (Exception e) 1222 { 1223 logger.error("Exception", e); 1224 return null; 1225 } 1226 } 1227 1228 1231 public static int getCURRENT() 1232 { 1233 return JetspeedLink.CURRENT; 1234 } 1235 public static int getDEFAULT() 1236 { 1237 return JetspeedLink.DEFAULT; 1238 } 1239 public static int getGROUP() 1240 { 1241 return JetspeedLink.GROUP; 1242 } 1243 public static int getPANE_ID() 1244 { 1245 return JetspeedLink.PANE_ID; 1246 } 1247 public static int getPANE_NAME() 1248 { 1249 return JetspeedLink.PANE_NAME; 1250 } 1251 public static int getPORTLET_ID() 1252 { 1253 return JetspeedLink.PORTLET_ID; 1254 } 1255 public static int getPORTLET_NAME() 1256 { 1257 return JetspeedLink.PORTLET_NAME; 1258 } 1259 public static int getROLE() 1260 { 1261 return JetspeedLink.ROLE; 1262 } 1263 public static int getUSER() 1264 { 1265 return JetspeedLink.USER; 1266 } 1267 public static String getDEFAULT_PAGE() 1268 { 1269 return ""; 1270 } 1271 1272 1275 1276 1287 public void setPortlet(Portlet portlet) 1288 { 1289 this.activePortlet = portlet; 1290 } 1291 1292 1293 1302 public DynamicURI setPortletByName(String portletName) 1303 { 1304 DynamicURI uri = null; 1305 try 1306 { 1307 uri = getRoot(); 1308 } 1309 catch (Exception e) 1310 { 1311 logger.error("Exception", e); 1312 return null; 1313 } 1314 if ((portletName != null) && (portletName.length() > 0)) 1315 { 1316 uri.addPathInfo(JetspeedResources.PATH_PORTLET_KEY, portletName); 1317 } 1318 return uri; 1319 } 1320 1321 1325 1326 1333 1334 public void init(Object data) 1335 { 1336 if (data instanceof JetspeedRunData) 1338 { 1339 this.rundata = (JetspeedRunData) data; 1340 } 1341 else 1342 { 1343 this.rundata = null; 1344 } 1345 profile = null; 1346 locator = null; 1347 initDone = false; 1348 return; 1349 } 1350 1353 public void refresh() 1354 { 1355 } 1357 1358 public DynamicURI setMediaType(String mediaType) 1359 { 1360 return getLink(JetspeedLink.CURRENT, null, null, JetspeedLink.DEFAULT, null, null, null, mediaType); 1361 } 1362 1363} 1364 | Popular Tags |