001/*
002 *  Copyright 2011 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.plugins.calendar.events;
017
018import java.io.UnsupportedEncodingException;
019import java.net.URLDecoder;
020import java.util.Map;
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;
030import org.apache.commons.lang.StringUtils;
031
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.web.repository.page.ZoneItem;
034
035/**
036 * Matcher which retrieves the zone item from its ID in the request URI and sets it as a request attribute.
037 */
038public class ZoneItemMatcher extends WildcardURIMatcher implements Serviceable
039{
040    
041    /** The ametys object resolver. */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    @Override
051    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
052    {
053        Map result = super.match(pattern, objectModel, parameters);
054        
055        String index = parameters.getParameter("zoneItemIndex", "1");
056        
057        if (result != null && StringUtils.isNotEmpty(index))
058        {
059            String zoneItemId = (String) result.get(index);
060            if (StringUtils.isNotEmpty(zoneItemId))
061            {
062                try
063                {
064                    String decodedId = URLDecoder.decode(zoneItemId, "UTF-8");
065                    
066                    ZoneItem zoneItem = _resolver.resolveById(decodedId);
067                    
068                    Request request = ObjectModelHelper.getRequest(objectModel);
069                    request.setAttribute(ZoneItem.class.getName(), zoneItem);
070                }
071                catch (UnsupportedEncodingException e)
072                {
073                    getLogger().error("UTF-8 is an unsupported encoding.", e);
074                    throw new PatternException("UTF-8 is an unsupported encoding.", e);
075                }
076            }
077        }
078        
079        return result;
080    }
081    
082}