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.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; 031 032import org.ametys.plugins.explorer.resources.Resource; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034 035/** 036 * Action to convert a resource file into images. 037 */ 038public class ConvertResource2ImagesAction extends ServiceableAction 039{ 040 /** The ametys object resolver. */ 041 protected AmetysObjectResolver _resolver; 042 /** The component for conversion */ 043 protected ConvertResource2ImagesComponent _resourceComponent; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 050 _resourceComponent = (ConvertResource2ImagesComponent) serviceManager.lookup(ConvertResource2ImagesComponent.ROLE); 051 } 052 053 @Override 054 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 055 { 056 Request request = ObjectModelHelper.getRequest(objectModel); 057 String siteName = (String) request.getAttribute("site"); 058 059 // Get the resource. 060 String resourcePath = parameters.getParameter("path", request.getParameter("path")); 061 Resource resource = _resolver.resolveByPath(_decodePath(resourcePath.substring(1))); 062 063 String cachePath = _resourceComponent.doCache(resource, siteName); 064 065 request.setAttribute(ImagesGenerator.IMAGES_DIRECTORY_PATH_REQUEST_ATTR, cachePath); 066 067 Map<String, String> result = new HashMap<>(); 068 result.put("resourceId", resource.getId()); 069 return result; 070 } 071 072 /** 073 * Decode the resource path 074 * @param path the resource path 075 * @return the decoded resource path 076 * @throws UnsupportedEncodingException if UTF-8 encoding is not supported 077 */ 078 protected String _decodePath (String path) throws UnsupportedEncodingException 079 { 080 StringBuffer sb = new StringBuffer(); 081 082 String[] parts = path.split("/"); 083 for (String part : parts) 084 { 085 sb.append("/"); 086 sb.append(URLDecoder.decode(part, "utf-8")); 087 } 088 return sb.toString(); 089 } 090}