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.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * Matcher for wrapped url
035 */
036public class WrappedMatcher extends WildcardURIMatcher implements Serviceable
037{
038    /** The CMS sites manager */
039    protected SiteManager _siteManager;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
045    }
046    
047    @Override
048    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
049    {
050        Map<String, Object> result = super.match(pattern, objectModel, parameters);
051
052        if (result == null)
053        {
054            return null;
055        }
056        
057        String siteName = (String) result.get("1");
058        Site site = _siteManager.getSite(siteName);
059        String skinId = site.getSkinId();
060
061        String sitemapLanguage = (String) result.get("2");
062        String templateName = (String) result.get("4");
063        
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        
066        request.setAttribute("site", siteName);
067        request.setAttribute("sitemapLanguage", sitemapLanguage);
068        request.setAttribute(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
069        request.setAttribute("skin", skinId);
070        request.setAttribute("template", templateName);
071        
072        result.put("site", siteName);
073        result.put("sitemapLanguage", sitemapLanguage);
074        result.put(GetContentAction.RESULT_RENDERINGLANGUAGE, sitemapLanguage);
075        result.put("skin", skinId);
076        result.put("template", templateName);
077
078        return result;
079    }
080}