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.source;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.Map;
021import java.util.regex.Matcher;
022
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang.StringUtils;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceNotFoundException;
028
029import org.ametys.web.skin.Skin;
030import org.ametys.web.skin.SkinTemplate;
031
032/**
033 * Resolve the template:// protocol seeking file in skins/{siteConf:skin}/templates/{currentTemplate}/
034 * or template:forcedvalue#defaultvalue://
035 * @see SkinSourceFactory
036 */
037public class TemplateSourceFactory extends SkinSourceFactory
038{
039    @Override
040    public Source getSource(String location, Map parameters) throws IOException, SourceNotFoundException
041    {
042        // lazy initialization to prevent chicken/egg scenario on startup
043        initializeComponents();
044        
045        Matcher m = __SOURCE_PATTERN.matcher(location);
046        if (!m.matches())
047        {
048            throw new MalformedURLException("URI must be like protocol://path/to/resource. Location was '" + location + "'");
049        }
050
051        String templateName = getTemplate(m.group(2), m.group(4)).getId();
052
053        String templateLocation = m.group(5);
054
055        return super.getSource("skin://templates/" + templateName + "/" + templateLocation, parameters);
056    }
057    
058    /**
059     * Return the current template
060     * @param name the skin name. Can be null or empty
061     * @param defaultValue switch to this value if skin cannot be determined, or if the determined skin does not exist. Can be null
062     * @return the template. Cannot be null.
063     * @throws SourceNotFoundException If the template cannot be determiner or the template do not exist
064     */
065    protected SkinTemplate getTemplate(String name, String defaultValue) throws SourceNotFoundException
066    {
067        Skin skin = getSkin(null, null);
068
069        String templateName = name;
070        
071        if (StringUtils.isEmpty(templateName))
072        {
073            Request request = ContextHelper.getRequest(_context);
074            templateName = (String) request.getAttribute("template");
075        }
076        
077        if (StringUtils.isEmpty(templateName))
078        {
079            templateName = defaultValue;
080        }
081        
082        if (StringUtils.isEmpty(templateName))
083        {
084            String message = "PageMatcher has to be used in current request to use this protocol.";
085            _logger.info(message);
086            throw new SourceNotFoundException(message);
087        }
088        
089        SkinTemplate template = skin.getTemplate(templateName);
090        if (template == null)
091        {
092            template = skin.getTemplate(defaultValue);
093        }
094        if (template == null)
095        {
096            String message = "The template '" + templateName + "' in the skin '" + skin.getId() + "' site does not exist.";
097            _logger.info(message);
098            throw new SourceNotFoundException(message);
099        }
100
101        return template;
102    }
103}