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.web;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.cms.transformation.URIResolver;
030import org.ametys.web.renderingcontext.RenderingContext;
031import org.ametys.web.renderingcontext.RenderingContextHandler;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 * Component providing base paths for computing URIs.
037 */
038public class URIPrefixHandler implements Serviceable, Contextualizable, Component
039{
040    /** Avalon role */
041    public static final String ROLE = URIPrefixHandler.class.getName();
042    
043    private RenderingContextHandler _renderingContextHandler;
044    private SiteManager _siteManager;
045    private org.ametys.cms.URIPrefixHandler _cmsPrefixHandler;
046    
047    private Context _context;
048
049    @Override
050    public void contextualize(Context context) throws ContextException
051    {
052        _context = context;
053    }
054
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
059        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
060        _cmsPrefixHandler = (org.ametys.cms.URIPrefixHandler) manager.lookup(org.ametys.cms.URIPrefixHandler.ROLE);
061    }
062    
063    /**
064     * Computes an URI prefix. Uses by various {@link URIResolver}.
065     * @param siteName the current site name. May be null or empty.
066     * @param absolute if the resulting prefix should be absolute.
067     * @param internal if the resulting prefix should be internal.
068     * @return the computes prefix.
069     */
070    public String computeUriPrefix(String siteName, boolean absolute, boolean internal)
071    {
072        if (StringUtils.isEmpty(siteName))
073        {
074            return _cmsPrefixHandler.computeUriPrefix(absolute, internal);
075        }
076        
077        if (internal)
078        {
079            return "cocoon://" + siteName;
080        }
081        else if (absolute)
082        {
083            return getAbsoluteUriPrefix(siteName);
084        }
085        else
086        {
087            return getUriPrefix(siteName);
088        }
089    }
090    
091    /**
092     * Get the application root. Can be empty if the application resides in the root context. <br>
093     * Use it to create a link beginning with the application root.
094     * @return The application root uri.
095     */
096    public String getUriPrefix()
097    {
098        Request request = ContextHelper.getRequest(_context);
099        
100        // _contextPath is set by the front-office or in certain particular cases to force FO-like links.
101        String externalContextPath = request.getParameter("_contextPath"); 
102        if (externalContextPath == null)
103        {
104            externalContextPath = (String) request.getAttribute("_contextPath");
105        }
106        
107        if (externalContextPath != null)
108        {
109            return externalContextPath;
110        }
111        else
112        {
113            return _cmsPrefixHandler.getUriPrefix();
114        }
115    }
116    
117    /**
118     * Get the full path to the workspace with site name.
119     * If the current workspace is the default one, the workspace path part will be empty.
120     * Use it when creating site-dependent links, such as page URIs.
121     * @param siteName the current site name.
122     * @return The workspace context path with site name.
123     */
124    public String getUriPrefix(String siteName)
125    {
126        if (_renderingContextHandler.getRenderingContext() == RenderingContext.FRONT)
127        {
128            return getUriPrefix();
129        }
130        
131        return getUriPrefix() + "/" + siteName;
132    }
133    
134    /**
135     * Get the absolutized version of the root uri.<br>
136     * Use it to create an absolute link beginning with the application root, 
137     * for instance when sending a mail linking to the application.
138     * @return The absolute root uri.
139     */
140    public String getAbsoluteUriPrefix()
141    {
142        Request request = ContextHelper.getRequest(_context);
143        
144        // _baseServerPath is set by the front-office or in certain particular cases to force FO-like links.
145        String baseServerPath = request.getParameter("_baseServerPath");
146        if (baseServerPath == null)
147        {
148            baseServerPath = (String) request.getAttribute("_baseServerPath");
149        }
150        
151        String uriPrefix = getUriPrefix();
152        
153        if (StringUtils.isNotEmpty(baseServerPath))
154        {
155            uriPrefix = baseServerPath + uriPrefix;
156        }
157        
158        if (!uriPrefix.startsWith("http"))
159        {
160            String scheme = request.getScheme();
161            int normalPort = scheme.equals("http") ? 80 : scheme.equals("https") ? 443 : -1;
162            int requestPort = request.getServerPort();
163            
164            uriPrefix = request.getScheme() + "://" + request.getServerName() + (requestPort != normalPort ? ":" + requestPort : "") + uriPrefix;
165        }
166        
167        return uriPrefix;
168    }
169    
170    /**
171     * Get an absolutized version of the workspace context path. Use it when
172     * linking to a page or an URI from outside the server, for instance when
173     * sending a link to a specific page in an e-mail.
174     * @param siteName The name of the current site.
175     * @return The absolute workspace context path.
176     */
177    public String getAbsoluteUriPrefix(String siteName)
178    {
179        if (_renderingContextHandler.getRenderingContext() == RenderingContext.FRONT)
180        {
181            Site site = _siteManager.getSite(siteName);
182            return site.getUrl();
183        }
184        
185        return getAbsoluteUriPrefix() + "/" + siteName; 
186    }
187}