1 16 package com.blandware.atleap.webapp.util.core; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.NewsModuleConstants; 20 import com.blandware.atleap.common.TestimonialModuleConstants; 21 import com.blandware.atleap.common.util.CommonConverter; 22 import com.blandware.atleap.common.util.ConvertUtil; 23 import com.blandware.atleap.common.util.PartialCollection; 24 import com.blandware.atleap.common.util.StringUtil; 25 import com.blandware.atleap.model.core.*; 26 import com.blandware.atleap.service.core.ContentResourceManager; 27 import com.blandware.atleap.service.core.LayoutManager; 28 import com.blandware.atleap.service.core.LookupManager; 29 import com.blandware.atleap.service.core.PageManager; 30 import com.blandware.atleap.service.news.NewsManager; 31 import com.blandware.atleap.service.testimonials.TestimonialManager; 32 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition; 33 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants; 34 import com.blandware.atleap.webapp.util.testimonials.TestimonialModuleWebConstants; 35 import org.apache.commons.beanutils.BeanUtils; 36 import org.apache.commons.beanutils.ConvertUtils; 37 import org.apache.commons.beanutils.PropertyUtils; 38 import org.apache.struts.Globals; 39 import org.apache.struts.action.ActionMapping; 40 import org.apache.struts.config.ForwardConfig; 41 import org.apache.struts.config.ModuleConfig; 42 import org.apache.struts.tiles.DefinitionsFactoryException; 43 import org.apache.struts.tiles.TilesUtil; 44 import org.apache.struts.util.MessageResources; 45 import org.apache.struts.util.ModuleUtils; 46 import org.apache.struts.util.RequestUtils; 47 import org.springframework.context.ApplicationContext; 48 import org.springframework.web.context.support.WebApplicationContextUtils; 49 50 import javax.servlet.ServletContext ; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 53 import java.io.UnsupportedEncodingException ; 54 import java.lang.reflect.InvocationTargetException ; 55 import java.net.MalformedURLException ; 56 import java.net.URLEncoder ; 57 import java.nio.charset.Charset ; 58 import java.util.*; 59 60 68 public class WebappUtil { 69 70 77 public static String getContentFieldIndex(String identifier) { 78 if ( identifier == null ) { 79 return null; 80 } 81 82 String result = null; 83 int openBracket = identifier.lastIndexOf("["); 84 int closeBracket = identifier.lastIndexOf("]"); 85 if ( openBracket > 0 && closeBracket > 2 && closeBracket - openBracket > 1 ) { 86 try { 87 Double.valueOf(identifier.substring(openBracket + 1, closeBracket)); 89 result = identifier.substring(openBracket + 1, closeBracket); 90 } catch ( Exception ex ) { 91 } 93 } 94 return result; 95 } 96 97 101 public static class IndexedContentFieldComparator implements Comparator { 102 111 public int compare(Object o1, Object o2) { 112 if ( !(o1 instanceof ContentField) || !(o2 instanceof ContentField) ) { 113 throw new ClassCastException ("Only ContentFields can be compared by this comparator"); 114 } 115 116 ContentField cf1 = (ContentField) o1; 117 ContentField cf2 = (ContentField) o2; 118 119 String index1 = getContentFieldIndex(cf1.getIdentifier()); 120 String index2 = getContentFieldIndex(cf2.getIdentifier()); 121 122 if ( index1 == null || index2 == null ) { 123 throw new ClassCastException ("Only Indexed ContentFields can be compared by this comparator"); 124 } 125 126 Double i1 = Double.valueOf(index1); 127 Double i2 = Double.valueOf(index2); 128 129 return i1.compareTo(i2); 130 } 131 } 132 133 134 143 public static Collection joinFields(Collection parentFields, Collection childFields) { 144 if ( parentFields == null || parentFields.size() == 0 ) { 145 return childFields; 146 } 147 if ( childFields == null || childFields.size() == 0 ) { 148 return parentFields; 149 } 150 Map fields = new HashMap(); 151 for ( Iterator iterator = parentFields.iterator(); iterator.hasNext(); ) { 152 ContentField contentField = (ContentField) iterator.next(); 153 String key = contentField.getIdentifier(); 154 fields.put(key, contentField); 155 } 156 for ( Iterator iterator = childFields.iterator(); iterator.hasNext(); ) { 157 ContentField contentField = (ContentField) iterator.next(); 158 String key = contentField.getIdentifier(); 159 fields.put(key, contentField); 160 } 161 return fields.values(); 162 } 163 164 171 public static String rolesToString(Collection roles) { 172 if ( roles == null || roles.size() == 0 ) { 173 return ""; 174 } 175 List roleNames = new ArrayList(); 176 for ( Iterator iterator = roles.iterator(); iterator.hasNext(); ) { 178 Role role = (Role) iterator.next(); 179 roleNames.add(role.getName()); 180 } 181 return ConvertUtil.convertListToString(roleNames, ","); 182 } 183 184 197 public static String getActionMappingURL(String action, String module, HttpServletRequest request, int urlType) { 198 return getActionMappingURL(action, module, request, urlType, true); 199 } 200 201 214 public static String getActionMappingURL(String action, String module, HttpServletRequest request, int urlType, boolean addLocaleSuffix) { 215 Locale locale = RequestUtils.getUserLocale(request, null); 216 String language = null; 217 if ( locale != null && addLocaleSuffix ) { 218 language = locale.getLanguage(); 219 } 220 return getActionMappingURL(action, module, request, urlType, language); 221 } 222 223 224 237 public static String getActionMappingURL(String action, String module, HttpServletRequest request, int urlType, String localeSuffix) { 238 239 StringBuffer url = new StringBuffer (); 240 ServletContext servletContext = request.getSession().getServletContext(); 241 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(module, request, servletContext); 242 243 String servletMapping = (String ) servletContext.getAttribute(Globals.SERVLET_KEY); 245 246 if ( urlType == WebappConstants.URL_TYPE_DOMAIN_RELATIVE ) { 248 url.append(request.getContextPath()); 249 } 250 251 if ( servletMapping != null && servletMapping.startsWith("*.") ) { 253 if ( localeSuffix != null && localeSuffix.trim().length() > 0 && !action.startsWith(Constants.LOCALIZED_URI_PREFIX) ) { 254 url.append(Constants.LOCALIZED_URI_PREFIX); 255 } 256 } 257 258 if ( (urlType == WebappConstants.URL_TYPE_DOMAIN_RELATIVE || urlType == WebappConstants.URL_TYPE_CONTEXT_RELATIVE) && moduleConfig != null ) { 260 url.append(moduleConfig.getPrefix()); 261 } 262 263 if ( servletMapping != null ) { 265 266 String queryString = null; 267 int questionMark = action.indexOf("?"); 268 if ( questionMark >= 0 ) { 269 queryString = action.substring(questionMark); 270 } 271 272 String actionMapping = getActionMappingName(action); 273 if ( servletMapping.startsWith("*.") ) { 274 275 url.append(actionMapping); 276 if ( localeSuffix != null && localeSuffix.trim().length() > 0 ) { 277 url.append(".").append(localeSuffix); 278 } 279 url.append(servletMapping.substring(1)); 280 } else if ( servletMapping.endsWith("/*") ) { 281 url.append(servletMapping.substring(0, servletMapping.length() - 2)); 282 url.append(actionMapping); 283 284 } else if ( servletMapping.equals("/") ) { 285 url.append(actionMapping); 286 } 287 288 if ( queryString != null ) { 289 url.append(queryString); 290 } 291 } 292 293 else { 296 if ( !action.startsWith("/") ) { 297 url.append("/"); 298 } 299 url.append(action); 300 } 301 302 return url.toString(); 303 } 304 305 319 protected static String getActionMappingName(String action) { 320 321 String value = action; 322 int question = action.indexOf("?"); 323 if ( question >= 0 ) { 324 value = value.substring(0, question); 325 } 326 327 int slash = value.lastIndexOf("/"); 328 int period = value.lastIndexOf("."); 329 if ( (period >= 0) && (period > slash) ) { 330 value = value.substring(0, period); 331 } 332 333 return value.startsWith("/") ? value : ("/" + value); 334 } 335 336 344 public static ForwardConfig findGlobalForwardConfig(String globalForward, String module, HttpServletRequest request) { 345 ServletContext servletContext = request.getSession().getServletContext(); 346 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(module, request, servletContext); 347 ForwardConfig forwardConfig = null; 348 if ( moduleConfig != null ) { 349 forwardConfig = moduleConfig.findForwardConfig(globalForward); 350 } 351 return forwardConfig; 352 } 353 354 355 365 public static String getActionForwardURL(String globalForward, String module, HttpServletRequest request, int urlType) throws MalformedURLException { 366 return getActionForwardURL(globalForward, module, request, urlType, true); 367 } 368 369 379 public static String getActionForwardURL(String globalForward, String module, HttpServletRequest request, int urlType, boolean addLocaleSuffix) throws MalformedURLException { 380 Locale locale = RequestUtils.getUserLocale(request, null); 381 String language = null; 382 if ( locale != null && addLocaleSuffix ) { 383 language = locale.getLanguage(); 384 } 385 return getActionForwardURL(globalForward, module, request, urlType, language); 386 } 387 388 398 public static String getActionForwardURL(String globalForward, String module, HttpServletRequest request, int urlType, String localeSuffix) throws MalformedURLException { 399 ForwardConfig forwardConfig = findGlobalForwardConfig(globalForward, module, request); 400 if ( forwardConfig == null ) { 401 throw new MalformedURLException ("No global forward with name '" + globalForward + "' could be found"); 402 } 403 return getActionForwardURL(forwardConfig, module, request, urlType, localeSuffix); 404 } 405 406 416 public static String getActionForwardURL(ForwardConfig forwardConfig, String module, HttpServletRequest request, int urlType) { 417 return getActionForwardURL(forwardConfig, module, request, urlType, true); 418 } 419 420 430 public static String getActionForwardURL(ForwardConfig forwardConfig, String module, HttpServletRequest request, int urlType, boolean addLocaleSuffix) { 431 Locale locale = RequestUtils.getUserLocale(request, null); 432 String language = null; 433 if ( locale != null && addLocaleSuffix ) { 434 language = locale.getLanguage(); 435 } 436 return getActionForwardURL(forwardConfig, module, request, urlType, language); 437 } 438 439 449 public static String getActionForwardURL(ForwardConfig forwardConfig, String module, HttpServletRequest request, int urlType, String localeSuffix) { 450 StringBuffer url = new StringBuffer (); 451 ServletContext servletContext = request.getSession().getServletContext(); 452 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(module, request, servletContext); 453 String path = forwardConfig.getPath(); 454 455 if ( !isExternalURL(path, request) ) { 456 457 if ( !isAbsoluteURL(path) ) { 458 if ( urlType == WebappConstants.URL_TYPE_DOMAIN_RELATIVE ) { 459 String contextPath = request.getContextPath(); 460 if ( !path.startsWith(contextPath) && !contextPath.equals("/") ) { 461 url.append(contextPath); 462 } 463 } 464 url.append(RequestUtils.forwardURL(request, forwardConfig, moduleConfig)); 465 } else { 466 url.append(path); 467 } 468 469 if ( localeSuffix != null && localeSuffix.trim().length() > 0 ) { 471 String servletMapping = (String ) servletContext.getAttribute(Globals.SERVLET_KEY); 473 String ext = null; 474 if ( servletMapping != null && servletMapping.startsWith("*.") ) { 475 ext = servletMapping.substring(1); 476 } else { 477 ext = ".jsp"; 478 } 479 480 String temp = url.toString(); 481 int questionMark = url.indexOf("?"); 482 if ( questionMark >= 0 ) { 483 temp = url.substring(0, questionMark); 484 } 485 486 int extDot = temp.lastIndexOf(ext); 488 if ( extDot < 0 && !ext.equals(".jsp") ) { 489 extDot = temp.lastIndexOf(".jsp"); 490 } 491 if ( extDot > 0 ) { 492 493 495 int fromIndex = 0; 496 if ( isAbsoluteURL(path) ) { 497 fromIndex = path.indexOf("://"); 499 if ( fromIndex < 0 ) { 500 fromIndex = path.indexOf(":") + 1; 501 } else { 502 fromIndex += 3; 503 } 504 } 505 506 String contextPath = request.getContextPath(); 507 if ( contextPath == null || contextPath.length() == 0 ) { 508 contextPath = "/"; 509 } 510 511 int suffixPos = extDot; 513 514 int correction = 0; 516 517 int insertPos; 519 if ("/".equals(contextPath)) { 520 insertPos = url.indexOf(contextPath, fromIndex); 521 } else { 522 insertPos = url.indexOf(contextPath, fromIndex) + contextPath.length(); 523 } 524 525 if ( url.indexOf(Constants.LOCALIZED_URI_PREFIX, insertPos) != insertPos ) { 526 url.insert(insertPos, Constants.LOCALIZED_URI_PREFIX); 527 correction = Constants.LOCALIZED_URI_PREFIX.length(); 528 suffixPos += correction; 529 } 530 531 temp = temp.substring(0, extDot); 532 533 int langDot = temp.lastIndexOf("."); 535 if ( langDot != -1 && langDot + correction == suffixPos - 3 ) { 536 url.replace(langDot + correction, suffixPos, "." + localeSuffix); 537 } else { 538 url.insert(suffixPos, "." + localeSuffix); 539 } 540 } 541 } 542 543 } else { 544 url.append(path); 545 } 546 547 return url.toString(); 548 } 549 550 569 public static String computeURL(String action, String forward, String href, String anchor, Map parameters, HttpServletRequest request, HttpServletResponse response, boolean encode, boolean redirect) throws MalformedURLException { 570 return computeURL(action, forward, href, anchor, parameters, request, response, encode, redirect, true); 571 } 572 573 592 public static String computeURL(String action, String forward, String href, String anchor, Map parameters, HttpServletRequest request, HttpServletResponse response, boolean encode, boolean redirect, boolean addLocaleSuffix) throws MalformedURLException { 593 Locale locale = RequestUtils.getUserLocale(request, null); 594 String language = null; 595 if ( locale != null && addLocaleSuffix ) { 596 language = locale.getLanguage(); 597 } 598 return computeURL(action, forward, href, anchor, parameters, request, response, encode, redirect, language); 599 } 600 601 602 621 public static String computeURL(String action, String forward, String href, String anchor, Map parameters, HttpServletRequest request, HttpServletResponse response, boolean encode, boolean redirect, String localeSuffix) throws MalformedURLException { 622 623 int n = 0; 625 if ( action != null ) { 626 n++; 627 } 628 if ( forward != null ) { 629 n++; 630 } 631 if ( href != null ) { 632 n++; 633 } 634 635 if ( n > 1 ) { 636 throw new MalformedURLException ("One and only one of 'action', 'forward' or 'href' attributes must be specified"); 637 } else if ( n == 0 ) { 638 return null; 639 } 640 641 StringBuffer url = null; 642 643 boolean urlIsExternal = false; 645 646 if ( action != null ) { 647 url = new StringBuffer (getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE, localeSuffix)); 648 } else if ( forward != null ) { 649 String urlString = getActionForwardURL(forward, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE, localeSuffix); 650 url = new StringBuffer (urlString); 651 urlIsExternal = isExternalURL(urlString, request); 652 } else if ( href != null ) { 653 urlIsExternal = isExternalURL(href, request); 654 url = new StringBuffer (); 655 656 if ( !urlIsExternal ) { 658 if ( !isAbsoluteURL(href) ) { 660 String contextPath = request.getContextPath(); 661 if ( !href.startsWith(contextPath) && !contextPath.equals("/") ) { 662 url.append(contextPath); 663 } 664 } 665 666 url.append(href); 667 668 int questionMark = url.indexOf("?"); 669 String temp = url.toString(); 670 if ( questionMark != -1 ) { 671 temp = url.substring(0, questionMark); 672 } 673 674 if ( localeSuffix != null && localeSuffix.trim().length() > 0 ) { 676 String servletMapping = (String ) request.getSession().getServletContext().getAttribute(Globals.SERVLET_KEY); 677 String ext = null; 678 if ( servletMapping != null && servletMapping.startsWith("*.") ) { 679 ext = servletMapping.substring(1); 680 } else { 681 ext = ".jsp"; 682 } 683 684 int extDot = temp.lastIndexOf(ext); 686 if ( extDot < 0 && !ext.equals(".jsp") ) { 687 extDot = temp.lastIndexOf(".jsp"); 688 } 689 if ( extDot > 0 ) { 690 691 693 int fromIndex = 0; 694 if ( isAbsoluteURL(href) ) { 695 fromIndex = href.indexOf("://"); 697 if ( fromIndex < 0 ) { 698 fromIndex = href.indexOf(":") + 1; 699 } else { 700 fromIndex += 3; 701 } 702 } 703 704 String contextPath = request.getContextPath(); 705 if ( contextPath == null || contextPath.length() == 0 ) { 706 contextPath = "/"; 707 } 708 709 int suffixPos = extDot; 711 712 int correction = 0; 714 715 int insertPos; 717 if ("/".equals(contextPath)) { 718 insertPos = url.indexOf(contextPath, fromIndex); 719 } else { 720 insertPos = url.indexOf(contextPath, fromIndex) + contextPath.length(); 721 } 722 723 if ( url.indexOf(Constants.LOCALIZED_URI_PREFIX, insertPos) != insertPos ) { 724 url.insert(insertPos, Constants.LOCALIZED_URI_PREFIX); 725 correction = Constants.LOCALIZED_URI_PREFIX.length(); 726 suffixPos += correction; 727 } 728 729 temp = temp.substring(0, extDot); 730 731 int langDot = temp.lastIndexOf("."); 733 if ( langDot != -1 && langDot + correction == suffixPos - 3 ) { 734 url.replace(langDot + correction, suffixPos, "." + localeSuffix); 735 } else { 736 url.insert(suffixPos, "." + localeSuffix); 737 } 738 } 739 } 740 } else { 741 url.append(href); 742 } 743 } 744 745 if ( anchor != null && anchor.trim().length() > 0 ) { 747 int anchorPos = url.indexOf("#"); 748 if ( anchorPos > 0 ) { 749 url.setLength(anchorPos); 750 } 751 url.append('#').append(anchor); 752 } 753 754 Map resultParams = new HashMap(); 756 int k = url.indexOf("?"); 757 if ( k != -1 ) { 758 String queryString = url.substring(k + 1); 759 url.setLength(k); 760 resultParams = RequestUtil.getRequestParametersFromString(queryString); 761 if ( parameters != null ) { 762 for ( Iterator i = parameters.entrySet().iterator(); i.hasNext(); ) { 763 Map.Entry entry = (Map.Entry) i.next(); 764 String key = (String ) entry.getKey(); 765 Object value = entry.getValue(); 766 resultParams.put(key, RequestUtil.mergeValues(resultParams.get(key), value)); 767 } 768 } 769 } else if ( parameters != null ) { 770 resultParams.putAll(parameters); 771 } 772 773 if ( !resultParams.isEmpty() ) { 774 int anchorPos = url.indexOf("#"); 776 if ( anchorPos > 0 ) { 777 anchor = url.substring(anchorPos); 778 url.setLength(anchorPos); 779 if ( encode ) { 780 try { 781 anchor = URLEncoder.encode(anchor, Constants.DEFAULT_ENCODING); 782 } catch ( UnsupportedEncodingException e ) { 783 } 785 } 786 } else { 787 anchor = null; 788 } 789 790 url.append("?").append(RequestUtil.createQueryStringFromMap(resultParams, "&", encode && !urlIsExternal)); 791 792 if ( anchor != null && anchor.trim().length() > 0 ) { 794 url.append('#').append(anchor); 795 } 796 } 797 798 String result = url.toString(); 799 800 if ( !urlIsExternal ) { 801 if ( redirect ) { 802 result = response.encodeRedirectURL(result); 803 } else { 804 result = response.encodeURL(result); 805 } 806 } 807 return result; 808 } 809 810 811 839 public static String getFieldValue(Map values, String localeIdentifier, HttpServletRequest request, boolean complexSearch) { 840 841 if ( values == null || values.isEmpty() ) { 842 return null; 843 } 844 845 String value = (String ) values.get(localeIdentifier); 846 if ( (value == null || value.length() == 0) && complexSearch ) { 847 ContentLocale defaultLocale = LocaleUtil.getInstance(request.getSession().getServletContext()).getDefaultLocale(); 849 value = (String ) values.get(defaultLocale.getIdentifier()); 850 851 if ( value == null || value.length() == 0 ) { 852 value = (String ) values.values().iterator().next(); 854 } 855 } 856 857 return value; 858 } 859 860 867 public static String changeUriExtension(String uri, String newExtension) { 868 int slash = uri.lastIndexOf("/"); 869 int period = uri.lastIndexOf("."); 870 if ( (period >= 0) && (period > slash) ) { 871 String uriWithout = uri.substring(0, period); 872 return uriWithout + newExtension; 873 } else { 874 return uri; 875 } 876 } 877 878 879 891 public static String getLocalizableInfo(Long localizableId, HttpServletRequest request) { 892 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext()); 893 LookupManager lookupManager = (LookupManager) applicationContext.getBean(Constants.LOOKUP_MANAGER_BEAN); 894 Localizable owner = lookupManager.retrieveLocalizable(localizableId); 895 return getLocalizableInfo(owner, request); 896 } 897 898 911 public static String getLocalizableInfo(Localizable localizable, HttpServletRequest request) { 912 if ( localizable == null ) { 913 return new String (); 914 } 915 String localizableInfo = new String (); 916 if ( localizable instanceof Layout ) { 917 Layout layout = (Layout) localizable; 918 localizableInfo = layout.getName(); 919 } else if ( localizable instanceof Page ) { 920 Map title = new HashMap(); 921 ContentField titleField = (ContentField) localizable.getContentFieldsMap().get("title"); 922 if ( titleField != null ) { 923 for ( Iterator i = titleField.getContentFieldValues().iterator(); i.hasNext(); ) { 924 ContentFieldValue contentFieldValue = (ContentFieldValue) i.next(); 925 title.put(contentFieldValue.getContentLocale().getIdentifier(), contentFieldValue.getSimpleValue()); 926 } 927 Locale locale = RequestUtils.getUserLocale(request, null); 928 localizableInfo = getFieldValue(title, locale.getLanguage(), request, true); 929 } 930 } else if ( localizable instanceof MailTemplate ) { 931 MailTemplate mailTemplate = (MailTemplate) localizable; 932 localizableInfo = mailTemplate.getIdentifier(); 933 } 934 935 localizableInfo = StringUtil.htmlEncode(localizableInfo); 937 938 MessageResources resources = (MessageResources) request.getAttribute(Globals.MESSAGES_KEY); 939 String ownerClassName = localizable.getClass().getName(); 940 int k = ownerClassName.lastIndexOf('.'); 942 ownerClassName = ownerClassName.substring(k + 1); 943 String key = "core.localizable.form.class." + ownerClassName; 944 Locale locale = RequestUtils.getUserLocale(request, null); 945 946 String msg = resources.getMessage(locale, key, localizableInfo); 947 return (msg == null || msg.length() == 0) ? localizableInfo : msg; 948 } 949 950 956 public static boolean hasCorrectValues(Map values) { 957 if (values == null) 958 return false; 959 boolean result = true; 960 for ( Iterator i = values.keySet().iterator(); i.hasNext(); ) { 961 String key = (String ) i.next(); 962 String value = (String ) values.get(key); 963 result = result && (value != null && value.length() > 0); 964 } 965 return result; 966 } 967 968 980 public static synchronized void copyProperties(Object dest, Object orig, HttpServletRequest request) throws IllegalAccessException , InvocationTargetException { 981 982 boolean requestSpecified = request != null; 983 984 CommonConverter converter = new CommonConverter(); 985 986 if ( requestSpecified ) { 987 Locale locale = RequestUtils.getUserLocale(request, null); 988 converter = new CommonConverter(locale); 989 } 990 991 ConvertUtils.register(converter, Date.class); 992 ConvertUtils.register(converter, String .class); 993 994 BeanUtils.copyProperties(dest, orig); 995 996 if ( requestSpecified ) { 997 ConvertUtils.register(new CommonConverter(), String .class); 999 ConvertUtils.register(new CommonConverter(), Date.class); 1000 } 1001 1002 } 1003 1004 1015 public static synchronized void copyProperties(Object dest, Object orig) throws IllegalAccessException , InvocationTargetException { 1016 copyProperties(dest, orig, null); 1017 } 1018 1019 1028 public static Set getIndexedOverridableFieldIdentifiers(Localizable owner, HttpServletRequest request) throws DefinitionsFactoryException { 1029 return getOverridableFieldIdentifiers(owner, request, true, true); 1030 } 1031 1032 1041 public static Set getUnIndexedOverridableFieldIdentifiers(Localizable owner, HttpServletRequest request) throws DefinitionsFactoryException { 1042 return getOverridableFieldIdentifiers(owner, request, true, false); 1043 } 1044 1045 1054 public static Set getOverridableFieldIdentifiers(Localizable owner, HttpServletRequest request) throws DefinitionsFactoryException { 1055 return getOverridableFieldIdentifiers(owner, request, false, false); 1056 } 1057 1058 1071 public static Set getOverridableFieldIdentifiers(Localizable owner, HttpServletRequest request, boolean considerIndexed, boolean indexed) throws DefinitionsFactoryException { 1072 1073 ServletContext servletContext = request.getSession().getServletContext(); 1074 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 1075 LayoutManager layoutManager = (LayoutManager) applicationContext.getBean(Constants.LAYOUT_MANAGER_BEAN); 1076 1077 Set fieldIdentifiers = new HashSet(); 1080 String layoutDefinition = null; 1081 if ( owner instanceof ContentPage || owner instanceof Layout ) { 1082 if ( owner instanceof ContentPage ) { 1086 ContentPage contentPage = (ContentPage) owner; 1087 layoutDefinition = contentPage.getLayout().getDefinition(); 1088 } else { 1089 Layout layout = (Layout) owner; 1090 layoutDefinition = layout.getDefinition(); 1091 } 1092 do { 1093 Layout layout = layoutManager.findLayoutByDefinition(layoutDefinition); 1094 if ( layout != null && (!(owner instanceof Layout) || !layout.equals(owner)) ) { 1095 fieldIdentifiers.addAll(layout.getContentFieldsMap().keySet()); 1097 } 1098 layoutDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(layoutDefinition, request, servletContext)).getExtends(); 1099 } while ( layoutDefinition != null ); 1100 } else if ( owner instanceof ActionPage ) { 1101 PartialCollection layouts = layoutManager.listLayouts(null); 1103 for ( Iterator i = layouts.iterator(); i.hasNext(); ) { 1104 Layout layout = (Layout) i.next(); 1105 fieldIdentifiers.addAll(layout.getContentFieldsMap().keySet()); 1106 } 1107 } else { 1108 return new HashSet(); 1109 } 1110 1111 Set identifiers = new HashSet(); 1113 for ( Iterator i = fieldIdentifiers.iterator(); i.hasNext(); ) { 1114 String identifier = (String ) i.next(); 1115 1116 int k = identifier.indexOf('['); 1117 if ( k != -1 ) { 1118 identifier = identifier.substring(0, k); 1119 } 1120 if ( owner instanceof ContentPage || owner instanceof ActionPage ) { 1121 ContentField pageField = (ContentField) owner.getContentFieldsMap().get(identifier); 1122 if ( pageField != null && pageField.getInternal().booleanValue() ) { 1123 continue; 1125 } 1126 } else if ( owner instanceof Layout ) { 1127 1133 } 1134 boolean fieldIsIndexed = (k != -1); 1135 if (considerIndexed) { 1136 if ((fieldIsIndexed && !indexed) || (!fieldIsIndexed && indexed)) { 1137 continue; 1138 } 1139 } 1140 1141 identifiers.add(identifier); 1142 } 1143 return identifiers; 1144 } 1145 1146 1153 public static Set getLocalizableUnIndexedFieldsIdentifiers(Localizable localizable) { 1154 Set result = new TreeSet(); 1155 List fields = localizable.getContentFields(); 1156 for (int i = 0; i < fields.size(); i++) { 1157 ContentField field = (ContentField) fields.get(i); 1158 if (field.getIndex() == null) { 1159 result.add(field.getIdentifierWithoutIndex()); 1160 } 1161 } 1162 return result; 1163 } 1164 1165 1173 public static Set getIndexedFieldIdentifiers(String layoutDefinition, HttpServletRequest request) throws DefinitionsFactoryException { 1174 return getFieldIdentifiers(layoutDefinition, request, true); 1175 } 1176 1177 1185 public static Set getUnIndexedFieldIdentifiers(String layoutDefinition, HttpServletRequest request) throws DefinitionsFactoryException { 1186 return getFieldIdentifiers(layoutDefinition, request, false); 1187 } 1188 1189 1190 1199 protected static Set getFieldIdentifiers(String layoutDefinition, HttpServletRequest request, boolean indexed) throws DefinitionsFactoryException { 1200 ServletContext servletContext = request.getSession().getServletContext(); 1201 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 1202 LayoutManager layoutManager = (LayoutManager) applicationContext.getBean(Constants.LAYOUT_MANAGER_BEAN); 1203 1204 Set fieldIdentifiers = new HashSet(); 1205 1206 do { 1207 Layout layout = layoutManager.findLayoutByDefinition(layoutDefinition); 1208 if ( layout != null ) { 1209 fieldIdentifiers.addAll(layout.getContentFieldsMap().keySet()); 1210 } 1211 layoutDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(layoutDefinition, request, servletContext)).getExtends(); 1212 } while ( layoutDefinition != null ); 1213 1214 Set identifiers = new HashSet(); 1215 for ( Iterator i = fieldIdentifiers.iterator(); i.hasNext(); ) { 1216 String identifier = (String ) i.next(); 1217 int k = identifier.indexOf('['); 1218 if ( k != -1 ) { 1219 identifier = identifier.substring(0, k); 1220 if ( indexed ) { 1221 identifiers.add(identifier); 1222 } 1223 } else { 1224 if ( !indexed ) { 1225 identifiers.add(identifier); 1226 } 1227 } 1228 } 1229 return identifiers; 1230 } 1231 1232 1238 public static boolean isCharsetSupported(String charset) { 1239 boolean result = false; 1240 try { 1241 result = Charset.isSupported(charset); 1242 } catch ( Exception e ) { 1243 } 1245 return result; 1246 } 1247 1248 1256 public static BaseObject lookupObject(String ref, ServletContext servletContext, String contextPath) { 1257 return lookupObject(ref, servletContext, contextPath, null); 1258 } 1259 1260 1271 public static BaseObject lookupObject(String ref, ServletContext servletContext, String contextPath, HttpServletRequest request) { 1272 1273 if ( ref == null || ref.trim().length() == 0 ) { 1274 return null; 1275 } 1276 1277 if (request != null) { 1278 if (ref.toLowerCase().startsWith("http://") || ref.toLowerCase().startsWith("https://")) { 1279 if (isExternalURL(ref, request)) { 1280 return null; 1281 } 1282 } 1283 } 1284 1285 BaseObject obj = null; 1286 String uri = new String (ref); 1287 1288 if ( uri.startsWith("http") ) { 1290 String scheme = null; 1291 if ( uri.startsWith("https") ) { 1292 scheme = "https://"; 1293 } else { 1294 scheme = "http://"; 1295 } 1296 uri = uri.substring(scheme.length()); 1297 1298 int k = uri.indexOf("/"); 1300 if ( k != -1 ) { 1301 uri = uri.substring(k); 1302 } 1303 1304 } 1305 1306 if ( uri.startsWith(contextPath) && !contextPath.equals("/") ) { 1308 uri = uri.substring(contextPath.length()); 1309 } 1310 1311 if ( !uri.startsWith("/") ) { 1313 uri = "/" + uri; 1314 } 1315 1316 int k = uri.indexOf("?"); 1318 if ( k != -1 ) { 1319 uri = uri.substring(0, k); 1320 } 1321 1322 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 1323 1324 if ( uri.startsWith(Constants.RESOURCES_URI_PREFIX) ) { 1326 ContentResourceManager contentResourceManager = (ContentResourceManager) applicationContext.getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 1328 obj = contentResourceManager.findContentResourceByUri(uri); 1329 } else { 1330 1332 if (uri.startsWith(Constants.LOCALIZED_URI_PREFIX + "/") 1335 && !uri.startsWith(NewsModuleWebConstants.NEWS_ITEM_URI_PREFIX) 1336 && !uri.startsWith(TestimonialModuleWebConstants.TESTIMONIAL_URI_PREFIX)) { 1337 uri = uri.substring(Constants.LOCALIZED_URI_PREFIX.length()); 1338 } 1339 1340 k = uri.lastIndexOf("."); 1342 if ( k != -1 ) { 1343 uri = uri.substring(0, k); 1344 } 1345 1346 k = uri.lastIndexOf("."); 1348 if ( k != -1 && k == uri.length() - 3 ) { 1349 uri = uri.substring(0, k); 1350 } 1351 1352 PageManager pageManager = (PageManager) applicationContext.getBean(Constants.PAGE_MANAGER_BEAN); 1353 obj = pageManager.findPageByUri(uri); 1354 } 1355 1356 return obj; 1357 } 1358 1359 1369 public static Map mapObjectsToRefs(Set refs, ServletContext servletContext, String contextPath) { 1370 Map linkedObjects = new HashMap(); 1371 for ( Iterator i = refs.iterator(); i.hasNext(); ) { 1372 String ref = (String ) i.next(); 1373 BaseObject linkedObject = lookupObject(ref, servletContext, contextPath); 1374 if ( linkedObject != null ) { 1375 linkedObjects.put(ref, linkedObject); 1376 } 1377 } 1378 return linkedObjects; 1379 } 1380 1381 1391 public static boolean isUserInRole(Collection roles, boolean all, String property, HttpServletRequest request) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 1392 boolean isUserInRole = all; 1393 if ( roles != null && !roles.isEmpty() ) { 1394 Iterator rolesIterator = roles.iterator(); 1395 while ( rolesIterator.hasNext() ) { 1396 Object bean = rolesIterator.next(); 1397 if ( bean == null ) { 1398 continue; 1399 } 1400 String roleName = null; 1401 if ( bean instanceof String ) { 1402 roleName = (String ) bean; 1403 } else if ( property != null && property.trim().length() > 0 ) { 1404 roleName = String.valueOf(PropertyUtils.getProperty(bean, property)); 1405 } else { 1406 roleName = String.valueOf(bean); 1407 } 1408 boolean roleCheck = request.isUserInRole(roleName.trim()); 1409 if ( all ) { 1410 isUserInRole = isUserInRole && roleCheck; 1411 } else { 1412 isUserInRole = isUserInRole || roleCheck; 1413 } 1414 } 1415 } else { 1416 isUserInRole = true; 1418 } 1419 return isUserInRole; 1420 } 1421 1422 1431 public static boolean isUserInRole(Collection roles, boolean all, HttpServletRequest request) { 1432 boolean isUserInRole = false; 1433 try { 1434 isUserInRole = isUserInRole(roles, all, null, request); 1435 } catch ( Exception ex ) { 1436 } 1438 return isUserInRole; 1439 } 1440 1441 1448 public static String [] getAPRoleNames(String uri, HttpServletRequest request) { 1449 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, request.getSession().getServletContext()); 1450 ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig(uri); 1451 if ( mapping == null ) { 1452 return null; 1453 } else { 1454 return mapping.getRoleNames(); 1455 } 1456 } 1457 1458 1465 public static List getAPRoleNamesAsList(String uri, HttpServletRequest request) { 1466 String [] roles = getAPRoleNames(uri, request); 1467 if ( roles == null ) { 1468 return null; 1469 } else { 1470 return Arrays.asList(roles); 1471 } 1472 } 1473 1474 1482 public static boolean isAbsoluteURL(String url) { 1483 if ( url == null ) { 1485 return false; 1486 } 1487 1488 int colonPos = url.indexOf(":"); 1490 if ( colonPos != -1 && url.startsWith("//", colonPos + 1) ) { 1491 String scheme = url.substring(0, colonPos); 1494 return isValidScheme(scheme); 1495 } else { 1496 return false; 1497 } 1498 1499 } 1500 1501 1515 public static boolean isExternalURL(String url, HttpServletRequest request) { 1516 1517 String temp = new String (url); 1518 1519 String host = request.getServerName(); 1521 boolean urlIsAbsolute = isAbsoluteURL(url); 1522 1523 if ( urlIsAbsolute ) { 1524 1525 int schemeEnd = url.indexOf("//"); 1527 1528 temp = url.substring(schemeEnd + 2); 1530 1531 boolean isHttps; 1532 String scheme = url.substring(0, schemeEnd); 1533 isHttps = "https:".equalsIgnoreCase(scheme); 1534 if (!isHttps) { 1535 if (!"http:".equalsIgnoreCase(scheme)) { 1536 return true; 1538 } 1539 } 1540 1541 int firstSlash = temp.indexOf("/"); 1542 if (firstSlash == -1) { 1543 firstSlash = temp.length(); 1544 } 1545 int firstColon = temp.indexOf(":"); 1546 if (firstColon == -1) { 1547 firstColon = temp.length(); 1548 } 1549 int firstNonHostChar = firstColon < firstSlash ? firstColon : firstSlash; 1550 if ( !temp.substring(0, firstNonHostChar).equals(host) ) { 1551 return true; 1552 } 1553 ServletContext servletContext = request.getSession().getServletContext(); 1555 String portStr = isHttps 1556 ? servletContext.getInitParameter(SslUtil.HTTPS_PORT_PARAM) 1557 : servletContext.getInitParameter(SslUtil.HTTP_PORT_PARAM); 1558 int port = Integer.valueOf(portStr).intValue(); 1559 boolean standardPort = !isHttps ? (port == 80) : (port == 443); 1561 temp = temp.substring(host.length()); 1562 if ( temp.startsWith(":") ) { 1563 temp = temp.substring(1); 1564 if ( !temp.startsWith(portStr) ) { 1565 return true; 1566 } 1567 temp = temp.substring(portStr.length()); 1568 } else if ( !standardPort ) { 1569 return true; 1570 } 1571 1572 String contextPath = request.getContextPath(); 1574 if ( !temp.startsWith(contextPath) ) { 1575 return true; 1576 } else { 1577 return false; 1579 } 1580 } else { 1581 return !url.startsWith("/"); 1583 } 1584 } 1585 1586 1599 protected static boolean isValidSchemeChar(char c) { 1600 return Character.isLetterOrDigit(c) || c == '+' || c == '-' || c == '.'; 1601 } 1602 1603 1619 protected static boolean isValidScheme(String scheme) { 1620 1621 if ( scheme == null ) { 1623 return true; 1624 } 1625 1626 if ( Character.isLetter(scheme.charAt(0)) ) { 1627 for ( int i = 1; i < scheme.length(); i++ ) { 1629 if ( !isValidSchemeChar(scheme.charAt(i)) ) { 1630 return false; 1631 } 1632 } 1633 } else { 1634 return false; 1635 } 1636 1637 return true; 1638 } 1639 1640} 1641 | Popular Tags |