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;
017
018import java.util.Iterator;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
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.components.modules.input.InputModule;
027import org.apache.commons.collections.iterators.ArrayIterator;
028
029/**
030 * This input module gives access to various URI prefixes used for computing actual URIs depending on the rendering context (front, preview, back), 
031 * on the current path prefix (live/, preview/, ...) and other environment constraints.
032 * It provides the following attributes:
033 * <blockquote><table border="1" cellpadding="2">
034 *   <tr><th>Attribute</th>
035 *       <th>Description</th>
036 *       <th>Usage</th>
037 *       <th>CMS (BO) value</th>
038 *       <th>Front-office value</th></tr>
039 *   <tr><th rowspan="2"><i>uri-prefix</i></th>
040 *       <td rowspan="2">the uri prefix, with or without site name</td>
041 *       <td><code>{uri-prefix:uri-prefix}</code></td>
042 *       <td>/cmscontext/prefix</td>
043 *       <td>/sitecontext</td></tr>
044 *   <tr><td><code>{uri-prefix:uri-prefix:site}</code></td>
045 *       <td>/cmscontext/prefix/site</td>
046 *       <td>/sitecontext</td></tr>
047 *   <tr><th rowspan="2"><i>absolute-uri-prefix</i></th>
048 *       <td rowspan="2">the absolute uri prefix, with or without site name</td>
049 *       <td><code>{uri-prefix:absolute-uri-prefix}</code></td>
050 *       <td>http://cms.server.com:port/cmscontext/prefix</td>
051 *       <td>http://www.server.com:port/sitecontext</td></tr>
052 *   <tr><td><code>{uri-prefix:absolute-uri-prefix:site}</code></td>
053 *       <td>http://cms.server.com:port/cmscontext/prefix/site</td>
054 *       <td>http://www.server.com:port/sitecontext</td></tr>
055 * </table></blockquote>
056 * 
057 * <h4>uri-prefix</h4>
058 * <p>Represents the full prefix, with or without site name.
059 * It comes in two forms: with and without the site name.
060 * Use the first form when creating site-independent links, such as plugin URIs.
061 * Use the first form when creating links within a given site, for instance when linking to a page.
062 * <blockquote>Examples: <i>/app/_cms</i> (without site), <i>/app/_cms/www</i> (with site)</blockquote>
063 * </p>
064 * <h4>absolute-uri-prefix</h4>
065 * <p>Absolutized version of the previous variable. Use it when linking to a page
066 * or an URI from outside the server, for instance when sending a link to a specific
067 * page in an e-mail.
068 * <blockquote>Example: <i>http://cms.server:8080/app/_cms/www</i></blockquote>
069 * </p>
070 */
071public class URIPrefixInputModule implements InputModule, Serviceable
072{
073    private static final String __URI_PREFIX = "uri-prefix";
074    private static final String __ABSOLUTE_URI_PREFIX = "absolute-uri-prefix";
075
076    private static URIPrefixHandler _prefixHandler;
077    
078    @Override
079    public void service(ServiceManager manager) throws ServiceException
080    {
081        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
082    }
083    
084    @Override
085    public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
086    {
087        if (__URI_PREFIX.equals(name))
088        {
089            return _prefixHandler.getUriPrefix();
090        }
091        else if (__ABSOLUTE_URI_PREFIX.equals(name))
092        {
093            return _prefixHandler.getAbsoluteUriPrefix();
094        }
095        else if (name != null && name.startsWith(__URI_PREFIX + ":"))
096        {
097            // Context workspace path with sitename.
098            String siteName = name.substring(__URI_PREFIX.length() + 1);
099            return _prefixHandler.getUriPrefix(siteName);
100        }
101        else if (name != null && name.startsWith(__ABSOLUTE_URI_PREFIX + ":"))
102        {
103            String siteName = name.substring(__ABSOLUTE_URI_PREFIX.length() + 1);
104            return _prefixHandler.getAbsoluteUriPrefix(siteName);
105        }
106        
107        throw new ConfigurationException("The attribute " + name + " does not exist in the URIPrefixInputModule.");
108    }
109
110    @Override
111    public Iterator getAttributeNames(Configuration modeConf, Map objectModel) throws ConfigurationException
112    {
113        return new ArrayIterator(new String[]{__URI_PREFIX, __ABSOLUTE_URI_PREFIX});
114    }
115
116    @Override
117    public Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel) throws ConfigurationException
118    {
119        return new Object[] {getAttribute(name, modeConf, objectModel)};
120    }
121}