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.ODFHelper; 033import org.ametys.odf.program.Program; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035 036/** 037 * Default helper to export a {@link Program} to CDM-fr format. 038 */ 039public class ExportCDMfrHelper implements Component, Serviceable, Contextualizable 040{ 041 /** Avalon Role */ 042 public static final String ROLE = ExportCDMfrHelper.class.getName(); 043 044 /** The name of request parameter to use to get the valid version of content */ 045 public static final String REQUEST_PARAM_VALID_LABEL = "validLabel"; 046 047 /** The name of request parameter to set this export for a Ametys application */ 048 public static final String REQUEST_PARAM_EXPORT_FOR_AMETYS = "forAmetys"; 049 050 /** The Ametys object resolver */ 051 protected AmetysObjectResolver _resolver; 052 /** The component for CDM export */ 053 protected ExportCDMfrManager _exportCDMfrManager; 054 /** The Avalon context */ 055 protected Context _context; 056 057 public void service(ServiceManager smanager) throws ServiceException 058 { 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _exportCDMfrManager = (ExportCDMfrManager) smanager.lookup(ExportCDMfrManager.ROLE); 061 } 062 063 public void contextualize(Context context) throws ContextException 064 { 065 _context = context; 066 } 067 068 /** 069 * Generate the CDM-fr XML export for the current program. 070 * @param contentHandler The content handler 071 * @param program the program to export 072 * @throws SAXException If an error occurs during sax 073 */ 074 public void export(ContentHandler contentHandler, Program program) throws SAXException 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 _exportCDMfrManager.generateCDM(contentHandler, program); 092 } 093 094 /** 095 * Prepare the request before export 096 * @param program the program to export 097 */ 098 protected void _prepareRequest(Program program) 099 { 100 Request request = _getRequest(); 101 102 // Add the program to the request 103 request.setAttribute(Content.class.getName(), program); 104 105 if (request.getParameter(REQUEST_PARAM_VALID_LABEL) != null) 106 { 107 request.setAttribute(ODFHelper.REQUEST_ATTRIBUTE_VALID_LABEL, true); 108 } 109 110 if (request.getParameter(REQUEST_PARAM_EXPORT_FOR_AMETYS) != null) 111 { 112 request.setAttribute(ExportCDMfrManager.REQUEST_ATTRIBUTE_EXPORT_FOR_AMETYS, true); 113 } 114 } 115 116 /** 117 * Prepare the response 118 * @param program the program to export 119 */ 120 protected void _prepareResponse(Program program) 121 { 122 Response response = ContextHelper.getResponse(_context); 123 response.setHeader("Content-Disposition", "attachment; filename=\"" + program.getCDMId() + ".xml" + "\""); 124 } 125 126 /** 127 * Get the request 128 * @return the request 129 */ 130 protected Request _getRequest() 131 { 132 return ContextHelper.getRequest(_context); 133 } 134}