001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.repository;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.matching.WildcardURIMatcher;
027import org.apache.cocoon.sitemap.PatternException;
028
029import org.ametys.cms.content.GetContentAction;
030import org.ametys.web.WebConstants;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033import org.ametys.web.repository.sitemap.Sitemap;
034
035/**
036 * Matcher for <code>Page</code>s.<br>
037 */
038public class SitemapMatcher extends WildcardURIMatcher implements Serviceable
039{
040    private SiteManager _siteManager;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
046    }
047    
048    @SuppressWarnings("unchecked")
049    @Override
050    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
051    {
052        Map result = super.match(pattern, objectModel, parameters);
053
054        if (result == null)
055        {
056            return null;
057        }
058        
059        String siteName = (String) result.get("1");
060        String sitemapLanguage = (String) result.get("2");
061        
062        Site site = _siteManager.getSite(siteName);
063        String skin = site.getSkinId();
064        
065        Sitemap sitemap = site.getSitemap(sitemapLanguage);
066        
067        Request request = ObjectModelHelper.getRequest(objectModel);
068        
069        // Seems to be like a hack: set the initial site name of the request 
070        String initialSiteName = (String) request.getAttribute("initialSiteName");
071        if (initialSiteName == null)
072        {
073            request.setAttribute("initialSiteName", siteName);
074        }
075        
076        request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, siteName);
077        request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME, sitemapLanguage);
078        request.setAttribute(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
079        request.setAttribute(WebConstants.REQUEST_ATTR_SITE, site);
080        request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, skin);
081        request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP, sitemap);
082        request.setAttribute(WebConstants.REQUEST_ATTR_TEMPLATE_ID, sitemap.getTemplate());
083        
084        result.put("site", siteName);
085        result.put("sitemapLanguage", sitemapLanguage);
086        result.put(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
087        result.put("skin", skin);
088        result.put("template", sitemap.getTemplate());
089
090        return result;
091    }
092}