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