Coverage report

  %line %branch
org.apache.jetspeed.portlets.layout.LayoutPortlet
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.portlets.layout;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.util.HashMap;
 21  
 import java.util.Locale;
 22  
 import java.util.Map;
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 import javax.portlet.ActionRequest;
 26  
 import javax.portlet.ActionResponse;
 27  
 import javax.portlet.PortletConfig;
 28  
 import javax.portlet.PortletException;
 29  
 import javax.portlet.PortletPreferences;
 30  
 import javax.portlet.RenderRequest;
 31  
 import javax.portlet.RenderResponse;
 32  
 
 33  
 import org.apache.commons.configuration.Configuration;
 34  
 import org.apache.commons.configuration.ConfigurationException;
 35  
 import org.apache.commons.configuration.PropertiesConfiguration;
 36  
 import org.apache.commons.logging.Log;
 37  
 import org.apache.commons.logging.LogFactory;
 38  
 import org.apache.jetspeed.CommonPortletServices;
 39  
 import org.apache.jetspeed.JetspeedActions;
 40  
 import org.apache.jetspeed.PortalReservedParameters;
 41  
 import org.apache.jetspeed.capabilities.CapabilityMap;
 42  
 import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
 43  
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 44  
 import org.apache.jetspeed.container.window.PortletWindowAccessor;
 45  
 import org.apache.jetspeed.layout.JetspeedPowerTool;
 46  
 import org.apache.jetspeed.locator.LocatorDescriptor;
 47  
 import org.apache.jetspeed.locator.TemplateDescriptor;
 48  
 import org.apache.jetspeed.locator.TemplateLocator;
 49  
 import org.apache.jetspeed.locator.TemplateLocatorException;
 50  
 import org.apache.jetspeed.om.page.Fragment;
 51  
 import org.apache.jetspeed.om.page.Page;
 52  
 import org.apache.jetspeed.page.PageManager;
 53  
 import org.apache.jetspeed.request.RequestContext;
 54  
 import org.apache.jetspeed.velocity.JetspeedPowerToolFactory;
 55  
 import org.apache.pluto.om.window.PortletWindow;
 56  
 
 57  
 /**
 58  
  */
 59  0
 public class LayoutPortlet extends org.apache.portals.bridges.common.GenericServletPortlet
 60  
 {
 61  
     public static final String GENERIC_TEMPLATE_TYPE = "generic";
 62  
 
 63  
     public static final String FRAGMENT_PROCESSING_ERROR_PREFIX = "fragment.processing.error.";
 64  
 
 65  
     public static final String FRAGMENT_ATTR = "fragment";
 66  
 
 67  
     public static final String LAYOUT_ATTR = "layout";
 68  
 
 69  
     public static final String HIDDEN = "hidden";
 70  
 
 71  
     public static final String LAYOUT_TEMPLATE_TYPE = "layout";
 72  
 
 73  
     public static final String DECORATOR_TYPE = "decorator";
 74  
     
 75  
     public static final String PARAM_SOLO_PAGE = "SoloPage";
 76  
     
 77  
     
 78  
     /** Commons logging */
 79  0
     protected final static Log log = LogFactory.getLog(LayoutPortlet.class);
 80  
     
 81  
     protected PortletRegistry registry;
 82  
     protected PageManager pageManager;
 83  
     protected JetspeedPowerToolFactory jptFactory;
 84  
     protected TemplateLocator templateLocator;
 85  
     protected PortletEntityAccessComponent entityAccess;
 86  
     protected PortletWindowAccessor windowAccess;
 87  
     protected TemplateLocator decorationLocator;
 88  
     
 89  0
     private Map layoutTemplatesCache = new HashMap();
 90  
     public static final String DEFAULT_TEMPLATE_EXT = ".vm";
 91  
     public static final String TEMPLATE_EXTENSION_KEY = "template.extension";
 92  
     public static final String DEFAULT_TEMPLATE_TYPE = "velocity";
 93  
     public static final String TEMPLATE_TYPE_KEY = "template.type";
 94  
     
 95  
     public void init( PortletConfig config ) throws PortletException
 96  
     {
 97  0
         super.init(config);
 98  
         
 99  0
         registry = (PortletRegistry)getPortletContext().getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
 100  0
         if (null == registry)
 101  
         {
 102  0
             throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
 103  
         }        
 104  0
         pageManager = (PageManager)getPortletContext().getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
 105  0
         if (null == pageManager)
 106  
         {
 107  0
             throw new PortletException("Failed to find the Page Manager on portlet initialization");
 108  
         }        
 109  0
         jptFactory = (JetspeedPowerToolFactory)getPortletContext().getAttribute(CommonPortletServices.CPS_JETSPEED_POWERTOOL_FACTORY);
 110  0
         if (null == jptFactory)
 111  
         {
 112  0
             throw new PortletException("Failed to find the JPT Factory on portlet initialization");
 113  
         }        
 114  
         
 115  0
         entityAccess = (PortletEntityAccessComponent) getPortletContext().getAttribute(CommonPortletServices.CPS_ENTITY_ACCESS_COMPONENT);
 116  0
         if (null == entityAccess)
 117  
         {
 118  0
             throw new PortletException("Failed to find the Entity Access on portlet initialization");
 119  
         }        
 120  
         
 121  0
         windowAccess = (PortletWindowAccessor) getPortletContext().getAttribute(CommonPortletServices.CPS_WINDOW_ACCESS_COMPONENT);
 122  0
         if (null == windowAccess)
 123  
         {
 124  0
             throw new PortletException("Failed to find the Window Access on portlet initialization");
 125  
         }        
 126  
         
 127  0
         templateLocator = (TemplateLocator) getPortletContext().getAttribute("TemplateLocator");
 128  0
         decorationLocator = (TemplateLocator) getPortletContext().getAttribute("DecorationLocator");
 129  0
     }
 130  
 
 131  
     public void doHelp( RenderRequest request, RenderResponse response ) throws PortletException, IOException
 132  
     {
 133  0
         RequestContext context = getRequestContext(request);
 134  0
         response.setContentType(context.getMimeType());        
 135  0
         JetspeedPowerTool jpt = getJetspeedPowerTool(request);
 136  
 
 137  0
         String absHelpPage = "";
 138  
 
 139  
         // request.setAttribute(PortalReservedParameters.PAGE_ATTRIBUTE, getPage(request));
 140  
         // request.setAttribute("fragment", getFragment(request, false));        
 141  
 
 142  
         try
 143  
         {
 144  0
             String helpPage = (String)request.getPortletSession().getAttribute(PortalReservedParameters.PAGE_LAYOUT_HELP);                       
 145  0
             if (helpPage == null)
 146  
             {
 147  0
                 PortletPreferences prefs = request.getPreferences();
 148  0
                 helpPage = prefs.getValue(PARAM_HELP_PAGE, null);
 149  0
                 if (helpPage == null)
 150  
                 {
 151  0
                     helpPage = this.getInitParameter(PARAM_HELP_PAGE);
 152  0
                     if (helpPage == null)
 153  0
                         helpPage = "columns";
 154  
                 }
 155  0
                 request.getPortletSession().setAttribute(PortalReservedParameters.PAGE_LAYOUT_HELP, helpPage);
 156  
             }
 157  
 
 158  0
             String templateKey = helpPage + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE  + "-help";
 159  0
             CachedTemplate ct = (CachedTemplate)layoutTemplatesCache.get(templateKey);
 160  0
             if (ct == null)
 161  
             {
 162  0
                 TemplateDescriptor template = null;
 163  0
                 Configuration props = getConfiguration(request, helpPage);
 164  0
                 String ext = (String) props.getString(TEMPLATE_EXTENSION_KEY);
 165  0
                 String path = helpPage + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE + "-help" + ext;                               
 166  0
                 template = jpt.getTemplate(path, JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE);
 167  0
                 if (template == null)
 168  
                 {
 169  0
                     String msg = "*** FAILED getTemplate:" + path;
 170  0
                     throw new TemplateLocatorException(msg);
 171  
                 }
 172  
                 else
 173  
                 {
 174  0
                     synchronized(layoutTemplatesCache)
 175  
                     {
 176  0
                         ct = new CachedTemplate(templateKey, template, props);
 177  0
                         layoutTemplatesCache.put(templateKey, ct);
 178  0
                     }                
 179  
                 }
 180  
             }
 181  
                 
 182  0
             absHelpPage = ct.getTemplate().getAppRelativePath();            
 183  0
             log.debug("Path to help page for LayoutPortlet " + absHelpPage);
 184  0
             request.setAttribute(PARAM_VIEW_PAGE, absHelpPage);
 185  
         }
 186  0
         catch (TemplateLocatorException e)
 187  
         {
 188  0
             throw new PortletException("Unable to locate view page " + absHelpPage, e);
 189  0
         }
 190  0
         super.doView(request, response);
 191  
 
 192  0
     }
 193  
     
 194  
     /**
 195  
      * 
 196  
      */
 197  
     public void doView( RenderRequest request, RenderResponse response ) throws PortletException, IOException
 198  
     {
 199  0
         RequestContext context = getRequestContext(request);
 200  0
         response.setContentType(context.getMimeType());        
 201  0
         PortletWindow window = context.getPortalURL().getNavigationalState().getMaximizedWindow();
 202  0
         boolean maximized = (window != null);
 203  0
         boolean solo = false;
 204  
 
 205  0
         if (maximized)
 206  
         {
 207  0
             request.setAttribute("layout", getMaximizedLayout(request));
 208  0
             solo = JetspeedActions.SOLO_STATE.equals(context.getPortalURL().getNavigationalState().getMappedState(window));
 209  0
             if ( solo )
 210  
             {
 211  0
                 maximized = false;
 212  
             }
 213  
         }
 214  
         else
 215  
         {
 216  0
             request.setAttribute("layout", getFragment(request, false));
 217  
         }
 218  0
         String viewPage = null;
 219  0
         String absViewPage = null;
 220  
         try
 221  
         {
 222  0
             JetspeedPowerTool jpt = getJetspeedPowerTool(request);
 223  0
             if (maximized)
 224  
             {
 225  0
                 viewPage = (String)request.getPortletSession().getAttribute(PortalReservedParameters.PAGE_LAYOUT_MAX);                       
 226  0
                 if (viewPage == null)
 227  
                 {
 228  0
                     PortletPreferences prefs = request.getPreferences();
 229  0
                     viewPage = prefs.getValue(PARAM_MAX_PAGE, null);
 230  0
                     if (viewPage == null)
 231  
                     {
 232  0
                         viewPage = this.getInitParameter(PARAM_MAX_PAGE);
 233  0
                         if (viewPage == null)
 234  0
                             viewPage = "maximized";
 235  
                     }
 236  0
                     request.getPortletSession().setAttribute(PortalReservedParameters.PAGE_LAYOUT_MAX, viewPage);
 237  0
                 }
 238  
             }
 239  0
             else if (solo)
 240  
             {
 241  0
                 viewPage = (String)request.getPortletSession().getAttribute(PortalReservedParameters.PAGE_LAYOUT_SOLO);                       
 242  0
                 if (viewPage == null)
 243  
                 {
 244  0
                     PortletPreferences prefs = request.getPreferences();                
 245  0
                     viewPage = prefs.getValue(PARAM_SOLO_PAGE, null);
 246  0
                     if (viewPage == null)
 247  
                     {
 248  0
                         viewPage = this.getInitParameter(PARAM_SOLO_PAGE);
 249  0
                         if (viewPage == null)
 250  
                         {
 251  0
                             viewPage = "solo";
 252  
                         }
 253  
                     }
 254  0
                     request.getPortletSession().setAttribute(PortalReservedParameters.PAGE_LAYOUT_SOLO, viewPage);                    
 255  0
                 }
 256  
             }
 257  
             else
 258  
             {
 259  0
                 viewPage = (String)request.getPortletSession().getAttribute(PortalReservedParameters.PAGE_LAYOUT_VIEW);                       
 260  0
                 if (viewPage == null)
 261  
                 {
 262  0
                     PortletPreferences prefs = request.getPreferences();                                
 263  0
                     viewPage = prefs.getValue(PARAM_VIEW_PAGE, null);
 264  0
                     if (viewPage == null)
 265  
                     {
 266  0
                         viewPage = this.getInitParameter(PARAM_VIEW_PAGE);
 267  0
                         if (viewPage == null)
 268  0
                             viewPage = "columns";
 269  
                     }
 270  0
                     request.getPortletSession().setAttribute(PortalReservedParameters.PAGE_LAYOUT_VIEW, viewPage);                                        
 271  
                 }
 272  
             }
 273  
             
 274  0
             String templateKey = viewPage + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE;
 275  0
             CachedTemplate ct = (CachedTemplate)layoutTemplatesCache.get(templateKey);
 276  0
             if (ct == null)
 277  
             {
 278  0
                 TemplateDescriptor template = null;
 279  0
                 Configuration props = getConfiguration(request, viewPage);
 280  0
                 String ext = (String) props.getString(TEMPLATE_EXTENSION_KEY);
 281  0
                 String path = viewPage + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE + ext;
 282  
                 
 283  0
                 template = jpt.getTemplate(path, JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE);
 284  0
                 if (template == null)
 285  
                 {
 286  0
                     String msg = "*** FAILED getTemplate:" + path;
 287  0
                     throw new TemplateLocatorException(msg);
 288  
                 }
 289  
                 else
 290  
                 {
 291  0
                     synchronized(layoutTemplatesCache)
 292  
                     {
 293  0
                         ct = new CachedTemplate(templateKey, template, props);
 294  0
                         layoutTemplatesCache.put(templateKey, ct);
 295  0
                     }
 296  
                 
 297  
                 }
 298  
             }
 299  0
             absViewPage = ct.getTemplate().getAppRelativePath();
 300  0
             log.debug("Path to view page for LayoutPortlet " + absViewPage);
 301  0
             request.setAttribute(PARAM_VIEW_PAGE, absViewPage);
 302  
         }
 303  0
         catch (TemplateLocatorException e)
 304  
         {
 305  0
             throw new PortletException("Unable to locate view page " + absViewPage, e);
 306  0
         }
 307  0
         super.doView(request, response);
 308  
 
 309  0
         request.removeAttribute(PortalReservedParameters.PAGE_ATTRIBUTE);
 310  0
         request.removeAttribute("fragment");
 311  0
         request.removeAttribute("layout");
 312  0
         request.removeAttribute("dispatcher");
 313  0
     }
 314  
     
 315  
     public void processAction(ActionRequest request, ActionResponse response)
 316  
     throws PortletException, IOException
 317  
     {
 318  0
         String page = request.getParameter("page");
 319  0
         String deleteFragmentId = request.getParameter("deleteId");
 320  0
         String portlets = request.getParameter("portlets");
 321  0
         if (deleteFragmentId != null && deleteFragmentId.length() > 0)
 322  
         {
 323  0
             removeFragment(page, deleteFragmentId);
 324  
         }
 325  0
         else if (portlets != null && portlets.length() > 0)
 326  
         {
 327  0
             int count = 0;
 328  0
             StringTokenizer tokenizer = new StringTokenizer(portlets, ",");            
 329  0
             while (tokenizer.hasMoreTokens())
 330  
             {
 331  0
                 String portlet = tokenizer.nextToken();
 332  
                 try
 333  
                 {
 334  0
                     if (portlet.startsWith("box_"))
 335  
                     {
 336  0
                         portlet = portlet.substring("box_".length());                        
 337  0
                         addPortletToPage(page, portlet);
 338  0
                         count++;
 339  
                     }
 340  
                 }
 341  0
                 catch (Exception e)
 342  
                 {
 343  0
                     log.error("failed to add portlet to page: " + portlet);
 344  0
                 }
 345  0
             }
 346  
             
 347  
         }       
 348  0
     }
 349  
 
 350  
     protected void removeFragment(String pageId, String fragmentId)
 351  
     {
 352  0
         Page page = null;
 353  
         try
 354  
         {
 355  0
             page = pageManager.getPage(pageId);
 356  
             
 357  
         }
 358  0
         catch (Exception e)
 359  
         {
 360  0
             log.error("failed to remove portlet " + fragmentId + " from page: " + pageId, e);
 361  0
         }
 362  0
         removeFragment(page,page.getRootFragment(), fragmentId);            
 363  0
     }
 364  
     
 365  
     protected void removeFragment(Page page, Fragment root, String fragmentId)
 366  
     {
 367  
         try
 368  
         {
 369  0
             Fragment f = page.getFragmentById(fragmentId);
 370  0
             if ( f == null )
 371  
             {
 372  
                 // ignore no longer existing fragment error
 373  0
                 return;
 374  
             }
 375  0
             root.getFragments().remove(f);
 376  0
             pageManager.updatePage(page);
 377  
         }
 378  0
         catch (Exception e)
 379  
         {
 380  0
             log.error("failed to remove portlet " + fragmentId + " from page: " + page, e);
 381  0
         }
 382  0
     }
 383  
     
 384  
     protected void addPortletToPage(String pageId, String portletId)
 385  
     {
 386  0
         Page page = null;
 387  
         try
 388  
         {
 389  0
             page = pageManager.getContentPage(pageId);
 390  
         }
 391  0
         catch (Exception e)
 392  
         {
 393  0
             log.error("failed to add portlet " + portletId + " to page: " + pageId, e);
 394  0
         }
 395  0
         addPortletToPage(page, page.getRootFragment(), portletId);
 396  0
     }
 397  
     
 398  
     protected void addPortletToPage(Page page, Fragment root, String portletId)
 399  
     {
 400  
         try
 401  
         {
 402  0
             Fragment fragment = pageManager.newFragment();
 403  0
             fragment.setType(Fragment.PORTLET);
 404  0
             fragment.setName(portletId);
 405  
             
 406  0
             root.getFragments().add(fragment);
 407  0
             pageManager.updatePage(page);            
 408  
         }
 409  0
         catch (Exception e)
 410  
         {
 411  0
             log.error("failed to add portlet " + portletId + " to page: " + page, e);
 412  0
         }
 413  0
     }
 414  
     
 415  
     /**
 416  
      * <p>
 417  
      * initJetspeedPowerTool
 418  
      * </p>
 419  
      * 
 420  
      * @param request
 421  
      * @param response
 422  
      * @return
 423  
      * @throws PortletException
 424  
      */
 425  
     protected JetspeedPowerTool getJetspeedPowerTool( RenderRequest request ) throws PortletException
 426  
     {
 427  0
         JetspeedPowerTool tool = (JetspeedPowerTool) request.getAttribute(PortalReservedParameters.JETSPEED_POWER_TOOL_REQ_ATTRIBUTE);
 428  0
         RequestContext requestContext = (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
 429  
 
 430  0
         if (tool == null)
 431  
         {
 432  
 
 433  
             try
 434  
             {
 435  0
                 if (requestContext == null)
 436  
                 {
 437  0
                     throw new IllegalStateException(
 438  
                             "LayoutPortlet unable to handle request because there is no RequestContext in "
 439  
                                     + "the HttpServletRequest.");
 440  
                 }
 441  
 
 442  0
                 tool = this.jptFactory.getJetspeedPowerTool(requestContext);
 443  0
                 request.setAttribute(PortalReservedParameters.JETSPEED_POWER_TOOL_REQ_ATTRIBUTE, tool);
 444  
             }
 445  
 
 446  0
             catch (Exception e1)
 447  
             {
 448  0
                 throw new PortletException("Unable to init JetspeedPowerTool: " + e1.toString(), e1);
 449  0
             }
 450  
         }
 451  
         
 452  0
         return tool;
 453  
     }
 454  
     
 455  
     /**
 456  
      * 
 457  
      * @param request
 458  
      * @param maximized
 459  
      * @return
 460  
      */
 461  
     protected Fragment getFragment( RenderRequest request, boolean maximized )
 462  
     {
 463  0
         String attribute = (maximized)
 464  
                 ? PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE
 465  
                 : PortalReservedParameters.FRAGMENT_ATTRIBUTE;
 466  0
         return (Fragment) request.getAttribute(attribute);       
 467  
     }
 468  
    
 469  
     /**
 470  
      * 
 471  
      * @param request
 472  
      * @return
 473  
      */
 474  
     protected Fragment getMaximizedLayout( RenderRequest request )
 475  
     {
 476  0
         return (Fragment) request.getAttribute(PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE);
 477  
     }
 478  
     
 479  
     /**
 480  
      * 
 481  
      * @param request
 482  
      * @return
 483  
      */
 484  
     protected RequestContext getRequestContext( RenderRequest request )
 485  
     {
 486  0
         RequestContext requestContext = (RequestContext) request
 487  
                 .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
 488  0
         if (requestContext != null)
 489  
         {
 490  0
             return requestContext;
 491  
         }
 492  
         else
 493  
         {
 494  0
             throw new IllegalStateException(
 495  
                     "getRequestContext() failed as it appears that no RenderRequest is available within the RenderRequest");
 496  
         }
 497  
     }
 498  
 
 499  
     /**
 500  
      * <p>
 501  
      * doEdit
 502  
      * </p>
 503  
      * 
 504  
      * @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest,
 505  
      *          javax.portlet.RenderResponse)
 506  
      * @param request
 507  
      * @param response
 508  
      * @throws PortletException
 509  
      * @throws IOException
 510  
      */
 511  
     public void doEdit( RenderRequest request, RenderResponse response ) throws PortletException, IOException
 512  
     {
 513  0
         doView(request, response);
 514  0
     }
 515  
     
 516  
     /**
 517  
      * 
 518  
      * @param request
 519  
      * @return
 520  
      * @throws TemplateLocatorException
 521  
      */
 522  
     protected LocatorDescriptor getTemplateLocatorDescriptor(RenderRequest request) throws TemplateLocatorException
 523  
     {
 524  0
         RequestContext requestContext = getRequestContext(request);
 525  0
         CapabilityMap capabilityMap = requestContext.getCapabilityMap();
 526  0
         Locale locale = requestContext.getLocale();
 527  
 
 528  0
         LocatorDescriptor templateLocatorDescriptor = templateLocator.createLocatorDescriptor(null);
 529  0
         templateLocatorDescriptor.setMediaType(capabilityMap.getPreferredMediaType().getName());
 530  0
         templateLocatorDescriptor.setCountry(locale.getCountry());
 531  0
         templateLocatorDescriptor.setLanguage(locale.getLanguage());
 532  0
         return templateLocatorDescriptor;     
 533  
     }
 534  
     
 535  
     
 536  
     /**
 537  
      * 
 538  
      * @param request
 539  
      * @return
 540  
      * @throws TemplateLocatorException
 541  
      */
 542  
     protected LocatorDescriptor getDecoratorLocatorDescriptor(RenderRequest request) throws TemplateLocatorException
 543  
     {
 544  0
         RequestContext requestContext = getRequestContext(request);
 545  0
         CapabilityMap capabilityMap = requestContext.getCapabilityMap();
 546  0
         Locale locale = requestContext.getLocale();
 547  
   
 548  0
         LocatorDescriptor decorationLocatorDescriptor = decorationLocator.createLocatorDescriptor(null);
 549  0
         decorationLocatorDescriptor.setMediaType(capabilityMap.getPreferredMediaType().getName());
 550  0
         decorationLocatorDescriptor.setCountry(locale.getCountry());
 551  0
         decorationLocatorDescriptor.setLanguage(locale.getLanguage());
 552  
         
 553  0
         return decorationLocatorDescriptor;
 554  
     }
 555  
     
 556  
     /**
 557  
      * 
 558  
      * @param request
 559  
      * @param fragment
 560  
      * @param page
 561  
      * @return
 562  
      * @throws TemplateLocatorException
 563  
      * @throws ConfigurationException
 564  
      */
 565  
     public String decorateAndInclude(RenderRequest request, Fragment fragment, Page page) throws TemplateLocatorException, ConfigurationException
 566  
     {   
 567  0
         String fragmentType = fragment.getType();
 568  0
         String decorator = fragment.getDecorator();
 569  0
         LocatorDescriptor decorationLocatorDescriptor = getDecoratorLocatorDescriptor(request);
 570  0
         if (decorator == null)
 571  
         {
 572  0
             decorator = page.getEffectiveDefaultDecorator(fragmentType);
 573  
         }
 574  
 
 575  
         // get fragment properties for fragmentType or generic
 576  0
         TemplateDescriptor propsTemp = getTemplate(decorator + "/" + DECORATOR_TYPE + ".properties", fragmentType,
 577  
                 decorationLocator, decorationLocatorDescriptor);
 578  0
         if (propsTemp == null)
 579  
         {
 580  0
             fragmentType = GENERIC_TEMPLATE_TYPE;
 581  0
             propsTemp = getTemplate(decorator + "/" + DECORATOR_TYPE + ".properties", fragmentType, decorationLocator,
 582  
                     decorationLocatorDescriptor);
 583  
         }
 584  
 
 585  
         // get decorator template
 586  0
         Configuration decoConf = new PropertiesConfiguration(propsTemp.getAbsolutePath());
 587  0
         String ext = decoConf.getString("template.extension");
 588  0
         String decoratorPath = decorator + "/" + DECORATOR_TYPE + ext;
 589  0
         TemplateDescriptor template = null;
 590  
         try
 591  
         {
 592  0
             template = getDecoration(request, decoratorPath, fragmentType);
 593  
         }
 594  0
         catch (TemplateLocatorException e)
 595  
         {
 596  0
             String parent = decoConf.getString("extends");
 597  0
             if (parent != null)
 598  
             {
 599  0
                 template = getDecoration(request, parent + "/" + DECORATOR_TYPE + ext, fragmentType);
 600  
             }
 601  0
         }
 602  
 
 603  0
         return  template.getAppRelativePath();
 604  
     }
 605  
     
 606  
     /**
 607  
      * 
 608  
      * @param request
 609  
      * @param path
 610  
      * @param templateType
 611  
      * @return
 612  
      * @throws TemplateLocatorException
 613  
      */
 614  
     protected TemplateDescriptor getDecoration( RenderRequest request, String path, String templateType ) throws TemplateLocatorException
 615  
     {        
 616  0
         return getTemplate(path, templateType, decorationLocator, getDecoratorLocatorDescriptor(request));
 617  
     }
 618  
     
 619  
     /**
 620  
      * 
 621  
      * @param path
 622  
      * @param templateType
 623  
      * @param locator
 624  
      * @param descriptor
 625  
      * @return
 626  
      * @throws TemplateLocatorException
 627  
      */
 628  
     protected TemplateDescriptor getTemplate( String path, String templateType, TemplateLocator locator,
 629  
             LocatorDescriptor descriptor ) throws TemplateLocatorException
 630  
     {
 631  
         
 632  0
         if (templateType == null)
 633  
         {
 634  0
             templateType = GENERIC_TEMPLATE_TYPE;
 635  
         }
 636  
         try
 637  
         {
 638  
 
 639  0
             descriptor.setName(path);
 640  0
             descriptor.setType(templateType);
 641  
 
 642  0
             TemplateDescriptor template = locator.locateTemplate(descriptor);
 643  0
             return template;
 644  
         }
 645  0
         catch (TemplateLocatorException e)
 646  
         {
 647  0
             log.error("Unable to locate template: " + path, e);
 648  0
             throw e;
 649  
         }
 650  
     }
 651  
 
 652  
     /**
 653  
      * Gets the configuration (layout.properties) object for the decoration.
 654  
      * @param name Name of the Decoration.
 655  
      * @return <code>java.util.Properties</code> representing the configuration
 656  
      * object.
 657  
      */
 658  
     protected Configuration getConfiguration( RenderRequest request, String name )
 659  
     {
 660  0
         Configuration props = null;
 661  0
         JetspeedPowerTool jpt = null;
 662  0
         String templatePropertiesPath = null;
 663  0
         String key = name;
 664  
         try
 665  
         {
 666  0
             jpt = getJetspeedPowerTool(request);
 667  0
             templatePropertiesPath = jpt.getTemplate(name + "/" + JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE + ".properties",
 668  
                     JetspeedPowerTool.LAYOUT_TEMPLATE_TYPE).getAbsolutePath();
 669  
         } 
 670  0
         catch (PortletException e)
 671  
         {
 672  0
             log.warn("Could not acquire JetspeedPowerTool from request",e);
 673  
         }
 674  0
         catch (TemplateLocatorException e)
 675  
         {
 676  0
             log.warn("Could not find templatePorpertiesPath",e);
 677  
         }
 678  0
         catch (Exception e)
 679  
         {
 680  0
             log.warn("Could not determine Layout template properties file",e);
 681  0
         }
 682  
         // if no path then set name to "default"
 683  0
         if (null == templatePropertiesPath)
 684  
         {
 685  0
             key = "default";
 686  
         }
 687  
         else
 688  
         {
 689  0
             key = templatePropertiesPath;
 690  
         }
 691  0
         if (log.isDebugEnabled())
 692  
         {
 693  0
             log.debug(
 694  
                     "Template descriptor path:<" + templatePropertiesPath + ">"
 695  
             );
 696  
         }
 697  
 
 698  
         // load Decoration.CONFIG_FILE_NAME (layout.properties)
 699  
         try
 700  
         {
 701  0
             props = new PropertiesConfiguration(templatePropertiesPath);
 702  0
             if (log.isDebugEnabled())
 703  0
                 log.debug("Successfully read in: <" + templatePropertiesPath + "> ");
 704  
         } 
 705  0
         catch (Exception e)
 706  
         {
 707  0
             props = new PropertiesConfiguration();
 708  0
             log.warn( "Could not locate the " + templatePropertiesPath + " file for layout template \"" + name + "\".  This layout template may not exist.",e );
 709  0
             props.setProperty( "id", name );
 710  0
             props.setProperty( TEMPLATE_TYPE_KEY, DEFAULT_TEMPLATE_TYPE );
 711  0
             props.setProperty( TEMPLATE_EXTENSION_KEY, DEFAULT_TEMPLATE_EXT);
 712  
         }
 713  
         finally
 714  
         {
 715  0
             String templateIdPropVal = (String) props.getProperty( "id" );
 716  0
             String templateNamePropVal = (String) props.getProperty( TEMPLATE_TYPE_KEY );
 717  0
             String templateExtPropVal = (String) props.getProperty(TEMPLATE_EXTENSION_KEY);
 718  
             
 719  0
             if ( templateIdPropVal == null )
 720  
             {
 721  0
                 templateIdPropVal = name;
 722  0
                 props.setProperty( "id", templateIdPropVal );
 723  
             }
 724  
             
 725  0
             if ( templateNamePropVal == null )
 726  
             {
 727  0
                 props.setProperty( TEMPLATE_TYPE_KEY, DEFAULT_TEMPLATE_TYPE );
 728  
             }
 729  0
             if ( templateExtPropVal == null )
 730  
             {
 731  0
                 props.setProperty( TEMPLATE_EXTENSION_KEY, DEFAULT_TEMPLATE_EXT );
 732  
             }
 733  0
         }
 734  
 
 735  0
         if (log.isDebugEnabled())
 736  
         {
 737  0
             log.debug("Template layout.properties extension is:<" + props.getString(TEMPLATE_EXTENSION_KEY));
 738  
         }
 739  0
         return props;
 740  
     }
 741  
 
 742  0
     class CachedTemplate
 743  
     {
 744  
         private String key;
 745  
         private TemplateDescriptor template;
 746  
         private Configuration config;
 747  
         
 748  
         public CachedTemplate(String key, TemplateDescriptor template, Configuration config)
 749  
         {
 750  
             this.key = key;
 751  
             this.template = template;
 752  
             this.config = config;
 753  
         }
 754  
 
 755  
         
 756  
         public Configuration getConfig()
 757  
         {
 758  
             return config;
 759  
         }
 760  
 
 761  
         
 762  
         public String getKey()
 763  
         {
 764  
             return key;
 765  
         }
 766  
 
 767  
         
 768  
         public TemplateDescriptor getTemplate()
 769  
         {
 770  
             return template;
 771  
         }
 772  
     }
 773  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.