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.plugins.flipbook;
017
018import java.io.IOException;
019import java.util.Map;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
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.ProcessingException;
027import org.apache.cocoon.ResourceNotFoundException;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030import org.apache.excalibur.source.Source;
031
032import org.ametys.core.resources.ImageResourceHandler;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034
035/**
036 * Resource handler for pdf resources files 
037 */
038public class PdfCoverResourceHandler extends ImageResourceHandler
039{
040    private static final Pattern _RESOURCE_PATH_PATTERN = Pattern.compile("^(.*resources(/.+))/([^/]+?)\\.pdf\\.([^/.]+)$");
041
042    /** Attachment component */
043    protected ConvertExternalResource2ImagesComponent _externalResourceComponent;
044
045    /** Ametys object resolver */
046    protected AmetysObjectResolver _ametysObjectResolver;
047
048    @Override
049    public void service(ServiceManager serviceManager) throws ServiceException
050    {
051        super.service(serviceManager);
052        _externalResourceComponent = (ConvertExternalResource2ImagesComponent) serviceManager.lookup(ConvertExternalResource2ImagesComponent.ROLE);
053        _ametysObjectResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
054    }
055    
056    @Override
057    public int getPriority()
058    {
059        // Should be processed before the image resource handler
060        return MIN_PRIORITY + 2000;
061    }
062    
063    @Override
064    public Source setup(String location, Map objectModel, Parameters par, Map<String, Object> additionalParameters) throws IOException, ProcessingException
065    {
066        try
067        {
068            return super.setup(location, objectModel, par, additionalParameters);
069        }
070        catch (ResourceNotFoundException e)
071        {
072            // The src provided does not point to a direct file, we can process it has a .pdf.* resource
073            
074            Matcher matcher = _RESOURCE_PATH_PATTERN.matcher(location);
075            
076            if (!matcher.matches())
077            {
078                throw new ProcessingException("Invalid resource path when generating resource images", e);
079            }
080            
081            String name = matcher.group(3) + ".pdf";
082            String path = matcher.group(2) + "/" + name;
083            String uri = matcher.group(1) + "/" + name;
084            
085            try
086            {
087                Source resource = _resolver.resolveURI(uri);
088                
089                Request request = ContextHelper.getRequest(_context);
090                String cachePath = _externalResourceComponent.doCache(resource, path, "external-resources", name);
091                request.setAttribute(ImagesGenerator.IMAGES_DIRECTORY_PATH_REQUEST_ATTR, cachePath);
092
093                Source src =  _resolver.resolveURI(cachePath + "/pages/page1.png");
094                
095                if (!src.exists())
096                {
097                    throw new ResourceNotFoundException("Resource not found for URI : " + src.getURI());
098                }
099                
100                return src;
101            }
102            catch (Exception ex)
103            {
104                throw new ProcessingException("Failed to get resource for URI " + location, ex);
105            }
106        }
107    }
108}