001/* 002 * Copyright 2018 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.odf.cdmfr; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.context.Context; 020import org.apache.avalon.framework.context.ContextException; 021import org.apache.avalon.framework.context.Contextualizable; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.cocoon.components.ContextHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.cocoon.environment.Response; 028import org.xml.sax.ContentHandler; 029import org.xml.sax.SAXException; 030 031import org.ametys.cms.repository.Content; 032import org.ametys.odf.program.Program; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034 035/** 036 * Default helper to export a {@link Program} to CDM-fr format. 037 */ 038public class ExportCDMfrHelper implements Component, Serviceable, Contextualizable 039{ 040 /** Avalon Role */ 041 public static final String ROLE = ExportCDMfrHelper.class.getName(); 042 043 /** The name of request parameter to use to get the valid version of content */ 044 public static final String REQUEST_PARAM_VALID_LABEL = "validLabel"; 045 046 /** The name of request parameter to set this export for a Ametys application */ 047 public static final String REQUEST_PARAM_EXPORT_FOR_AMETYS = "forAmetys"; 048 049 /** The Ametys object resolver */ 050 protected AmetysObjectResolver _resolver; 051 /** The component for CDM export */ 052 protected ExportCDMfrManager _exportCMDfrManager; 053 /** The Avalon context */ 054 protected Context _context; 055 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 059 _exportCMDfrManager = (ExportCDMfrManager) smanager.lookup(ExportCDMfrManager.ROLE); 060 } 061 062 public void contextualize(Context context) throws ContextException 063 { 064 _context = context; 065 } 066 067 /** 068 * Generate the CDM-fr XML export for the current program. 069 * @param contentHandler The content handler 070 * @throws SAXException If an error occurs during sax 071 */ 072 public void export(ContentHandler contentHandler) throws SAXException 073 { 074 Program program = _getProgram(); 075 076 _prepareRequest(program); 077 078 _prepareResponse(program); 079 080 _doExport(contentHandler, program); 081 } 082 083 /** 084 * Do export 085 * @param contentHandler The content handler to sax into 086 * @param program the porogram to export 087 * @throws SAXException If an error occurs during sax 088 */ 089 protected void _doExport(ContentHandler contentHandler, Program program) throws SAXException 090 { 091 _exportCMDfrManager.generateCDM(contentHandler, program); 092 } 093 094 /** 095 * Get the program to export 096 * @return the program to export 097 */ 098 protected Program _getProgram() 099 { 100 // Get the program from request 101 Request request = _getRequest(); 102 103 String programId = request.getParameter("id"); 104 Program program = _resolver.resolveById(programId); 105 106 // Add the program to the request 107 request.setAttribute(Content.class.getName(), program); 108 109 return program; 110 } 111 112 /** 113 * Prepare the request before export 114 * @param program the program to export 115 */ 116 protected void _prepareRequest(Program program) 117 { 118 Request request = _getRequest(); 119 120 if (request.getParameter(REQUEST_PARAM_VALID_LABEL) != null) 121 { 122 request.setAttribute(ExportCDMfrManager.REQUEST_ATTRIBUTE_VALID_LABEL, true); 123 } 124 125 if (request.getParameter(REQUEST_PARAM_EXPORT_FOR_AMETYS) != null) 126 { 127 request.setAttribute(ExportCDMfrManager.REQUEST_ATTRIBUTE_EXPORT_FOR_AMETYS, true); 128 } 129 } 130 131 /** 132 * Prepare the response 133 * @param program the program to export 134 */ 135 protected void _prepareResponse(Program program) 136 { 137 Response response = ContextHelper.getResponse(_context); 138 response.setHeader("Content-Disposition", "attachment; filename=\"" + program.getCDMId() + ".xml" + "\""); 139 } 140 141 /** 142 * Get the request 143 * @return the request 144 */ 145 protected Request _getRequest() 146 { 147 return ContextHelper.getRequest(_context); 148 } 149}