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.plugins.explorer.resources.actions; 017 018import java.io.UnsupportedEncodingException; 019import java.net.URLDecoder; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.ResourceNotFoundException; 027import org.apache.cocoon.acting.ServiceableAction; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.SourceResolver; 030 031import org.ametys.core.right.RightManager; 032import org.ametys.plugins.explorer.resources.Resource; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.UnknownAmetysObjectException; 035 036/** 037 * This action adds HTTP headers to the response for a ZIP archive 038 * 039 */ 040public class IsResourceCacheableAction extends ServiceableAction 041{ 042 private AmetysObjectResolver _resolver; 043 private RightManager _rightManager; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 049 _rightManager = (RightManager) smanager.lookup(RightManager.ROLE); 050 } 051 052 @Override 053 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 054 { 055 Map<String, Object> result = new HashMap<>(); 056 057 String id = parameters.getParameter("id", null); 058 String path = parameters.getParameter("path", null); 059 060 try 061 { 062 Resource resource = null; 063 if (id != null) 064 { 065 resource = _resolver.resolveById(id); 066 } 067 else 068 { 069 resource = _resolver.resolveByPath(_decodePath(path.substring(1))); 070 } 071 072 result.put("isCacheable", _rightManager.hasAnonymousReadAccess(resource.getParent())); 073 return result; 074 } 075 catch (UnknownAmetysObjectException e) 076 { 077 if (id != null) 078 { 079 throw new ResourceNotFoundException(String.format("The resource with id '%s' does not exist", id)); 080 } 081 else 082 { 083 throw new ResourceNotFoundException(String.format("The resource at path '%s' does not exist", path)); 084 } 085 } 086 } 087 088 private String _decodePath (String path) throws UnsupportedEncodingException 089 { 090 StringBuffer sb = new StringBuffer(); 091 092 String[] parts = path.split("/"); 093 for (String part : parts) 094 { 095 sb.append("/"); 096 sb.append(URLDecoder.decode(part, "utf-8")); 097 } 098 return sb.toString(); 099 } 100}