Guest
Public paste!

Michiel Trimpe

By: a guest | Aug 25th, 2008 | Syntax: Java | Size: 2.30 KB | Hits: 78 | Expires: Never
Copy text to clipboard
  1.         // Get the CMS site through the request context
  2.         private CMSSite getSite(RequestContext context) {
  3.                 CMSContextResolver resolver = (CMSContextResolver) Jetspeed.getComponentManager().getComponent(
  4.                                 "CMSContextResolver");
  5.  
  6.                 CMSPortalURL url = new JetspeedCMSPortalURL(context.getRequest(), context.getResponse(), context.getPath(),
  7.                                 context.getCharacterEncoding(), (Boolean) forceGetField(resolver, "mapServletPathToSitename"),
  8.                                 (Boolean) forceGetField(resolver, "mapServletPathToSitemapURL"), (Boolean) forceGetField(resolver,
  9.                                                 "useDefaultSite"));
  10.  
  11.                 return getSite(url);
  12.         }
  13.        
  14.         public CMSSite getSite(CMSPortalURL portalURL) {
  15.                 try {
  16.                         portalRegistration = (HippoPortalRegistration) Jetspeed.getComponentManager().getComponent(
  17.                                         HippoPortalRegistration.class);
  18.  
  19.                         CMSApplicationRegistry registry = (CMSApplicationRegistry) collapseProxy(portalRegistration
  20.                                         .getCMSApplicationRegistry());
  21.                         Map serverDomainMap = (Map) forceGetField(registry, "serverDomainMap");
  22.  
  23.                         CMSDomain host = portalURL.getHost();
  24.                         String hostName = host.getName();
  25.                         int hostPort = host.getPort();
  26.                         String fullDomainName = hostPort == 80 ? hostName : hostName + ":" + hostPort;
  27.  
  28.                         Object domain = serverDomainMap.get(fullDomainName);
  29.                         if (domain != null) {
  30.                                 Class domainClass = domain.getClass();
  31.                                 String siteName = portalURL.getSiteName();
  32.                                 if (siteName == null) {
  33.                                         siteName = (String) forceGetField(domain, "defaultSite");
  34.                                 }
  35.                                 return (CMSSite) domainClass.getMethod("getSite", String.class).invoke(domain, siteName);
  36.                         } else {
  37.                                 return null;
  38.                         }
  39.                 } catch (IllegalArgumentException e) {
  40.                 } catch (IllegalAccessException e) {
  41.                 } catch (InvocationTargetException e) {
  42.                 } catch (SecurityException e) {
  43.                 } catch (NoSuchMethodException e) {
  44.                 }
  45.                 return null;
  46.         }
  47.  
  48.         private Object forceGetField(Object object, String fieldName) {
  49.                 try {
  50.                         Field field = object.getClass().getDeclaredField(fieldName);
  51.                         field.setAccessible(true);
  52.                         return field.get(object);
  53.                 } catch (IllegalArgumentException e) {
  54.                 } catch (IllegalAccessException e) {
  55.                 } catch (SecurityException e) {
  56.                 } catch (NoSuchFieldException e) {
  57.                 }
  58.                 return null;
  59.         }
  60.  
  61.         private Object collapseProxy(Object object) {
  62.                 return forceGetField(Proxy.getInvocationHandler(object), "registration");
  63.         }