001/*
002 *  Copyright 2021 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.io.IOException;
019import java.io.InputStream;
020import java.io.Serializable;
021import java.util.Map;
022
023import javax.jcr.Node;
024import javax.jcr.Repository;
025import javax.jcr.RepositoryException;
026
027import org.apache.avalon.framework.parameters.Parameters;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031import org.apache.cocoon.ProcessingException;
032import org.apache.cocoon.caching.CacheableProcessingComponent;
033import org.apache.cocoon.environment.SourceResolver;
034import org.apache.cocoon.reading.AbstractReader;
035import org.apache.commons.io.IOUtils;
036import org.apache.excalibur.source.SourceValidity;
037import org.apache.excalibur.source.impl.validity.TimeStampValidity;
038import org.xml.sax.SAXException;
039
040import org.ametys.cms.data.type.ResourceElementTypeHelper;
041import org.ametys.plugins.repository.AmetysRepositoryException;
042import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
043import org.ametys.plugins.repository.provider.AbstractRepository;
044
045/**
046 * Read a resource stored in the repository by its UUID
047 */
048public class ResourceByUUIDReader extends AbstractReader implements Serviceable, CacheableProcessingComponent
049{
050    private String _uuid;
051    private Repository _repository;
052    private Resource _resource;
053    
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        _repository = (Repository) smanager.lookup(AbstractRepository.ROLE);
057    }
058    
059    @Override
060    public void setup(SourceResolver sourceResolver, Map objectModelMap, String src, Parameters par) throws ProcessingException, SAXException, IOException
061    {
062        super.setup(sourceResolver, objectModelMap, src, par);
063        _uuid = src;
064        
065        javax.jcr.Session session = null;
066        try
067        {
068            session = _repository.login(); 
069            Node node = session.getNodeByIdentifier(_uuid);
070            JCRRepositoryData resourceData = new JCRRepositoryData(node);
071            _resource = new Resource(resourceData);
072            ResourceElementTypeHelper.readResourceData(resourceData, _resource);
073        }
074        catch (RepositoryException e)
075        {
076            if (session != null)
077            {
078                session.logout();
079            }
080
081            throw new AmetysRepositoryException("Unable to get resource for uuid: " + _uuid, e);
082        }
083    }
084
085    public void generate() throws IOException, SAXException, ProcessingException
086    {
087        try (InputStream is = _resource.getInputStream())
088        {
089            IOUtils.copy(is, out);
090        }
091    }
092
093    public Serializable getKey()
094    {
095        return this.getClass().getName() + "_" + _uuid;
096    }
097
098    public SourceValidity getValidity()
099    {
100        return new TimeStampValidity(getLastModified());
101    }
102    
103    @Override
104    public long getLastModified()
105    {
106        return _resource.getLastModificationDate().toInstant().toEpochMilli();
107    }
108    
109    @Override
110    public void recycle()
111    {
112        super.recycle();
113        _resource = null;
114    }
115}