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