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