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