001/*
002 *  Copyright 2014 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.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.ProcessingException;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032
033import org.ametys.cms.repository.Content;
034import org.ametys.plugins.explorer.resources.Resource;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037
038/**
039 * Action to convert a content attachment file into images.
040 */
041public class ConvertContentAttachment2ImagesAction extends ServiceableAction
042{
043    /** The ametys object resolver. */
044    protected AmetysObjectResolver _resolver;
045    
046    /** The component for conversion */
047    protected ConvertContentAttachment2ImagesComponent _attachmentComponent;
048    
049    @Override
050    public void service(ServiceManager serviceManager) throws ServiceException
051    {
052        super.service(serviceManager);
053        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
054        _attachmentComponent = (ConvertContentAttachment2ImagesComponent) serviceManager.lookup(ConvertContentAttachment2ImagesComponent.ROLE);
055    }
056    
057    @Override
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        String siteName = (String) request.getAttribute("site");
062        
063        // Get the resource.
064        String resourcePath = parameters.getParameter("path", request.getParameter("path"));
065        
066        try
067        {
068            Resource resource = _resolver.resolveByPath(_decodePath(resourcePath));
069            
070            String cachePath = _attachmentComponent.doCache(resource, parameters.getParameter("contentName", null), siteName);
071            request.setAttribute(ImagesGenerator.IMAGES_DIRECTORY_PATH_REQUEST_ATTR, cachePath);
072            
073            // We need to set content in request attribute for content attachment resolver
074            Content content = _getContent(resource);
075            request.setAttribute(Content.class.getName(), content);
076            
077            Map<String, String> result = new HashMap<>();
078            result.put("resourceId", resource.getId());
079            return result;
080        }
081        catch (Exception e)
082        {
083            throw new ProcessingException("An error occurred during resolving resource path " + resourcePath, e);
084        }
085    }
086    
087    /**
088     * Get the content from resource
089     * @param resource The resource
090     * @return the content or null if not found
091     */
092    protected Content _getContent (Resource resource)
093    {
094        AmetysObject parent = resource.getParent();
095        while (parent != null)
096        {
097            if (parent instanceof Content)
098            {
099                return (Content) parent;
100            }
101            parent = parent.getParent();
102        }
103        return null;
104    }
105    
106    /**
107     * Decode the resource path
108     * @param path the resource path
109     * @return the decoded resource path
110     * @throws UnsupportedEncodingException if UTF-8 encoding is not supported
111     */
112    protected String _decodePath (String path) throws UnsupportedEncodingException
113    {
114        StringBuffer sb = new StringBuffer();
115        
116        String[] parts = path.split("/");
117        for (String part : parts)
118        {
119            sb.append("/");
120            sb.append(URLDecoder.decode(part, "utf-8"));
121        }
122        return sb.toString();
123    }
124}