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;
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.matching.WildcardURIMatcher;
029import org.apache.cocoon.sitemap.PatternException;
030
031import org.ametys.cms.content.GetContentAction;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.site.Site;
035import org.ametys.web.repository.site.SiteManager;
036import org.ametys.web.repository.sitemap.Sitemap;
037
038/**
039 * Matcher for <code>Page</code>s.<br>
040 */
041public class PageMatcher extends WildcardURIMatcher implements Serviceable
042{
043    private static final Pattern __PAGE_PATH_PATTERN = Pattern.compile("^([^/]+)/([^/]+)/(.+)$");
044    private SiteManager _siteManager;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
050    }
051
052    @SuppressWarnings("unchecked")
053    @Override
054    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
055    {
056        Map result = super.match(pattern, objectModel, parameters);
057
058        if (result == null)
059        {
060            return null;
061        }
062        
063        String pagePath = (String) result.get(parameters.getParameter("useWildcard", "1"));
064        try
065        {
066            String localpagePath = null;
067            String siteName = null;
068            String sitemapLanguage = null;
069            Matcher matcher = __PAGE_PATH_PATTERN.matcher(pagePath);
070            
071            if (matcher.matches())
072            {
073                siteName = matcher.group(1);
074                sitemapLanguage = matcher.group(2);
075                localpagePath = matcher.group(3);
076            }
077            else
078            {
079                if (getLogger().isWarnEnabled())
080                {
081                    getLogger().warn("Invalid page path: " + pagePath);
082                }
083
084                // invalid path, consider it as not found
085                return null;
086            }
087            
088            Site site = _siteManager.getSite(siteName);
089            String skin = site.getSkinId();
090            
091            Sitemap sitemap = site.getSitemap(sitemapLanguage);
092            Page page = sitemap.getChild(localpagePath);
093            
094            Request request = ObjectModelHelper.getRequest(objectModel);
095            
096            // Seems to be like a hack: set the initial site name of the request 
097            String initialSiteName = (String) request.getAttribute("initialSiteName");
098            if (initialSiteName == null)
099            {
100                request.setAttribute("initialSiteName", siteName);
101            }
102            
103            request.setAttribute("site", siteName);
104            request.setAttribute("sitemapLanguage", sitemapLanguage);
105            request.setAttribute(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
106            request.setAttribute(Site.class.getName(), site);
107            request.setAttribute(Sitemap.class.getName(), sitemap);
108            request.setAttribute(Page.class.getName(), page);
109            request.setAttribute("pageId", page.getId());
110            request.setAttribute("skin", skin);
111            request.setAttribute("template", page.getTemplate());
112            request.setAttribute("document", localpagePath);
113            
114            result.put("site", siteName);
115            result.put("sitemapLanguage", sitemapLanguage);
116            result.put(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
117            result.put("document", localpagePath);
118            result.put("pageId", page.getId());
119            result.put("skin", skin);
120            result.put("template", page.getTemplate());
121        }
122        catch (UnknownAmetysObjectException e)
123        {
124            if (getLogger().isDebugEnabled())
125            {
126                getLogger().debug("No page for path '" + pagePath + "'", e);
127            }
128            
129            // No match
130            return null;
131        }
132
133        return result;
134    }
135}