001/*
002 *  Copyright 2016 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.content;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ResourceNotFoundException;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.cms.transformation.xslt.ResolveURIComponent;
031import org.ametys.plugins.explorer.resources.Resource;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.runtime.authentication.AccessDeniedException;
036import org.ametys.web.repository.page.Page;
037
038/**
039 * Get the page attachment uri from the id of a resource.
040 * Tests if the current asked resource exists and it is an resource of a page.<br>
041 * <ul>
042 * <li>If the resource does not exist, an {@link ResourceNotFoundException} is thrown.</li>
043 * <li>If the resource is not an resource of a page, an {@link AccessDeniedException} is thrown.</li>
044 * <li>If the resource exists and is a page's attachment, redirect to the page attachment uri.</li>
045 * </ul>
046 */
047public class GetPageAttachmentAction extends ServiceableAction
048{
049    private AmetysObjectResolver _resolver;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
056    }
057    
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        Page currentPage = (Page) request.getAttribute(Page.class.getName());
062        
063        String id = parameters.getParameter("id", null);
064        boolean download = parameters.getParameterAsBoolean("download", false);
065        
066        try
067        {
068            Resource resource = _resolver.resolveById(id);
069         
070            Page page = _getParentPage(resource);
071            if (page == null)
072            {
073                throw new AccessDeniedException(String.format("Access denied to resource with id '%s' which is not an attachment of a page", id));
074            }
075            
076            request.setAttribute(Page.class.getName(), page);
077            
078            String uri = ResolveURIComponent.resolve("attachment-page", id, download);
079            redirector.globalRedirect(true, uri);
080            
081            return EMPTY_MAP;
082            
083        }
084        catch (UnknownAmetysObjectException e)
085        {
086            throw new ResourceNotFoundException(String.format("The attachment with id '%s' does not exist", id));
087        }
088        finally 
089        {
090            request.setAttribute(Page.class.getName(), currentPage);
091        }
092    }
093    
094    private Page _getParentPage (Resource resource)
095    {
096        AmetysObject parent = resource.getParent();
097        while (parent != null)
098        {
099            if (parent instanceof Page)
100            {
101                return (Page) parent;
102            }
103            parent = parent.getParent();
104        }
105        
106        return null;
107    }
108}