001/* 002 * Copyright 2010 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.explorer.resources.generators; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.ServiceableGenerator; 026import org.apache.cocoon.serialization.ZipArchiveSerializer; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.core.util.URLEncoder; 032import org.ametys.plugins.explorer.resources.Resource; 033import org.ametys.plugins.explorer.resources.ResourceCollection; 034import org.ametys.plugins.repository.AmetysObject; 035import org.ametys.plugins.repository.AmetysObjectIterable; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037 038/** 039 * Generates a ZIP archive for the selected resources 040 * 041 */ 042public class ZipArchiveGenerator extends ServiceableGenerator 043{ 044 private AmetysObjectResolver _resolver; 045 046 @Override 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 super.service(smanager); 050 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 051 } 052 053 @Override 054 public void generate() throws IOException, SAXException, ProcessingException 055 { 056 Request request = ObjectModelHelper.getRequest(objectModel); 057 String id = request.getParameter("id"); 058 String[] files = request.getParameterValues("file"); 059 060 contentHandler.startDocument(); 061 contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE); 062 063 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 064 065 if (files != null) 066 { 067 // Export the selected files 068 069 for (String fileId : files) 070 { 071 Resource resource = _resolver.resolveById(fileId); 072 073 AttributesImpl attrs = new AttributesImpl(); 074 attrs.addCDATAAttribute("name", resource.getName()); 075 attrs.addCDATAAttribute("src", "cocoon:/resource?id=" + URLEncoder.encodeParameter(fileId)); 076 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", attrs); 077 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry"); 078 } 079 } 080 else 081 { 082 // Export only files of parent folder 083 ResourceCollection collection = _resolver.resolveById(id); 084 085 AmetysObjectIterable<AmetysObject> children = collection.getChildren(); 086 for (AmetysObject ao : children) 087 { 088 if (ao instanceof Resource) 089 { 090 Resource resource = (Resource) ao; 091 092 AttributesImpl attrs = new AttributesImpl(); 093 attrs.addCDATAAttribute("name", resource.getName()); 094 attrs.addCDATAAttribute("src", "cocoon:/resource?id=" + URLEncoder.encodeParameter(resource.getId())); 095 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", attrs); 096 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry"); 097 } 098 } 099 } 100 101 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 102 103 contentHandler.endPrefixMapping("zip"); 104 contentHandler.endDocument(); 105 } 106 107}