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