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.editor;
017
018import java.net.URI;
019import java.util.Collections;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.cms.transformation.ConsistencyChecker.CHECK;
032import org.ametys.cms.transformation.URIResolver;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.URIPrefixHandler;
037import org.ametys.web.content.GetSiteAction;
038import org.ametys.web.renderingcontext.RenderingContext;
039import org.ametys.web.renderingcontext.RenderingContextHandler;
040import org.ametys.web.repository.page.Page;
041import org.ametys.web.repository.site.SiteManager;
042
043/**
044 * {@link URIResolver} for type "page".<br>
045 * These links point to a CMS page.
046 */
047public class PageURIResolver extends AbstractLogEnabled implements URIResolver, Serviceable, Contextualizable
048{
049    private AmetysObjectResolver _resolver;
050    private SiteManager _siteManager;
051    private Context _context;
052    private RenderingContextHandler _renderingContextHandler;
053    private URIPrefixHandler _prefixHandler;
054
055    @Override
056    public void contextualize(Context context) throws ContextException
057    {
058        _context = context;
059    }
060    
061    @Override
062    public void service(ServiceManager manager) throws ServiceException
063    {
064        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
065        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
066        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
067        _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
068    }
069    
070    @Override
071    public String getType()
072    {
073        return "page";
074    }
075
076    @Override
077    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
078    {
079        Request request = ContextHelper.getRequest(_context);
080        
081        // Issue CMS-3391
082        @SuppressWarnings("deprecation")
083        String currentSite = (String) request.getAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR);
084        if (currentSite == null)
085        {
086            currentSite = (String) request.getAttribute("siteName");
087        }
088        RenderingContext context = _renderingContextHandler.getRenderingContext();
089        
090        String pagePath;
091        String siteName =  null;
092        
093        try
094        {
095            Page page = _resolver.resolveById(uri);
096            pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html";
097            siteName = page.getSiteName();
098        }
099        catch (UnknownAmetysObjectException e)
100        {
101            getLogger().warn("Link to unexisting page " + uri);
102            return "";
103        }
104        
105        try
106        {
107            if (!siteName.equals(currentSite) && context == RenderingContext.FRONT)
108            {
109                String url = _siteManager.getSite(siteName).getUrl();
110                return url + "/" + pagePath;
111            }
112            
113            if (!(context == RenderingContext.BACK))
114            {
115                StringBuilder result = new StringBuilder();
116                
117                if (internal)
118                {
119                    result.append("cocoon://").append(siteName);
120                }
121                else if (absolute)
122                {
123                    result.append(_prefixHandler.getAbsoluteUriPrefix(siteName));
124                }
125                else
126                {
127                    result.append(_prefixHandler.getUriPrefix(siteName));
128                }
129                
130                result.append("/").append(pagePath);
131                
132                return new URI(null, null, result.toString(), null).toASCIIString();
133            }
134            else // back
135            {
136                return "javascript:parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + uri + "'}) && void(0);";
137            }
138        }
139        catch (Exception e)
140        {
141            throw new IllegalStateException(e);
142        }
143    }
144    
145    @Override
146    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
147    {
148        return resolve(uri, download, absolute, internal);
149    }
150    
151    @Override
152    public String resolveImageAsBase64(String uri, int height, int width)
153    {
154        return resolve(uri, false, false, false);
155    }
156    
157    @Override
158    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
159    {
160        return resolve(uri, download, absolute, internal);
161    }
162    
163    @Override
164    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
165    {
166        return resolve(uri, false, false, false);
167    }
168    
169    @Override
170    public CHECK checkLink(String uri, boolean shortTest)
171    {
172        try
173        {
174            _resolver.resolveById(uri);
175            return CHECK.SUCCESS;
176        }
177        catch (UnknownAmetysObjectException e)
178        {
179            return CHECK.UNKNOWN;
180        }
181    }
182    
183    @Override
184    public I18nizableText getLabel(String uri)
185    {
186        try
187        {
188            Page page = _resolver.resolveById(uri);
189            return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_LABEL", Collections.singletonList(page.getTitle()));
190        }
191        catch (UnknownAmetysObjectException e)
192        {
193            return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_UNKNOWN");
194        }
195    }
196}