001/* 002 * Copyright 2023 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.cms.data; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.stream.Stream; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.acting.ServiceableAction; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Redirector; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.environment.SourceResolver; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 033import org.ametys.plugins.repository.AmetysObject; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035 036/** 037 * Get the ametys object binary from attribute and put it in the request 038 */ 039public class GetAmetysObjectBinaryAction extends ServiceableAction 040{ 041 /** The ametys object resolver */ 042 protected AmetysObjectResolver _resolver; 043 044 @Override 045 public void service(ServiceManager serviceManager) throws ServiceException 046 { 047 super.service(serviceManager); 048 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 049 } 050 051 public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 052 { 053 Map<String, String> result = new HashMap<>(); 054 Request request = ObjectModelHelper.getRequest(objectModel); 055 056 AmetysObject ametysObject; 057 String id = parameters.getParameter("id", null); 058 String path = parameters.getParameter("path", null); 059 String fileName = parameters.getParameter("fileName", null); 060 061 if (id != null) 062 { 063 ametysObject = _resolver.resolveById(id); 064 } 065 else if (path != null) 066 { 067 path = path.replaceAll("%3A", ":"); 068 ametysObject = _resolver.resolveByPath(path); 069 } 070 else 071 { 072 ametysObject = (AmetysObject) request.getAttribute(AmetysObject.class.getName()); 073 } 074 075 String attribute = parameters.getParameter("attribute", null); 076 077 if (StringUtils.isNotBlank(attribute) && ametysObject instanceof ModelAwareDataAwareAmetysObject modelAwareDataAwareAmetysObject) 078 { 079 Binary binary = _getBinary(modelAwareDataAwareAmetysObject, attribute, fileName); 080 request.setAttribute(Binary.class.getName(), binary); 081 } 082 083 084 return result; 085 } 086 087 private Binary _getBinary(ModelAwareDataAwareAmetysObject object, String attribute, String filename) 088 { 089 if (object.isMultiple(attribute)) 090 { 091 // if multiple binary, take the first file with name equals to the filename 092 Binary[] binaries = object.getValue(attribute, false, new Binary[0]); 093 return Stream.of(binaries) 094 .filter(b -> b.getFilename().equals(filename)) 095 .findFirst() 096 .orElse(null); 097 } 098 099 return object.getValue(attribute); 100 } 101} 102